retry

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2024 License: MIT Imports: 6 Imported by: 4

README

Retry

Go Reference

Retry is a simple generic function retryer. It comes with 4 predefined retry timing algorithms and the flexibility to add and define your own.

Retry Comes In 2 Flavors

  1. A simple function that takes a RetryAlgorithm, max number of retries, the function name that you want to perform retries on, and its arguments.
  2. A RetryManager which holds your retry config and is safe for concurrent use. The RetryManager has a Retry function on it which is a wrapper for the retry function in number 1. This wrapper uses the config in the RetryManager. The Retry function on the RetryManager only requires the function name that is to be called and its list of arguments.

Both versions return a slice of any, and an error. The function will retry your function up to the max retry value specified. If on the last try, your function returns an error, Retry will return an empty slice of any and the error from the function. Both versions also come in a WithLogger version. The WithLogger versions take a logger function that takes an error as an argument.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Retry

func Retry(alg RetryAlgorithm, maxRetries int, retryableFn any, args ...any) ([]any, error)

Retry is a function that retries a function call using the provided retry algorithm. It takes the retry algorithm, the maximum number of retries, the function to call, and the arguments to pass to the function. It returns the results of the function call and an error if the function call fails. Retry always returns the results of the function call as a slice of any, even if the function returns a single value. Retry returns the exact number of results that the function retryableFn returns unless the function returns an error. In which case Retry will retry the function call using the provided retry algorithm until the maximum number of retries is reached. If the function call fails after the maximum number of retries, Retry will an empty slice of any and the error that caused the function call to fail.

func RetryWithLogger

func RetryWithLogger(alg RetryAlgorithm, maxRetries int, loggerFn func(err error), retryableFn any, args ...any) ([]any, error)

RetryWithLogger is a function that retries a function call using the provided retry algorithm. It takes the retry algorithm, the maximum number of retries, a logging function that takes an error as an argument, the function to call, and the arguments to pass to the function. It returns the results of the function call and an error if the function call fails. RetryWithLogger always returns the results of the function call as a slice of any, even if the function returns a single value. RetryWithLogger returns the exact number of results that the function retryableFn returns unless the function returns an error. In which case RetryWithLogger will retry the function call using the provided retry algorithm until the maximum number of retries is reached. If the function call fails after the maximum number of retries, RetryWithLogger will an empty slice of any and the error that caused the function call to fail.

Types

type AlgExp

type AlgExp struct {
	Base         int
	RetryCount   int
	TimeDuration time.Duration
}

AlgExp is a retry algorithm that sleeps for an exponential interval. The exponential interval is calculated by multiplying the base by 2 to the power of the retry count.

func NewAlgExp

func NewAlgExp(base int, duration time.Duration) *AlgExp

AlgExp is a retry algorithm that sleeps for an exponential interval. NewAlgExp creates a new AlgExp with the given base and time duration.

func NewAlgExpDefault

func NewAlgExpDefault() *AlgExp

AlgExp is a retry algorithm that sleeps for an exponential interval. NewAlgExpDefault creates a new AlgExp with a default base of 1000 and a default time duration of time.Millisecond.

func (*AlgExp) Clone

func (a *AlgExp) Clone() RetryAlgorithm

func (*AlgExp) Reset

func (a *AlgExp) Reset()

func (*AlgExp) SleepFunc

func (a *AlgExp) SleepFunc()

type AlgExpJitter

type AlgExpJitter struct {
	Base         int
	RetryCount   int
	TimeDuration time.Duration
}

AlgExpJitter is a retry algorithm that sleeps for an exponential interval with jitter. The exponential interval is calculated by multiplying the base by 2 to the power of the retry count multiplied by a random float64 + 0.5.

func NewAlgExpJitter

func NewAlgExpJitter(base int, duration time.Duration) *AlgExpJitter

AlgExpJitter is a retry algorithm that sleeps for an exponential interval with jitter. NewAlgExpJitter creates a new AlgExpJitter with the given base and time duration.

func NewAlgExpJitterDefault

func NewAlgExpJitterDefault() *AlgExpJitter

AlgExpJitter is a retry algorithm that sleeps for an exponential interval with jitter. NewAlgExpJitterDefault creates a new AlgExpJitter with a default base of 1000 and a default time duration of time.Millisecond.

func (*AlgExpJitter) Clone

func (a *AlgExpJitter) Clone() RetryAlgorithm

func (*AlgExpJitter) Reset

func (a *AlgExpJitter) Reset()

func (*AlgExpJitter) SleepFunc

func (a *AlgExpJitter) SleepFunc()

