Documentation
¶
Overview ¶
Package retry provides configurable retry strategies and utilities.
Index ¶
Constants ¶
This section is empty.
Variables ¶
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.
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 Dispatcher ¶ added in v0.3.1
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.1
func NewDispatcher(tasks chan<- func()) *Dispatcher
NewDispatcher creates a new dispatcher. Dispatcher will send tasks for execution to the specified channel.
func (*Dispatcher) Close ¶ added in v0.3.1
func (d *Dispatcher) Close()
Close closes the dispatcher and stops all retries.
func (*Dispatcher) Do ¶ added in v0.3.1
Do creates and dispatches a task. If fn returns a retryable error, the task is dispatched again according to backoff.
func (*Dispatcher) Drain ¶ added in v0.3.1
func (d *Dispatcher) Drain()
Drain closes the dispatcher and waits for retries to complete at most once.
type Wrapper ¶
Wrapper is a function that decorates a Backoff with additional behavior.
func Chain ¶
Chain composes multiple wrappers into a single wrapper. Wrappers are applied in reverse order: first wrapper is outermost.
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.