Documentation
¶
Overview ¶
Package retry provides HTTP retry logic with exponential backoff for LLM provider API calls.
Index ¶
Constants ¶
const MaxErrorBody = 4096
MaxErrorBody bounds how much of an error response body is read.
Variables ¶
var DefaultPolicy = Policy{ MaxRetries: 5, BaseDelay: 1 * time.Second, MaxDelay: 32 * time.Second, }
DefaultPolicy is the default retry policy.
Functions ¶
func Backoff ¶ added in v0.46.0
Backoff supplies the existing positive pacing floor for confirmed quota.
func Do ¶
func Do(ctx context.Context, client *http.Client, buildReq func() (*http.Request, error), p Policy, notify OnRetry) (*http.Response, error)
Do executes an HTTP request with retries on transient failures. The buildReq function is called on each attempt to produce a fresh request (necessary because http.Request.Body is consumed on each attempt). Returns the successful response or the last error.
func ErrorBody ¶ added in v0.46.0
ErrorBody reads a bounded prefix of an error response body, for classification, and returns the text that may be shown for it. A body that was cut (longer than the bound, or a failed read) is not shown: the cut can split a credential the upstream echoed, and redacting whole values downstream would then miss its prefix.
Types ¶
type OnRetry ¶
OnRetry is called before each retry wait. Providers can use this to emit user-visible status updates (e.g. "rate limited, retrying in 2s").
type Policy ¶
type Policy struct {
NoNetworkRetry bool
StartAttempt int
// Wait replaces only the sleeper. The caller may return typed control flow
// to release the credential snapshot before the next attempt.
Wait func(context.Context, int, int, time.Duration) error
MaxRetries int // max retry attempts (default 5)
BaseDelay time.Duration // initial wait (default 1s)
MaxDelay time.Duration // cap per wait (default 32s)
Disabled bool // true = no retries, single attempt only
// Retryable optionally vetoes retrying an otherwise-retryable response.
// It receives the response (headers available) and its body; returning
// false stops retries and returns the response to the caller with its body
// restored, so the caller can parse it (e.g. a usage-limit 429 that a retry
// will never clear). Only consulted for statuses isRetryable already allows.
Retryable func(resp *http.Response, body []byte) bool
}
Policy controls retry behaviour. Use DefaultPolicy for standard settings. A zero-value Policy means "use defaults" — set Disabled=true to skip retries entirely.