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

+14 -11
View File
@@ -1,4 +1,4 @@
// Command api serves the PulseCast Go API, SSE stream and cached fallback.
// Command api serves the PulseCast Go API, SSE stream and runtime snapshot cache.
package main
import (
@@ -12,38 +12,41 @@ import (
"time"
"pulsecast/api/internal/api"
"pulsecast/api/internal/cache"
"pulsecast/api/internal/config"
"pulsecast/api/internal/ml"
"pulsecast/api/internal/sse"
"pulsecast/api/internal/store"
)
func main() {
cfg := config.Load()
// A partial dataset is not fatal: the process starts and /api/readyz reports
// 503 until the baked artifacts are complete, instead of crash-looping.
st, err := store.Load(cfg.DataDir)
if err != nil {
slog.Error("baked data is incomplete", "dir", cfg.DataDir, "error", err)
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
client := ml.NewWithToken(cfg.FastAPIURL, cfg.MLTimeout, cfg.ControlToken)
snapshots := cache.New(client, cache.Options{
RefreshInterval: cfg.RefreshInterval,
Timeout: cfg.BootstrapTimeout,
})
// Loading happens in the background so the process can answer /api/readyz
// with 503 immediately instead of crash-looping while FastAPI starts.
go snapshots.Run(ctx)
hub := sse.NewHub(cfg.Heartbeat)
go hub.Run(ctx)
server := &http.Server{
Addr: cfg.Addr,
Handler: api.New(cfg, st, ml.NewWithToken(cfg.FastAPIURL, cfg.MLTimeout, cfg.ControlToken), hub).Handler(),
Handler: api.New(cfg, snapshots, client, hub).Handler(),
ReadHeaderTimeout: 5 * time.Second,
IdleTimeout: 120 * time.Second,
}
serveErr := make(chan error, 1)
go func() {
slog.Info("api listening", "addr", cfg.Addr, "data_dir", cfg.DataDir, "fastapi", cfg.FastAPIURL)
slog.Info("api listening", "addr", cfg.Addr, "fastapi", cfg.FastAPIURL,
"refresh", cfg.RefreshInterval)
serveErr <- server.ListenAndServe()
}()