retry

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: AGPL-3.0 Imports: 4 Imported by: 0

README

Util package for retrying, functions and tools for situations where we are retrying/polling for a change.

Based around a 'RetryStrategy' interface that allows different approaches such as:

  • retry X times
  • retry for X seconds
  • retry for 2 minutes but backing off

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(fn func() error, retryStrat Strategy) error

Types

type FailFastError

type FailFastError struct {
	// contains filtered or unexported fields
}

func FailFast

func FailFast(err error) *FailFastError

FailFast allows code to break out of the retry if they encounter a situation they would prefer to fail fast. - `retry.Do` will not retry if the error is of type `FailFastError`, instead it will immediately return the wrapped error.

func (*FailFastError) Error

func (f *FailFastError) Error() string

func (*FailFastError) Unwrap

func (f *FailFastError) Unwrap() error

Unwrap function means FailFastError will still work with errors.Is and errors.As for the wrapped error

type Strategy

type Strategy interface {
	// NextRetryInterval calls can be considered as marking the completion of an attempt
	NextRetryInterval() time.Duration // returns the duration to sleep before making the next attempt (may not be fixed, e.g. if strategy is to back-off)
	Done() bool                       // returns true when caller should stop retrying
	Summary() string                  // message to summarise usage (i.e. number of retries, time take, if it failed and why, e.g. "timed out after 120 seconds (8 attempts)"
	Reset()                           // reset is called before the first attempt is made, can be used for recording start time or setting attempts to zero
}

Strategy interface allows for flexible strategies for retrying/polling functions.

func NewBackoffAndRetryForeverStrategy

func NewBackoffAndRetryForeverStrategy(backoffIntervals []time.Duration, retryInterval time.Duration) Strategy

NewBackoffAndRetryForeverStrategy will keep retrying until there is a success. For the first retries it will wait the durations specified by the `backoffIntervals` slice, after which it will use the `retryInterval` indefinitely Note: caller can still use retry.FailFast(err) to wrap an error if it wants to break out of the retry

func NewDoublingBackoffStrategy

func NewDoublingBackoffStrategy(initialInterval time.Duration, maxRetries uint64) Strategy

NewDoublingBackoffStrategy keeps retrying until successful or until it reaches maxRetries, doubling the initialInterval wait period after each additional retry to avoid exacerbating problems with failing traffic

func NewTimeoutStrategy

func NewTimeoutStrategy(timeout time.Duration, interval time.Duration) Strategy

NewTimeoutStrategy retries at the provided (fixed) interval until the timeout duration has elapsed

Jump to

Keyboard shortcuts

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