retryhttp

package
v0.72.4 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package retryhttp provides an HTTP RoundTripper with exponential backoff retry logic.

This package implements retry functionality at the transport level, making it compatible with any HTTP client including ogen-generated clients.

Index

Constants

This section is empty.

Variables

DefaultRetryableStatusCodes are the status codes that trigger a retry by default.

Functions

func WrapClient

func WrapClient(client *http.Client, opts ...Option) *http.Client

WrapClient wraps an existing http.Client with retry logic. It returns a new client that uses the original client's transport.

Types

type Option

type Option func(*RetryTransport)

Option is a functional option for configuring RetryTransport.

func WithBackoffMultiplier

func WithBackoffMultiplier(m float64) Option

WithBackoffMultiplier sets the backoff multiplier.

func WithInitialBackoff

func WithInitialBackoff(d time.Duration) Option

WithInitialBackoff sets the initial backoff duration.

func WithJitter

func WithJitter(j float64) Option

WithJitter sets the jitter factor (0.0 to 1.0).

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets the logger for error logging.

func WithMaxBackoff

func WithMaxBackoff(d time.Duration) Option

WithMaxBackoff sets the maximum backoff duration.

func WithMaxRetries

func WithMaxRetries(n int) Option

WithMaxRetries sets the maximum number of retries.

func WithOnRetry

func WithOnRetry(fn func(attempt int, req *http.Request, resp *http.Response, err error, backoff time.Duration)) Option

WithOnRetry sets a callback invoked before each retry.

func WithRetryableStatusCodes

func WithRetryableStatusCodes(codes []int) Option

WithRetryableStatusCodes sets the status codes that trigger a retry.

func WithShouldRetry

func WithShouldRetry(fn func(resp *http.Response, err error) bool) Option

WithShouldRetry sets a custom retry decision function.

func WithTransport

func WithTransport(t http.RoundTripper) Option

WithTransport sets the underlying transport.

type RetryTransport

type RetryTransport struct {
	// Transport is the underlying RoundTripper. If nil, http.DefaultTransport is used.
	Transport http.RoundTripper

	// MaxRetries is the maximum number of retry attempts. Default is 3.
	MaxRetries int

	// InitialBackoff is the initial backoff duration. Default is 1 second.
	InitialBackoff time.Duration

	// MaxBackoff is the maximum backoff duration. Default is 30 seconds.
	MaxBackoff time.Duration

	// BackoffMultiplier is the factor by which backoff increases. Default is 2.0.
	BackoffMultiplier float64

	// Jitter adds randomness to backoff to prevent thundering herd. Default is 0.1 (10%).
	Jitter float64

	// RetryableStatusCodes defines which HTTP status codes trigger a retry.
	// Default is 429, 500, 502, 503, 504.
	RetryableStatusCodes []int

	// ShouldRetry is an optional function for custom retry logic.
	// If set, it's called instead of checking RetryableStatusCodes.
	ShouldRetry func(resp *http.Response, err error) bool

	// OnRetry is an optional callback invoked before each retry attempt.
	OnRetry func(attempt int, req *http.Request, resp *http.Response, err error, backoff time.Duration)

	// Logger is used for logging errors. If nil, a null logger is used.
	Logger *slog.Logger
}

RetryTransport wraps an http.RoundTripper with retry logic.

func New

func New() *RetryTransport

New creates a new RetryTransport with default settings.

func NewWithOptions

func NewWithOptions(opts ...Option) *RetryTransport

NewWithOptions creates a new RetryTransport with the given options.

func (*RetryTransport) Client

func (rt *RetryTransport) Client() *http.Client

Client returns an *http.Client configured with this RetryTransport.

func (*RetryTransport) RoundTrip

func (rt *RetryTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper with retry logic.

Jump to

Keyboard shortcuts

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