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 ¶
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 ¶
IsRetryableError reports whether an error is transient and worth retrying (timeouts, connection resets, DNS failures).
func IsRetryableStatus ¶
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 ¶
WithBaseDelay sets the initial backoff delay.
func WithMaxRetries ¶
WithMaxRetries sets the maximum number of retries.