retry

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsRetryable

func IsRetryable(class ErrorClass) bool

IsRetryable returns true if the error class warrants a retry.

Types

type ErrorClass

type ErrorClass int

ErrorClass categorizes an error for retry decisions.

const (
	ErrorClassUnknown ErrorClass = iota
	ErrorClassContextOverflow
	ErrorClassRateLimit
	ErrorClassOverloaded
	ErrorClassServerError
	ErrorClassNetwork
)

func ClassifyError

func ClassifyError(err error) (ErrorClass, string)

ClassifyError examines an error and returns its class and a human-readable reason.

type Policy

type Policy struct {
	MaxAttempts   int
	InitialDelay  time.Duration
	MaxDelay      time.Duration
	BackoffFactor float64
}

Policy configures retry behavior with exponential backoff and retry-after header support.

func DefaultPolicy

func DefaultPolicy() *Policy

DefaultPolicy returns a retry policy with sensible defaults for LLM APIs: 3 attempts, 1s initial delay, 30s max delay, 2x backoff factor.

func (*Policy) Delay

func (p *Policy) Delay(attempt int, headers http.Header) time.Duration

Delay computes the wait duration for the given attempt number (1-based). If headers contain retry-after or retry-after-ms, those take precedence.

func (*Policy) Retry

func (p *Policy) Retry(ctx context.Context, fn func() error) error

Retry executes fn with exponential backoff. It respects context cancellation and classifies errors to decide whether to retry. Returns the last error if all attempts fail, or nil on success.

func (*Policy) RetryWithHeaders

func (p *Policy) RetryWithHeaders(ctx context.Context, fn func() (http.Header, error)) error

RetryWithHeaders is like Retry but the function also returns HTTP response headers for retry-after parsing.

Jump to

Keyboard shortcuts

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