retry

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2022 License: Apache-2.0 Imports: 8 Imported by: 2

README

go-retry

GoDoc

go-retry is a package that helps facilitate retry logic with jittered exponential backoff. It provides a convenient interface for configuring various parameters. See below for more information.

Example

func makeNetworkCall(ctx context.Context) {
    defaultBackoff := retry.DefaultBackoff()

    // try at most 5 times
    getErr := retry.Retry(ctx, defaultBackoff, 5, func(ctx context.Context) error {
        response, err := http.Get("https://my.favorite.service")
        if err != nil {
            return err
        }
        // do something with response...
    })

    if getErr != nil {
        // get failed, even after all the retries
    }
}

Copyright Vimeo.

Documentation

Overview

Package retry implements a state struct and methods providing exponential backoff as well as a wrapper function that does exponential backoff.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Retry

func Retry(ctx context.Context, b Backoff, steps int, f func(context.Context) error) error

Retry calls the function `f` at most `steps` times using the exponential backoff parameters defined in `b`, or until the context expires.

func Typed added in v1.4.0

func Typed[T any](ctx context.Context, r *Retryable, f func(context.Context) (T, error)) (T, error)

Typed provides a wrapper around the Retryable type that handles arbitrary callback return-types in addition to an error.

Types

type Backoff

type Backoff struct {

	// If MaxBackoff == MinBackoff the backoff is constant.
	// If MinBackoff > MaxBackoff, the implementation may generate a runtime panic.
	MaxBackoff time.Duration
	MinBackoff time.Duration
	// Jitter is the maximum value that may be added or substracted based on
	// the output of a prng.
	// Jitter should be < 1, and may produce /interesting/ results if it is > 1 or < 0.
	Jitter float64
	// ExpFactor should be > 1, otherwise it will converge to 0.
	ExpFactor float64
	// contains filtered or unexported fields
}

Backoff contains the state implementing a generator returning a sequence of intervals to wait.

func DefaultBackoff

func DefaultBackoff() Backoff

DefaultBackoff returns a reasonable default backoff instance.

func (*Backoff) BackoffN

func (b *Backoff) BackoffN(n int) time.Duration

BackoffN is a stateless method that uses the parameters in the receiver to return a backoff interval appropriate for the Nth retry.

func (Backoff) Clone

func (b Backoff) Clone() Backoff

Clone returns a cloned copy of a Backoff struct.

func (*Backoff) Next

func (b *Backoff) Next() time.Duration

Next returns the next time interval to wait in the sequence.

func (*Backoff) Reset

func (b *Backoff) Reset()

Reset resets the step-count on its receiver. It is *not* thread-safe.

type CtxErrors added in v1.3.0

type CtxErrors struct {
	*Errors
	CtxErr error
}

CtxErrors bundles together Errors and a Ctx error to differentiate the errors that fail due to context expiration errors from errors that exhaust their maximum number of retries.

type Error added in v1.3.0

type Error struct {
	// When is when the error occured in the retry cycle.
	When time.Time

	// Err is the underlying error.
	Err error
}

Error is an error that occurs at a particular time.

func (*Error) Error added in v1.3.0

func (e *Error) Error() string

Error implements the error interface.

func (*Error) Unwrap added in v1.3.0

func (e *Error) Unwrap() error

Unwrap follows go-1.13-style wrapping semantics.

type Errors added in v1.3.0

type Errors struct {
	Errs []*Error
}

Errors is a collection errors that happen across multiple retries.

func (*Errors) As added in v1.3.0

func (e *Errors) As(target interface{}) bool

As will return true if any of the underlying errors matches the target and sets the argument to that error specifically. It returns false otherwise, leaving the argument unchanged. See https://golang.org/pkg/errors/#As

func (*Errors) Error added in v1.3.0

func (e *Errors) Error() string

Error implements the error interface.

func (*Errors) Is added in v1.3.0

func (e *Errors) Is(target error) bool

Is will return true if any of the underlying errors matches the target. See https://golang.org/pkg/errors/#Is

func (*Errors) Unwrap added in v1.3.0

func (e *Errors) Unwrap() error

Unwrap returns the most recent error that occured during retrying.

type Retryable

type Retryable struct {
	// Backoff parameters to use for retry
	B Backoff

	// ShouldRetry is a filter function to indicate whether to continue
	// iterating based on the error.
	// An implementation that uniformly returns true is used if nil
	ShouldRetry func(error) bool

	// Maximum retry attempts
	MaxSteps int32

	// Clock provides a clock to use when backing off (if nil, uses
	// github.com/vimeo/go-clocks.DefaultClock())
	Clock clocks.Clock
}

Retryable manages the operations of a retryable operation.

func NewRetryable

func NewRetryable(MaxSteps int32) *Retryable

NewRetryable returns a newly constructed Retryable instance

func (*Retryable) Retry

func (r *Retryable) Retry(ctx context.Context, f func(context.Context) error) error

Retry calls the function `f` at most `MaxSteps` times using the exponential backoff parameters defined in `B`, or until the context expires.

Jump to

Keyboard shortcuts

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