Documentation
¶
Overview ¶
Package retry is the one attempt loop the runtime's retrying call sites share: provider HTTP requests (providers.DoWithRetry), A2A agent calls, TTS and STT requests, MCP connects and realtime WebSocket connects.
Do runs an operation until it succeeds, fails in a way its policy does not retry, or runs out of attempts. Between attempts it waits the policy's jittered backoff, or the wait the failure itself asked for (a Retry-After), and stops at once when the context ends. Every retry decision is logged and reported to the process-wide Observer, so each call site emits the same retry metric.
Index ¶
- func Do[T any](ctx context.Context, p Policy, op func(ctx context.Context) (T, error)) (T, error)
- func IsContextError(err error) bool
- func ParseRetryAfter(h http.Header) time.Duration
- func SetObserver(o Observer)
- func Sleep(ctx context.Context, d time.Duration) error
- type Backoff
- type Observer
- type Outcome
- type Policy
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Do ¶
Do calls op until it succeeds, fails with an error the policy does not retry, asks to wait longer than MaxRetryAfter, or has made MaxAttempts attempts, and returns the last attempt's value and error. It makes no attempt once ctx has ended, and returns ctx.Err() when ctx ends before or while it waits to retry. A failure after ctx ended is not retried, counted or logged: it returns the attempt's error when that names the context's end, or else ctx.Err().
A policy of one attempt reports nothing to the Observer: there is no retry to count.
func IsContextError ¶
IsContextError reports whether err is a context's cancellation or deadline.
func ParseRetryAfter ¶
ParseRetryAfter returns the wait a Retry-After header asks for, in delta-seconds or HTTP-date form, or zero when it is absent, unparsable or already past.
func SetObserver ¶
func SetObserver(o Observer)
SetObserver installs the process-wide Observer; nil removes it.
Types ¶
type Backoff ¶
type Backoff struct {
// Initial is the wait before the first retry, before jitter.
Initial time.Duration
// Max caps every wait. Zero or negative means no cap short of
// maxUncapped, which keeps a large attempt from overflowing.
Max time.Duration
// Multiplier grows the wait per attempt. Zero means 2 (exponential);
// 1 keeps it fixed.
Multiplier float64
// JitterMin and JitterMax bound the random factor the wait is scaled by.
// Both zero means no jitter.
JitterMin, JitterMax float64
}
Backoff computes the wait before each retry: Initial grown by Multiplier per attempt and capped at Max, then scaled by a uniform random factor in [JitterMin, JitterMax] and capped at Max again, so no wait exceeds Max.
func Exponential ¶
Exponential is the Backoff DoWithRetry has always used: initial doubled per attempt, lengthened by up to half at random, and never more than ceiling.
func FullJitter ¶
FullJitter is the exponential Backoff whose waits are uniform in [0, min(ceiling, initial*2^attempt)): it spreads callers that failed together the most.
type Outcome ¶
type Outcome string
Outcome names a retry decision reported to the Observer. The values are the label values of the provider retry metric.
const ( // OutcomeRetry is reported before each wait for another attempt. OutcomeRetry Outcome = "retry" // OutcomeSuccess is reported when an attempt after the first succeeds. OutcomeSuccess Outcome = "success" // OutcomeExhausted is reported when the last attempt fails with an error // the policy would otherwise have retried. OutcomeExhausted Outcome = "exhausted" // OutcomeRetryAfterTooLong is reported when a failure asks to wait longer // than the policy's MaxRetryAfter, and so is not retried. OutcomeRetryAfterTooLong Outcome = "retry_after_too_long" )
type Policy ¶
type Policy struct {
// Name identifies the call site in logs and to the Observer.
Name string
// MaxAttempts is the number of attempts in all, the first included.
// Values below 1 mean 1.
MaxAttempts int
// Backoff computes the wait between attempts.
Backoff Backoff
// Classify reports whether a failure is worth another attempt and how long
// the failure asked to wait first (zero: use Backoff). Nil retries every
// failure but the context's own.
Classify func(err error) (retry bool, after time.Duration)
// MaxRetryAfter is the longest wait a failure may ask for and still be
// retried. A failure asking for longer is returned at once: retrying
// sooner than the server allows only spends an attempt against a limit
// still in force, and waiting longer would stall the caller. Zero means
// no limit.
MaxRetryAfter time.Duration
// LogAttrs are added to the retry log lines.
LogAttrs []any
}
Policy configures Do.
func FullJitterPolicy ¶
func FullJitterPolicy( kind, provider string, attempts int, initial, ceiling time.Duration, classify func(error) (bool, time.Duration), ) Policy
FullJitterPolicy is the policy of an idempotent one-shot call (a TTS or STT request): attempts in all, FullJitter waits from initial up to ceiling, and a failure's own wait honored up to ceiling. provider is logged and, prefixed with kind, names the site.