retry

package
v1.4.8 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxAttempts = 5
	DefaultBaseDelay   = 500 * time.Millisecond
	DefaultMaxDelay    = 30 * time.Second
)

Default parameters for the retry loop.

Variables

This section is empty.

Functions

func ClassifyHTTPResponse

func ClassifyHTTPResponse(resp *http.Response, bodyForError []byte) error

ClassifyHTTPResponse returns a RetryableError for transient HTTP failures (429, 5xx) and a plain error for permanent ones. Returns nil on success (2xx). The retryAfter duration is parsed from the Retry-After header when present.

func Do

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

Do executes fn, retrying on RetryableError up to p.MaxAttempts times with exponential backoff and full jitter. If fn returns a non-retryable error or the context is cancelled, it returns immediately.

func IsRetryable

func IsRetryable(err error) bool

IsRetryable returns true if the error is a RetryableError.

Types

type Policy

type Policy struct {
	MaxAttempts int
	BaseDelay   time.Duration
	MaxDelay    time.Duration
}

Policy controls retry behaviour.

func DefaultPolicy

func DefaultPolicy() Policy

DefaultPolicy returns a sensible default retry policy.

type RetryableError

type RetryableError struct {
	Err        error
	RetryAfter time.Duration
}

RetryableError wraps an error that should be retried and optionally carries a server-suggested wait duration (e.g. from a Retry-After header).

func (*RetryableError) Error

func (e *RetryableError) Error() string

func (*RetryableError) Unwrap

func (e *RetryableError) Unwrap() error

Jump to

Keyboard shortcuts

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