luci: Index | Files | Directories

package retry

import ""


Package Files

defaults.go exponential.go limited.go retry.go


const Stop time.Duration = -1

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

func Retry Uses

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).

type Callback Uses

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 Uses

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

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

type ExponentialBackoff Uses

type ExponentialBackoff struct {

    // 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 Uses

func (b *ExponentialBackoff) Next(ctx context.Context, err error) time.Duration

Next implements Iterator.

type Factory Uses

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 Uses

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 Uses

func Default() Iterator

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

func NewIterator Uses

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 Uses

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 Uses

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 Uses

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

Next implements the Iterator interface.


transientPackage transient allows you to tag and retry 'transient' errors (i.e.

Package retry imports 4 packages (graph) and is imported by 63 packages. Updated 2021-01-26. Refresh now. Tools for package owners.