Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Do ¶
func Do(ctx context.Context, maxAttempts int, f AttemptFunc) error
Do executes the provided function with retry semantics.
Parameters:
- ctx controls cancellation and timeouts.
- maxAttempts defines the maximum number of attempts. A value of 0 means retry indefinitely until the context is canceled.
- f is the function to execute; it receives the zero-based attempt number.
This is a convenience wrapper around New(...) with default configuration and a custom maxAttempts value.
func IsUnretryable ¶
IsUnretryable reports whether the error is marked as unretryable.
Types ¶
type AttemptFunc ¶
AttemptFunc represents a single retryable operation. The argument is the zero-based attempt number. Returning nil indicates success; a non-nil error triggers retry logic.
type Backoff ¶
type Backoff interface {
// Next returns the duration to wait before the next retry attempt.
// The attempt parameter is zero-based (first retry = attempt 0).
Next(attempt int) time.Duration
}
Backoff defines a strategy for calculating delay durations between retry attempts.
type ExponentialBackoff ¶
type ExponentialBackoff struct {
Base time.Duration
Factor float64
Max time.Duration
Jitter float64
}
ExponentialBackoff increases the delay exponentially with each attempt.
Base is the initial delay. Factor is the exponential multiplier (e.g. 2.0). Max caps the maximum delay (0 means no limit). Jitter adds a random variation as a fraction of the computed delay.
type FixedBackoff ¶
FixedBackoff implements a constant delay between attempts.
Interval defines the base delay duration. Jitter adds a random variation in the range [-Jitter, +Jitter] as a fraction of Interval (e.g. 0.2 = ±20%).
type IsRetryableFunc ¶
IsRetryableFunc determines whether an error is retryable. Returning false stops retries immediately.
type LinearBackoff ¶
type LinearBackoff struct {
Base time.Duration
Step time.Duration
Max time.Duration
Jitter float64
}
LinearBackoff increases the delay linearly with each attempt.
Base is the initial delay. Step is added for each subsequent attempt. Max caps the maximum delay (0 means no limit). Jitter adds a random variation as a fraction of the computed delay.
type Retrier ¶
type Retrier interface {
// Do executes the provided AttemptFunc until it succeeds,
// the context is canceled, or retry limits are exceeded.
Do(context.Context, AttemptFunc) error
}
Retrier executes an operation with retry semantics.
func New ¶
func New(opts ...RetryOption) Retrier
New creates a new Retrier with optional configuration. By default, it uses:
- a linear backoff
- a maximum of 3 attempts
- a retryable check that retries on any non-nil error
type RetryOption ¶
type RetryOption func(*retrier)
RetryOption configures a Retrier.
func WithBackoff ¶
func WithBackoff(backoff Backoff) RetryOption
WithBackoff sets a custom backoff strategy.
func WithIsRetryableFunc ¶
func WithIsRetryableFunc(isRetryable IsRetryableFunc) RetryOption
WithIsRetryableFunc sets a custom function to determine whether an error should be retried.
func WithMaxAttempts ¶
func WithMaxAttempts(maxAttempts int) RetryOption
WithMaxAttempts sets the maximum number of retry attempts. A value of 0 means unlimited retries.
type UnretryableError ¶
type UnretryableError struct {
// contains filtered or unexported fields
}
UnretryableError marks an error as non-retryable.
When this error is returned (or wrapped), the retry mechanism should stop immediately and propagate the error to the caller. The original cause is preserved and can be accessed via errors.Unwrap or errors.As.
func (*UnretryableError) Error ¶
func (e *UnretryableError) Error() string
func (*UnretryableError) Unwrap ¶
func (e *UnretryableError) Unwrap() error