feat: runtime bootstrap snapshot cache with backoff

This commit is contained in:
Noel Sabia committed 2026-11-14 06:08:53 +01:00
1 parent 08f60a14d1
commit aa6bb23e24
19 files changed
+1400 -384

No files matched your search

+31 -20
View File
@@ -12,12 +12,12 @@ import (
"sync"
"time"
"pulsecast/api/internal/cache"
"pulsecast/api/internal/config"
"pulsecast/api/internal/ml"
"pulsecast/api/internal/model"
"pulsecast/api/internal/scenario"
"pulsecast/api/internal/sse"
"pulsecast/api/internal/store"
)
// MLClient is the FastAPI prediction surface the data handlers need.
@@ -37,22 +37,23 @@ type TrainClient interface {
// Server holds process-wide state. One instance per pod.
type Server struct {
cfg config.Config
store *store.Store
cache *cache.Cache
ml MLClient
hub *sse.Hub
scenario *scenario.State
loc *time.Location
now func() time.Time
mu sync.RWMutex
sim map[string]map[string][]model.ForecastPoint // sensor -> horizon -> points
simFor string // scenario the sim belongs to
simGen int // invalidates staffing cache
staffing map[string]model.StaffingResponse
mu sync.RWMutex
sim map[string]map[string][]model.ForecastPoint // sensor -> horizon -> points
simFor string // scenario the sim belongs to
simGen int // invalidates staffing cache
staffing map[string]model.StaffingResponse
lastForecast map[string][]model.ForecastPoint // sensor|horizon|scenario -> last live points
}
// New builds a server. hub must be non-nil; main runs hub.Run.
func New(cfg config.Config, st *store.Store, client MLClient, hub *sse.Hub) *Server {
func New(cfg config.Config, snapshots *cache.Cache, client MLClient, hub *sse.Hub) *Server {
loc, err := time.LoadLocation(model.StoreHours.TZ)
if err != nil {
loc = time.UTC
@@ -61,14 +62,15 @@ func New(cfg config.Config, st *store.Store, client MLClient, hub *sse.Hub) *Ser
cfg.MLTimeout = ml.DefaultTimeout
}
return &Server{
cfg: cfg,
store: st,
ml: client,
hub: hub,
scenario: scenario.New(),
loc: loc,
now: time.Now,
staffing: make(map[string]model.StaffingResponse),
cfg: cfg,
cache: snapshots,
ml: client,
hub: hub,
scenario: scenario.New(),
loc: loc,
now: time.Now,
staffing: make(map[string]model.StaffingResponse),
lastForecast: make(map[string][]model.ForecastPoint),
}
}
@@ -194,10 +196,19 @@ func writeError(w http.ResponseWriter, status int, code, message string) {
_, _ = w.Write(raw)
}
// meta snapshots the cache metadata once per response, so versions and the
// cache block cannot disagree when a refresh lands mid-request.
func (s *Server) meta() model.Meta {
if s.cache == nil {
return model.Meta{}
}
return s.cache.Meta()
}
// setVersions adds the contract's version headers.
func (s *Server) setVersions(w http.ResponseWriter) {
data, modelVersion, context := s.store.Versions()
w.Header().Set("X-Data-Version", data)
w.Header().Set("X-Model-Version", modelVersion)
w.Header().Set("X-Context-Version", context)
meta := s.meta()
w.Header().Set("X-Data-Version", meta.DataVersion)
w.Header().Set("X-Model-Version", meta.ModelVersion)
w.Header().Set("X-Context-Version", meta.ContextVersion)
}