Documentation
¶
Overview ¶
Package retry provides exponential backoff with jitter for retrying operations that may fail transiently.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CalculateDelay ¶
func CalculateDelay(attempt int, cfg RetryConfig) time.Duration
CalculateDelay computes the backoff delay for the given attempt number. The delay is min(baseDelay * 2^attempt, maxDelay), with optional equal jitter that guarantees at least 50% of the calculated delay.
func Do ¶
func Do(ctx context.Context, cfg RetryConfig, fn func() error) error
Do executes fn with retry logic according to cfg. It returns nil on success, or the last error if all retries are exhausted. It respects context cancellation between retries.
func IsRetryable ¶
IsRetryable returns true if the given HTTP status code indicates a retryable error: 408, 429, 500, 502, 503, 504.
Types ¶
type RetryConfig ¶
type RetryConfig struct {
MaxRetries int
BaseDelay time.Duration
MaxDelay time.Duration
Jitter bool
}
RetryConfig holds configuration for retry behavior.
func DefaultRetryConfig ¶
func DefaultRetryConfig() RetryConfig
DefaultRetryConfig returns a RetryConfig with sensible defaults: 3 retries, 1s base delay, 30s max delay, jitter enabled.
type RetryableError ¶
RetryableError represents an error that may be retried, optionally including an HTTP status code and a Retry-After duration.
func (*RetryableError) Error ¶
func (e *RetryableError) Error() string
func (*RetryableError) Unwrap ¶
func (e *RetryableError) Unwrap() error