retry

package
v0.0.0-...-add0f8d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2024 License: Apache-2.0 Imports: 4 Imported by: 109

Documentation

Index

Constants

View Source
const Stop time.Duration = -1

Stop is a sentinel value returned by a Iterator to indicate that no more attempts should be made.

Variables

This section is empty.

Functions

func Retry

func Retry(ctx context.Context, f Factory, fn func() error, callback Callback) (err error)

Retry executes a function 'fn'. If the function returns an error, it will be re-executed according to a retry plan.

If a Factory is supplied, it will be called to generate a single retry Iterator for this Retry round. If nil, Retry will execute the target function exactly once regardless of return value.

If the supplied context is canceled, retry will stop executing. Retry will not execute the supplied function at all if the context is canceled when Retry is invoked.

If 'callback' is not nil, it will be invoked if an error occurs (prior to sleeping).

Types

type Callback

type Callback func(error, time.Duration)

Callback is a callback function that Retry will invoke every time an attempt fails prior to sleeping.

func LogCallback

func LogCallback(ctx context.Context, opname string) Callback

LogCallback builds a Callback which logs a Warning with the opname, error and delay.

type ExponentialBackoff

type ExponentialBackoff struct {
	Limited

	// Multiplier is the exponential growth multiplier. If < 1, a default of 2
	// will be used.
	Multiplier float64
	// MaxDelay is the maximum duration. If <= zero, no maximum will be enforced.
	MaxDelay time.Duration
}

ExponentialBackoff is an Iterator implementation that implements exponential backoff retry.

func (*ExponentialBackoff) Next

Next implements Iterator.

type Factory

type Factory func() Iterator

Factory is a function that produces an independent Iterator instance.

Since each Iterator is mutated as it is iterated through, this is used to produce a fresh Iterator for a new round of retries. Unless the caller is fully aware of what they're doing, this should not return the an Iterator instance more than once.

type Iterator

type Iterator interface {
	// Returns the next retry delay, or Stop if no more retries should be made.
	Next(context.Context, error) time.Duration
}

Iterator describes a stateful implementation of retry logic.

func Default

func Default() Iterator

Default is a Factory that returns a new instance of the default iterator configuration.

func NewIterator

func NewIterator(next func(context.Context, error) time.Duration) Iterator

NewIterator creates an Iterator based on a "next" function. It is a concise way to implement an Iterator.

func None

func None() Iterator

None is a Factory that returns an Iterator that explicitly calls Stop after the first try. This is helpful to pass to libraries which use retry.Default if given nil, but where you don't want any retries at all (e.g. tests).

type Limited

type Limited struct {
	// Delay is the next generated delay.
	Delay time.Duration

	// Retries, if >= 0, is the number of remaining retries. If <0, no retry
	// count will be applied.
	Retries int

	// MaxTotal is the maximum total elapsed time. If <= 0, no maximum will be
	// enforced.
	MaxTotal time.Duration
	// contains filtered or unexported fields
}

Limited is an Iterator implementation that may be limited by a maximum number of retries and/or time.

func (*Limited) Next

func (i *Limited) Next(ctx context.Context, _ error) time.Duration

Next implements the Iterator interface.

Directories

Path Synopsis
Package transient allows you to tag and retry 'transient' errors (i.e.
Package transient allows you to tag and retry 'transient' errors (i.e.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL