retry

package
v2.23.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do[T any](ctx context.Context, p Policy, op func(ctx context.Context) (T, error)) (T, error)

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

func IsContextError(err error) bool

IsContextError reports whether err is a context's cancellation or deadline.

func ParseRetryAfter

func ParseRetryAfter(h http.Header) time.Duration

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.

func Sleep

func Sleep(ctx context.Context, d time.Duration) error

Sleep waits d, returning ctx.Err() early if ctx ends first.

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

func Exponential(initial, ceiling time.Duration) Backoff

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

func FullJitter(initial, ceiling time.Duration) Backoff

FullJitter is the exponential Backoff whose waits are uniform in [0, min(ceiling, initial*2^attempt)): it spreads callers that failed together the most.

func (Backoff) Delay

func (b Backoff) Delay(attempt int) time.Duration

Delay returns the wait after the given failed attempt (0-based).

type Observer

type Observer func(name string, outcome Outcome)

Observer receives every retry decision Do makes, with the policy's Name.

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.

func (*Policy) Wait

func (p *Policy) Wait(attempt int, after time.Duration) (delay time.Duration, ok bool)

Wait is how long Do waits after the given failed attempt (0-based): the wait the failure asked for, or else the backoff. ok is false when the failure asked for longer than MaxRetryAfter, and Do stops retrying instead.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL