Documentation
¶
Overview ¶
Package backoff implements a simple exponential backoff algorithm for retrying operations.
Use Do function for retrying operations that may fail.
Simple example:
boff := backoff.New(
backoff.Exponential(),
backoff.MaxAttempts(5),
backoff.Notify(func(err error, next time.Duration, count uint) {
log.Println(count, next, err)
}),
)
err := boff.Do(ctx, func(ctx context.Context) error {
err := performAction()
if isUnrecoverable(err) {
return backoff.Permanent(err)
}
return err
})
Example using generics to return a value:
val, err := backoff.Do(ctx, boff, func(ctx context.Context) (string, error) {
// perform operation that returns (string, error)
return "result", nil
})
Index ¶
- Variables
- func Continue(err error) errordeprecated
- func Do[T any](ctx context.Context, b Backoff, fn func(ctx context.Context) (T, error)) (T, error)
- func Permanent(err error) error
- type Backoff
- type Option
- func Exponential() Option
- func Interval(duration time.Duration) Option
- func MaxAttempts(v uint) Option
- func MaxElapsedTime(duration time.Duration) Option
- func MaxInterval(duration time.Duration) Option
- func MaxRetries(v uint) Optiondeprecated
- func Multiplier(multiplier float64) Option
- func Notify(fn func(err error, duration time.Duration, count uint)) Option
- func RandomizationFactor(factor float64) Option
- type PermanentError
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMaxRetries is returned when the maximum number of retries is reached. ErrMaxRetries = errors.New("maximum retries reached") // ErrMaxElapsedTime is returned when the maximum elapsed time is reached. ErrMaxElapsedTime = errors.New("maximum elapsed time reached") )
Functions ¶
Types ¶
type Backoff ¶
Backoff manages the settings for exponential backoff retries. A Backoff instance is safe for concurrent use by multiple goroutines.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures backoff settings.
func Exponential ¶
func Exponential() Option
Exponential enables exponential backoff behavior. If not specified, the backoff interval remains constant at the initial interval. Default is false (constant backoff interval).
func Interval ¶
Interval sets the initial backoff interval. Values less than 0 will be clamped to 0. Default is 500 milliseconds.
func MaxAttempts ¶ added in v0.1.1
MaxAttempts sets the maximum number of attempts (including the initial one). If set to 0, there is no attempt limit. Default is 0 (unlimited attempts).
func MaxElapsedTime ¶
MaxElapsedTime sets the maximum total duration allowed for retries, starting from the initial attempt. If exceeded, retries halt immediately. If set to 0, there is no time limit. Values less than 0 will be clamped to 0. Default is 0 (no time limit).
func MaxInterval ¶
MaxInterval sets the maximum duration for a single backoff interval. Once reached, the interval will not grow any further. Values less than 0 will be clamped to 0. Default is 60 seconds.
func MaxRetries
deprecated
func Multiplier ¶
Multiplier sets the multiplier used to increase the backoff interval exponentially. The value must be at least 1.0. Values less than 1.0 will be clamped to 1.0. Default is 1.5.
func Notify ¶
Notify configures a notification callback triggered on each failed retry. The callback receives the error of the failed attempt, the sleep duration before the next attempt, and the attempt count (starting from 1). Default is nil (no notification).
func RandomizationFactor ¶
RandomizationFactor sets the randomization factor to add jitter to the backoff intervals. The jitter adds a random variance between -factor and +factor to each base interval duration. The value must be between 0.0 and 1.0. Values outside this range will be clamped to 0.0 or 1.0. Default is 0.5 (adding up to ±50% jitter to the interval).
type PermanentError ¶
PermanentError is an interface that errors can implement to indicate that the retry loop should stop immediately and return this error.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package backofftest provides utilities for testing code that uses the backoff package.
|
Package backofftest provides utilities for testing code that uses the backoff package. |