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 ¶
- Variables
- func WrapClient(client *http.Client, opts ...Option) *http.Client
- type Option
- func WithBackoffMultiplier(m float64) Option
- func WithInitialBackoff(d time.Duration) Option
- func WithJitter(j float64) Option
- func WithLogger(l *slog.Logger) Option
- func WithMaxBackoff(d time.Duration) Option
- func WithMaxRetries(n int) Option
- func WithOnRetry(fn func(attempt int, req *http.Request, resp *http.Response, err error, ...)) Option
- func WithRetryableStatusCodes(codes []int) Option
- func WithShouldRetry(fn func(resp *http.Response, err error) bool) Option
- func WithTransport(t http.RoundTripper) Option
- type RetryTransport
Constants ¶
This section is empty.
Variables ¶
var DefaultRetryableStatusCodes = []int{ http.StatusTooManyRequests, http.StatusInternalServerError, http.StatusBadGateway, http.StatusServiceUnavailable, http.StatusGatewayTimeout, }
DefaultRetryableStatusCodes are the status codes that trigger a retry by default.
Functions ¶
Types ¶
type Option ¶
type Option func(*RetryTransport)
Option is a functional option for configuring RetryTransport.
func WithBackoffMultiplier ¶
WithBackoffMultiplier sets the backoff multiplier.
func WithInitialBackoff ¶
WithInitialBackoff sets the initial backoff duration.
func WithLogger ¶
WithLogger sets the logger for error logging.
func WithMaxBackoff ¶
WithMaxBackoff sets the maximum backoff duration.
func WithMaxRetries ¶
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 ¶
WithRetryableStatusCodes sets the status codes that trigger a retry.
func WithShouldRetry ¶
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 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.