Documentation
¶
Overview ¶
Package retry provides configurable retry strategies and utilities.
Index ¶
- Variables
- func Abort(err error) error
- func Do(ctx context.Context, backoff Backoff, fn func(ctx context.Context) error) error
- func DoValue[T any](ctx context.Context, backoff Backoff, fn func(ctx context.Context) (T, error)) (T, error)
- func OnComplete(fn func(error)) doOptFunc
- func WithCleanup(fn func()) doOptFunc
- type Backoff
- type DispatchFunc
- type Dispatcher
- type DoOpt
- type Wrapper
Constants ¶
This section is empty.
Variables ¶
var ErrCanceled = errors.New("cancelled")
ErrCanceled is passed to a cleanup function when an operation is canceled.
var ErrDispatcherClosed = errors.New("dispatcher is closed")
ErrDispatcherClosed is returned when attempting to dispatch a task through a closed Dispatcher.
Functions ¶
func Do ¶ added in v0.3.1
Do executes a function with retries according to the provided backoff strategy. Retries the function on failure until it succeeds, the context is cancelled, the backoff strategy indicates stopping or a non-retryable error is returned by the function. Returns the last error if all retries fail, or nil on success.
func DoValue ¶ added in v0.8.0
func DoValue[T any](ctx context.Context, backoff Backoff, fn func(ctx context.Context) (T, error)) (T, error)
DoValue executes a function with retries according to the provided backoff strategy. Retries the function on failure until it succeeds, the context is cancelled, the backoff strategy indicates stopping or a non-retryable error is returned by the function. Returns the last error if all retries fail, or the value returned by fn and nil error on success.
func OnComplete ¶ added in v0.7.3
func OnComplete(fn func(error)) doOptFunc
OnComplete attaches a callback function fn to an operation. The callback is invoked when the operation completes and receives the final error:
- nil on success;
- the last error on failure;
- ErrCanceled if the operation has been canceled.
func WithCleanup ¶ added in v0.7.1
func WithCleanup(fn func()) doOptFunc
WithCleanup attaches cleanup function fn to an operation. The cleanup function is called when the operation completes.
Types ¶
type Backoff ¶
type Backoff interface {
// Next returns the next wait duration and whether to retry.
Next() (delay time.Duration, ok bool)
// Attempt returns the number of retries attempted so far.
Attempt() int
// Reset restarts the backoff counter to the initial state.
Reset()
}
Backoff implements a retry strategy with increasing delays.
var NoBackoff Backoff = &noRetry{}
NoBackoff is a Backoff that immediately stops retrying. Useful for disabling retries or as a terminal condition.
func Exponential ¶
Exponential creates a backoff with doubling delays.
type DispatchFunc ¶ added in v0.5.0
DispatchFunc is a function Dispatcher uses to dispatch tasks.
type Dispatcher ¶ added in v0.3.0
type Dispatcher struct {
// contains filtered or unexported fields
}
Dispatcher creates and dispatches tasks with retries according to backoff strategies.
func NewDispatcher ¶ added in v0.3.0
func NewDispatcher(fn DispatchFunc) *Dispatcher
NewDispatcher creates a new dispatcher. Dispatcher will dispatch tasks for execution using the provided function.
func (*Dispatcher) Close ¶ added in v0.3.0
func (d *Dispatcher) Close()
Close closes the dispatcher and stops all retries.
type DoOpt ¶ added in v0.7.1
type DoOpt interface {
// contains filtered or unexported methods
}
DoOpt represents an option that can be passed to Do methods.
type Wrapper ¶ added in v0.2.0
Wrapper is a function that decorates a Backoff with additional behavior.
func Jitter ¶
Jitter randomizes delays from another backoff by ±j. The delay can never be less than 0.
func MaxDuration ¶
MaxDuration limits the cumulative delay time from a backoff. It's best-effort, and should not be used to guarantee an exact amount of time.
func MaxRetries ¶
MaxRetries caps the number of retries for another backoff.