mirror of
https://github.com/AmanTahiliani/box-box.git
synced 2026-08-07 19:56:18 -04:00
feat(#76): harvest request-scoped availability and freshness truth
Backend-only re-cut of the #76 availability work onto main, stacked on the canonical Weekend Context API. Adds request-scoped freshness reporting so aggregate responses cannot report fresh when a component is stale, plus local-first driver summary resolution and cache/pacing truth. The frontend half of #76 is deliberately excluded: it is built on the Weekend shell that failed owner review, including the full-width Partial banner treatment. Availability presentation is re-cut with the shell in #89. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
65
internal/web/freshness.go
Normal file
65
internal/web/freshness.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const (
|
||||
dataSourceHeader = "X-BoxBox-Data-Source"
|
||||
dataFreshnessHeader = "X-BoxBox-Data-Freshness"
|
||||
)
|
||||
|
||||
type staleResponseReporter interface {
|
||||
LastResponseWasStale() bool
|
||||
}
|
||||
|
||||
// markOpenF1Response publishes request-scoped success provenance. Callers must
|
||||
// pass the scoped client used for this response, never Server.client.
|
||||
func markOpenF1Response(w http.ResponseWriter, client staleResponseReporter) {
|
||||
markOpenF1AggregateResponse(w, client, false)
|
||||
}
|
||||
|
||||
func markOpenF1AggregateResponse(w http.ResponseWriter, client staleResponseReporter, partial bool) {
|
||||
freshness := "fresh"
|
||||
if partial {
|
||||
freshness = "partial"
|
||||
}
|
||||
markOpenF1Availability(w, client, freshness)
|
||||
}
|
||||
|
||||
func markOpenF1Availability(w http.ResponseWriter, client staleResponseReporter, freshness string) {
|
||||
w.Header().Set(dataSourceHeader, "openf1")
|
||||
if client != nil && client.LastResponseWasStale() {
|
||||
w.Header().Set(dataFreshnessHeader, "stale")
|
||||
return
|
||||
}
|
||||
if freshness == "" {
|
||||
freshness = "fresh"
|
||||
}
|
||||
w.Header().Set(dataFreshnessHeader, freshness)
|
||||
}
|
||||
|
||||
func markDataResponse(w http.ResponseWriter, source, freshness string) {
|
||||
w.Header().Set(dataSourceHeader, source)
|
||||
w.Header().Set(dataFreshnessHeader, freshness)
|
||||
}
|
||||
|
||||
func markLocalResponse(w http.ResponseWriter, partial bool) {
|
||||
w.Header().Set(dataSourceHeader, "local")
|
||||
if partial {
|
||||
w.Header().Set(dataFreshnessHeader, "partial")
|
||||
return
|
||||
}
|
||||
w.Header().Set(dataFreshnessHeader, "local")
|
||||
}
|
||||
|
||||
func markMixedResponse(w http.ResponseWriter, client staleResponseReporter, partial bool) {
|
||||
w.Header().Set(dataSourceHeader, "mixed")
|
||||
if client != nil && client.LastResponseWasStale() {
|
||||
w.Header().Set(dataFreshnessHeader, "stale")
|
||||
} else if partial {
|
||||
w.Header().Set(dataFreshnessHeader, "partial")
|
||||
} else {
|
||||
w.Header().Set(dataFreshnessHeader, "local")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user