type AlgFibonacci

type AlgFibonacci struct {
	Start1       int
	Start2       int
	Val1         int
	Val2         int
	TimeDuration time.Duration
}

AlgFibonacci is a retry algorithm that sleeps for a fibonacci interval. The fibonacci interval is calculated by adding the previous two values in the sequence.

func NewAlgFibonacci

func NewAlgFibonacci(start1 int, start2 int, duration time.Duration) *AlgFibonacci

AlgFibonacci is a retry algorithm that sleeps for a fibonacci interval. NewAlgFibonacci creates a new AlgFibonacci with the given start1, start2, and time duration.

func NewAlgFibonacciDefault

func NewAlgFibonacciDefault() *AlgFibonacci

AlgFibonacci is a retry algorithm that sleeps for a fibonacci interval. NewAlgFibonacciDefault creates a new AlgFibonacci with a default start1 of 0, start2 of 1, and a default time duration of time.Millisecond.

func (*AlgFibonacci) Clone

func (a *AlgFibonacci) Clone() RetryAlgorithm

func (*AlgFibonacci) Reset

func (a *AlgFibonacci) Reset()

func (*AlgFibonacci) SleepFunc

func (a *AlgFibonacci) SleepFunc()

type AlgSimple

type AlgSimple struct {
	Interval     int
	TimeDuration time.Duration
}

AlgSimple is a simple retry algorithm that sleeps for a fixed interval.

func NewAlgSimple

func NewAlgSimple(interval int, duration time.Duration) *AlgSimple

AlgSimple is a simple retry algorithm that sleeps for a fixed interval. NewAlgSimple creates a new AlgSimple with the given interval and time duration.

func NewAlgSimpleDefault

func NewAlgSimpleDefault() *AlgSimple

AlgSimple is a simple retry algorithm that sleeps for a fixed interval. NewAlgSimpleDefault creates a new AlgSimple with a default interval of 1000 and a default time duration of time.Millisecond.

func (*AlgSimple) Clone

func (a *AlgSimple) Clone() RetryAlgorithm

func (*AlgSimple) Reset

func (a *AlgSimple) Reset()

func (*AlgSimple) SleepFunc

func (a *AlgSimple) SleepFunc()

type RetryAlgorithm

type RetryAlgorithm interface {
	SleepFunc()
	Reset()
	Clone() RetryAlgorithm
}

RetryAlgorithm is an interface that defines the methods that a retry algorithm must implement. SleepFunc is a method that will be called when the function should sleep before retrying. Reset is a method that will be called when the function should reset the algorithm. Clone is a method that will be called when the function should clone the algorithm.

type RetryManager

type RetryManager struct {
	Algorithm  RetryAlgorithm
	MaxRetries int
}

RetryManager is a manager for retrying functions with a given algorithm and max retries.

func NewRetryManager

func NewRetryManager(algorithm RetryAlgorithm, maxRetries int) *RetryManager

NewRetryManager creates a new RetryManager with the given algorithm and max retries. RetryManager is safe for concurrent use as the Retry function on the RetryManager will clone the algorithm before using it. Ensuring that the algorithm is not modified by multiple goroutines.

func (*RetryManager) Retry

func (r *RetryManager) Retry(fn any, args ...any) ([]any, error)

Retry is a function that retries a function call using the provided retry algorithm. It takes the function to call and the arguments to pass to the function. It returns the results of the function call and an error if the function call fails. Retry always returns the results of the function call as a slice of any, even if the function returns a single value. Retry returns the exact number of results that the function retryableFn returns unless the function returns an error. In which case Retry will retry the function call using the provided retry algorithm until the maximum number of retries is reached. If the function call fails after the maximum number of retries, Retry will an empty slice of any and the error that caused the function call to fail.

func (*RetryManager) RetryWithLogger

func (r *RetryManager) RetryWithLogger(loggerFn func(err error), fn any, args ...any) ([]any, error)

RetryWithLogger is a function that retries a function call using the provided retry algorithm. It takes a logging function that takes an error as an argument, the function to call, and the arguments to pass to the function. It returns the results of the function call and an error if the function call fails. RetryWithLogger always returns the results of the function call as a slice of any, even if the function returns a single value. RetryWithLogger returns the exact number of results that the function retryableFn returns unless the function returns an error. In which case RetryWithLogger will retry the function call using the provided retry algorithm until the maximum number of retries is reached. If the function call fails after the maximum number of retries, RetryWithLogger will an empty slice of any and the error that caused the function call to fail.

Jump to

Keyboard shortcuts

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