Documentation
¶
Overview ¶
Package retry provides exponential backoff retry logic for transient failures.
Index ¶
- Variables
- func DefaultIsRetryable(err error) bool
- func Do(ctx context.Context, cfg Config, fn RetryableFunc) error
- func DoWithResult[T any](ctx context.Context, cfg Config, fn func(ctx context.Context) (T, error)) (T, error)
- func MarkNotRetryable(err error) error
- func MarkRetryable(err error) error
- type Config
- type RetryError
- type RetryableFunc
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotRetryable wraps non-retryable errors to stop retry attempts. ErrNotRetryable = errors.New("retry: error is not retryable") // ErrMaxRetries is returned when all retry attempts are exhausted. ErrMaxRetries = errors.New("retry: max retries exceeded") // ErrContextCanceled wraps context cancellation errors. ErrContextCanceled = errors.New("retry: context canceled") )
Sentinel errors.
Functions ¶
func DefaultIsRetryable ¶
DefaultIsRetryable returns true for errors that are typically transient. Override with Config.IsRetryable for custom behavior.
func Do ¶
func Do(ctx context.Context, cfg Config, fn RetryableFunc) error
Do executes fn with retries according to cfg. Returns the last error if all retries fail.
func DoWithResult ¶
func DoWithResult[T any](ctx context.Context, cfg Config, fn func(ctx context.Context) (T, error)) (T, error)
DoWithResult executes fn with retries and returns a result value.
func MarkNotRetryable ¶
MarkNotRetryable wraps an error to indicate it should not be retried.
func MarkRetryable ¶
MarkRetryable wraps an error to explicitly indicate it can be retried.
Types ¶
type Config ¶
type Config struct {
// MaxRetries is the maximum number of retry attempts (default: 3).
// Set to 0 for no retries (execute once).
MaxRetries int
// InitialBackoff is the delay before the first retry (default: 100ms).
InitialBackoff time.Duration
// MaxBackoff caps the backoff duration (default: 30s).
MaxBackoff time.Duration
// Multiplier increases backoff after each retry (default: 2.0).
Multiplier float64
// Jitter adds randomness to prevent thundering herd (default: 0.1 = 10%).
// Value between 0 and 1 where 0 means no jitter and 1 means +/- 100%.
Jitter float64
// IsRetryable determines if an error should be retried.
// If nil, defaults to DefaultIsRetryable.
IsRetryable func(error) bool
}
Config configures retry behavior.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config with sensible defaults.
type RetryError ¶
type RetryError struct {
// Cause is the last error returned by the function.
Cause error
// Attempts is the number of attempts made.
Attempts int
// Err is the sentinel error (ErrMaxRetries, ErrNotRetryable, or ErrContextCanceled).
Err error
}
RetryError provides details about a failed retry operation.
func (*RetryError) Error ¶
func (e *RetryError) Error() string
func (*RetryError) Is ¶
func (e *RetryError) Is(target error) bool
func (*RetryError) Unwrap ¶
func (e *RetryError) Unwrap() error
type RetryableFunc ¶
RetryableFunc is the function type that can be retried.