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

+25
View File
@@ -12,6 +12,31 @@ Runs `go vet ./...`, `go test -count=1 ./...`, and `go build ./...`. The same
target runs in Gitea Actions on every push and pull request
(`.gitea/workflows/ci.yml`).
## Runtime data
The API image contains **no baked data**. At startup it fetches
`GET {FASTAPI_URL}/bootstrap` and keeps `sensors`, `series`, `context` and
`meta` in memory. A background loop refreshes the snapshot; on failure it keeps
the last good snapshot and retries with exponential backoff
**1, 2, 4, 8, 16, 30 s (cap)**, resetting on success.
- `GET /api/readyz` returns `503` until the first valid snapshot is loaded.
After that, FastAPI being down does not fail readiness.
- `GET /api/meta` reports the cache state under `cache`:
`source`, `last_success_at`, `last_error`, `next_retry_at`, `failures`.
- `GET /api/forecast` calls FastAPI `/predict`, remembers the last good points
per `sensor|horizon|scenario`, and serves them with `"cached": true` when
FastAPI is unavailable.
- `testdata/` is used by tests only and is excluded from the image by
`.dockerignore`.
| Env | Default | Purpose |
|---|---|---|
| `FASTAPI_URL` | `http://localhost:8000` | FastAPI base URL |
| `BOOTSTRAP_REFRESH` | `60s` | Healthy snapshot refresh interval |
| `BOOTSTRAP_TIMEOUT` | `20s` | Per-request `/bootstrap` timeout |
| `ML_TIMEOUT` | `1500ms` | Per-request `/predict` timeout |
## Container
The image is a multi-stage build. The runtime is