retry

package
v0.49.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 9 Imported by: ? 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 provides HTTP retry logic with exponential backoff for LLM provider API calls.

Index

Constants

View Source
const MaxErrorBody = 4096

MaxErrorBody bounds how much of an error response body is read.

Variables

View Source
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

func Backoff(attempt int) time.Duration

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

func ErrorBody(r io.Reader) (body []byte, text string)

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

type OnRetry func(attempt int, status int, wait time.Duration)

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.

Jump to

Keyboard shortcuts

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