Documentation
¶
Overview ¶
Package retry provides a retry middleware implementation for httpio.
The retry pattern automatically re-attempts failed HTTP requests using exponential backoff with jitter. This helps improve reliability in the presence of transient errors, such as network timeouts or temporary server issues.
The middleware allows configuration of maximum retries, which HTTP status codes and errors are considered retryable, and the backoff strategy. Each retry waits for an exponentially increasing delay, randomized with jitter to avoid thundering herd.
Important: Only requests that match the configured retryable status codes or errors will be retried. The middleware also ensures that request bodies are properly cloned for each retry, and respects the context deadline for cancellation.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// MaxRetries is the maximum number of retries before giving up.
MaxRetries int
// RetryableStatusCodes defines which HTTP status codes should trigger a retry.
RetryableStatusCodes []int
// BaseDelay is the base delay for exponential backoff.
BaseDelay time.Duration
// MaxDelay is the maximum delay between retries.
MaxDelay time.Duration
// ErrorPredicate allows custom error checking to determine if an error should be retried.
ErrorPredicate func(err error) bool
// JitterFactor is the randomization factor for backoff delay (0 = no jitter, 0.2 = 20% jitter, etc).
JitterFactor float64
}
Config defines the configuration for the retry middleware.
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a configuration with sensible defaults.
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
RetryMiddleware implements the struct-based middleware for retrying failed requests
func New ¶
func New(config *Config) *Middleware
New creates a new retry middleware with the provided configuration.
func (*Middleware) Handle ¶
func (m *Middleware) Handle(next middleware.Handler) middleware.Handler
Handle implements the MiddlewareHandler interface