feat: runtime bootstrap snapshot cache with backoff
This commit is contained in:
1 parent
08f60a14d1
commit
aa6bb23e24
19 files changed
+1400
-384
No files matched your search
+43
-2
@@ -1,5 +1,6 @@
|
||||
// Package ml is the FastAPI client. Every call is bounded by a 1.5 s timeout;
|
||||
// callers fall back to baked JSON on any error.
|
||||
// Package ml is the FastAPI client. Prediction calls are bounded by a 1.5 s
|
||||
// timeout; callers fall back to the last good forecast on any error. The larger
|
||||
// /bootstrap snapshot gets its own, longer transport timeout.
|
||||
package ml
|
||||
|
||||
import (
|
||||
@@ -23,11 +24,16 @@ const DefaultTimeout = 1500 * time.Millisecond
|
||||
// larger than prediction payloads, so they get more than DefaultTimeout.
|
||||
const controlTimeout = 30 * time.Second
|
||||
|
||||
// bootstrapTimeout is the upper transport bound for the /bootstrap snapshot,
|
||||
// which carries the full hourly series for every sensor.
|
||||
const bootstrapTimeout = 30 * time.Second
|
||||
|
||||
// Client talks to the FastAPI ML service.
|
||||
type Client struct {
|
||||
baseURL string
|
||||
http *http.Client
|
||||
control *http.Client
|
||||
bootstrap *http.Client
|
||||
controlToken string
|
||||
}
|
||||
|
||||
@@ -46,10 +52,45 @@ func NewWithToken(baseURL string, timeout time.Duration, controlToken string) *C
|
||||
baseURL: strings.TrimRight(baseURL, "/"),
|
||||
http: &http.Client{Timeout: timeout},
|
||||
control: &http.Client{Timeout: controlTimeout},
|
||||
bootstrap: &http.Client{Timeout: bootstrapTimeout},
|
||||
controlToken: controlToken,
|
||||
}
|
||||
}
|
||||
|
||||
// Bootstrap fetches GET /bootstrap, the runtime snapshot the API caches. The
|
||||
// caller supplies the shorter request deadline; this method only validates the
|
||||
// transport response and that the snapshot is usable.
|
||||
func (c *Client) Bootstrap(ctx context.Context) (model.Bootstrap, error) {
|
||||
var out model.Bootstrap
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+"/bootstrap", nil)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("ml: build request: %w", err)
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
resp, err := c.bootstrap.Do(req)
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("ml: /bootstrap: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
snippet, _ := io.ReadAll(io.LimitReader(resp.Body, 256))
|
||||
return out, fmt.Errorf("ml: /bootstrap: status %d: %s", resp.StatusCode, strings.TrimSpace(string(snippet)))
|
||||
}
|
||||
raw, err := io.ReadAll(io.LimitReader(resp.Body, 64<<20))
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("ml: read /bootstrap: %w", err)
|
||||
}
|
||||
if err := json.Unmarshal(raw, &out); err != nil {
|
||||
return out, fmt.Errorf("ml: decode /bootstrap: %w", err)
|
||||
}
|
||||
if len(out.Sensors) == 0 {
|
||||
return out, fmt.Errorf("ml: /bootstrap returned no sensors")
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// PredictRequest mirrors CONTRACT.md §3 POST /predict.
|
||||
type PredictRequest struct {
|
||||
Sensor string `json:"sensor"`
|
||||
|
||||
Reference in new issue
Block a user