retry

package
v0.31.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package retry provides HTTP retry logic with exponential backoff for LLM provider API calls.

Index

Constants

This section is empty.

Variables

View Source
var DefaultPolicy = Policy{
	MaxRetries: 5,
	BaseDelay:  1 * time.Second,
	MaxDelay:   32 * time.Second,
}

DefaultPolicy is the default retry policy.

Functions

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.

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 {
	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