mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-10-22 06:06:59 +00:00
feat: same-provider retry for transient upstream failures
Adds spec §1's opt-in retry engine: RetryConfig (top-level, zero value disabled) and retryPolicy (retry.go), wired into upstreamJSON — the single HTTP chokepoint every adapter's chat/embeddings/listModels call already funneled through. A transient failure (net error, 429, 5xx) retries up to Attempts times with exponential backoff capped at 2s, honoring a qualifying Retry-After header on 429, draining and closing each failed attempt's body before retrying. Never retries a non-429 4xx, a translate error, or after context cancellation. Because upstreamJSON returns before any adapter forwards a byte to the client (forwardStream/forwardJSON run only after it returns), the 'zero bytes reached client' safety rule and the 'streaming retries only the initial exchange' rule both hold by construction — no adapter streaming logic needed to change. buildAdapters(cfg) keeps its v0.1 signature: it derives the retryPolicy from cfg.Retry internally rather than taking it as a parameter, so every existing direct call (15+ across provider_openai_test.go) keeps compiling unmodified. Every v0.1 test passes byte-identically, without edits.
This commit is contained in:
@@ -19,7 +19,13 @@ const (
|
||||
|
||||
// anthropicAdapter is the providerAdapter for provider type "anthropic".
|
||||
type anthropicAdapter struct {
|
||||
client *http.Client
|
||||
client *http.Client
|
||||
// retry is nil for every adapter built by a bare newAnthropicAdapter
|
||||
// call (every v0.1 test, and any caller that never wires one) —
|
||||
// nil-safe, per retryPolicy.do's own contract, so leaving it unset is
|
||||
// exactly v0.1's one-attempt behavior. buildAdapters (providers.go)
|
||||
// sets it from cfg.Retry for production use.
|
||||
retry *retryPolicy
|
||||
adapterName string
|
||||
baseURL string
|
||||
apiKey string
|
||||
@@ -106,7 +112,7 @@ func (a *anthropicAdapter) chatCompletion(ctx context.Context, w http.ResponseWr
|
||||
hdr.Set("Accept", "text/event-stream")
|
||||
}
|
||||
|
||||
resp, err := upstreamJSON(ctx, a.client, http.MethodPost, a.baseURL+anthropicMessagesPath, hdr, body)
|
||||
resp, err := upstreamJSON(ctx, a.client, http.MethodPost, a.baseURL+anthropicMessagesPath, hdr, body, a.retry)
|
||||
if err != nil {
|
||||
return usage{}, err
|
||||
}
|
||||
@@ -200,7 +206,7 @@ func (a *anthropicAdapter) embeddings(_ context.Context, _ http.ResponseWriter,
|
||||
// falls back to a provider's explicitly configured models on error rather
|
||||
// than this adapter guessing at a fallback itself.
|
||||
func (a *anthropicAdapter) listModels(ctx context.Context) ([]string, error) {
|
||||
resp, err := upstreamJSON(ctx, a.client, http.MethodGet, a.baseURL+anthropicModelsPath, a.requestHeaders(false), nil)
|
||||
resp, err := upstreamJSON(ctx, a.client, http.MethodGet, a.baseURL+anthropicModelsPath, a.requestHeaders(false), nil, a.retry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user