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 -9
View File
@@ -10,13 +10,17 @@ import (
// Config is the process configuration.
type Config struct {
Addr string
DataDir string
FastAPIURL string
ControlUser string
ControlPass string
ControlToken string
Heartbeat time.Duration
MLTimeout time.Duration
// RefreshInterval is how often a healthy /bootstrap snapshot is refreshed.
RefreshInterval time.Duration
// BootstrapTimeout bounds one /bootstrap request.
BootstrapTimeout time.Duration
}
// Load reads configuration from env with local-development defaults.
@@ -26,14 +30,15 @@ type Config struct {
func Load() Config {
port := getenv("PORT", "8080")
return Config{
Addr: ":" + port,
DataDir: getenv("DATA_DIR", "testdata/data"),
FastAPIURL: getenv("FASTAPI_URL", "http://localhost:8000"),
ControlUser: os.Getenv("CONTROL_USER"),
ControlPass: os.Getenv("CONTROL_PASS"),
ControlToken: os.Getenv("CONTROL_TOKEN"),
Heartbeat: durationEnv("SSE_HEARTBEAT", 15*time.Second),
MLTimeout: durationEnv("ML_TIMEOUT", 1500*time.Millisecond),
Addr: ":" + port,
FastAPIURL: getenv("FASTAPI_URL", "http://localhost:8000"),
ControlUser: os.Getenv("CONTROL_USER"),
ControlPass: os.Getenv("CONTROL_PASS"),
ControlToken: os.Getenv("CONTROL_TOKEN"),
Heartbeat: durationEnv("SSE_HEARTBEAT", 15*time.Second),
MLTimeout: durationEnv("ML_TIMEOUT", 1500*time.Millisecond),
RefreshInterval: durationEnv("BOOTSTRAP_REFRESH", 60*time.Second),
BootstrapTimeout: durationEnv("BOOTSTRAP_TIMEOUT", 20*time.Second),
}
}
+23 -6
View File
@@ -6,22 +6,25 @@ import (
)
func TestLoadDefaults(t *testing.T) {
for _, key := range []string{"PORT", "DATA_DIR", "FASTAPI_URL", "CONTROL_USER", "CONTROL_PASS", "CONTROL_TOKEN", "SSE_HEARTBEAT", "ML_TIMEOUT"} {
for _, key := range []string{
"PORT", "FASTAPI_URL", "CONTROL_USER", "CONTROL_PASS", "CONTROL_TOKEN",
"SSE_HEARTBEAT", "ML_TIMEOUT", "BOOTSTRAP_REFRESH", "BOOTSTRAP_TIMEOUT",
} {
t.Setenv(key, "")
}
cfg := Load()
if cfg.Addr != ":8080" {
t.Errorf("Addr=%q", cfg.Addr)
}
if cfg.DataDir != "testdata/data" {
t.Errorf("DataDir=%q", cfg.DataDir)
}
if cfg.FastAPIURL != "http://localhost:8000" {
t.Errorf("FastAPIURL=%q", cfg.FastAPIURL)
}
if cfg.Heartbeat != 15*time.Second || cfg.MLTimeout != 1500*time.Millisecond {
t.Errorf("timeouts: heartbeat=%s ml=%s", cfg.Heartbeat, cfg.MLTimeout)
}
if cfg.RefreshInterval != 60*time.Second || cfg.BootstrapTimeout != 20*time.Second {
t.Errorf("snapshot: refresh=%s timeout=%s", cfg.RefreshInterval, cfg.BootstrapTimeout)
}
if cfg.ControlAuthConfigured() {
t.Error("control auth should be unconfigured by default")
}
@@ -32,16 +35,17 @@ func TestLoadDefaults(t *testing.T) {
func TestLoadFromEnv(t *testing.T) {
t.Setenv("PORT", "9999")
t.Setenv("DATA_DIR", "/data")
t.Setenv("FASTAPI_URL", "http://ml:8000")
t.Setenv("CONTROL_USER", "user")
t.Setenv("CONTROL_PASS", "pass")
t.Setenv("CONTROL_TOKEN", "token-123")
t.Setenv("SSE_HEARTBEAT", "5s")
t.Setenv("ML_TIMEOUT", "2s")
t.Setenv("BOOTSTRAP_REFRESH", "30s")
t.Setenv("BOOTSTRAP_TIMEOUT", "10s")
cfg := Load()
if cfg.Addr != ":9999" || cfg.DataDir != "/data" || cfg.FastAPIURL != "http://ml:8000" {
if cfg.Addr != ":9999" || cfg.FastAPIURL != "http://ml:8000" {
t.Fatalf("cfg=%+v", cfg)
}
if cfg.ControlToken != "token-123" {
@@ -50,6 +54,9 @@ func TestLoadFromEnv(t *testing.T) {
if cfg.Heartbeat != 5*time.Second || cfg.MLTimeout != 2*time.Second {
t.Fatalf("timeouts: heartbeat=%s ml=%s", cfg.Heartbeat, cfg.MLTimeout)
}
if cfg.RefreshInterval != 30*time.Second || cfg.BootstrapTimeout != 10*time.Second {
t.Fatalf("snapshot: refresh=%s timeout=%s", cfg.RefreshInterval, cfg.BootstrapTimeout)
}
if !cfg.ControlAuthConfigured() {
t.Fatal("control auth should be configured")
}
@@ -58,17 +65,27 @@ func TestLoadFromEnv(t *testing.T) {
func TestDurationAcceptsPlainSeconds(t *testing.T) {
t.Setenv("SSE_HEARTBEAT", "7")
t.Setenv("ML_TIMEOUT", "3")
t.Setenv("BOOTSTRAP_REFRESH", "45")
t.Setenv("BOOTSTRAP_TIMEOUT", "12")
cfg := Load()
if cfg.Heartbeat != 7*time.Second || cfg.MLTimeout != 3*time.Second {
t.Fatalf("timeouts: heartbeat=%s ml=%s", cfg.Heartbeat, cfg.MLTimeout)
}
if cfg.RefreshInterval != 45*time.Second || cfg.BootstrapTimeout != 12*time.Second {
t.Fatalf("snapshot: refresh=%s timeout=%s", cfg.RefreshInterval, cfg.BootstrapTimeout)
}
}
func TestDurationFallsBackOnInvalidValue(t *testing.T) {
t.Setenv("SSE_HEARTBEAT", "soon")
t.Setenv("ML_TIMEOUT", "-1s")
t.Setenv("BOOTSTRAP_REFRESH", "later")
t.Setenv("BOOTSTRAP_TIMEOUT", "-3s")
cfg := Load()
if cfg.Heartbeat != 15*time.Second || cfg.MLTimeout != 1500*time.Millisecond {
t.Fatalf("timeouts: heartbeat=%s ml=%s", cfg.Heartbeat, cfg.MLTimeout)
}
if cfg.RefreshInterval != 60*time.Second || cfg.BootstrapTimeout != 20*time.Second {
t.Fatalf("snapshot: refresh=%s timeout=%s", cfg.RefreshInterval, cfg.BootstrapTimeout)
}
}