Documentation
¶
Index ¶
- func Retry(alg RetryAlgorithm, maxRetries int, retryableFn any, args ...any) ([]any, error)
- func RetryWithLogger(alg RetryAlgorithm, maxRetries int, loggerFn func(err error), retryableFn any, ...) ([]any, error)
- type AlgExp
- type AlgExpJitter
- type AlgFibonacci
- type AlgSimple
- type RetryAlgorithm
- type RetryManager
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Retry ¶
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 ¶
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 ¶
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
type AlgExpJitter ¶
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 ¶
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 ¶
AlgSimple is a simple retry algorithm that sleeps for a fixed interval.
func NewAlgSimple ¶
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
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.