retry

package
v0.12.146-go Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package retry provides HTTP request execution with exponential backoff. It retries on transient errors (429, 502, 503, 504) and respects the Retry-After response header.

Index

Constants

View Source
const (
	// DefaultBaseDelay is the initial backoff delay.
	DefaultBaseDelay = 500 * time.Millisecond
	// DefaultMaxRetries is the maximum number of retry attempts.
	DefaultMaxRetries = 2
)

Variables

This section is empty.

Functions

func Do

func Do(ctx context.Context, buildReq func() (*http.Request, error), opts ...Option) (*http.Response, error)

Do sends an HTTP request with retry logic. The buildReq function is called before each attempt to produce a fresh *http.Request (since request bodies are consumed on send).

It retries on network errors and retryable HTTP status codes (429, 502, 503, 504), using exponential backoff. If the response includes a Retry-After header, that value is used instead of the computed backoff.

func IsRetryableError

func IsRetryableError(err error) bool

IsRetryableError reports whether an error is transient and worth retrying (timeouts, connection resets, DNS failures).

func IsRetryableStatus

func IsRetryableStatus(code int) bool

IsRetryableStatus reports whether an HTTP status code is retryable.

Types

type Config

type Config struct {
	// BaseDelay is the initial backoff delay (default 500ms).
	BaseDelay time.Duration
	// MaxRetries is the maximum number of retry attempts (default 2).
	MaxRetries int
	// Client is the HTTP client to use. If nil, http.DefaultClient is used.
	Client *http.Client
}

Config controls retry behavior.

type Option

type Option func(*Config)

Option configures retry behavior.

func WithBaseDelay

func WithBaseDelay(d time.Duration) Option

WithBaseDelay sets the initial backoff delay.

func WithClient

func WithClient(cl *http.Client) Option

WithClient sets the HTTP client.

func WithMaxRetries

func WithMaxRetries(n int) Option

WithMaxRetries sets the maximum number of retries.

Jump to

Keyboard shortcuts

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