retro

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2024 License: MIT Imports: 6 Imported by: 0

README

goioc/retro: Handy retry-library

goioc

Go go.dev reference CodeFactor

Quality Gate Status codecov DeepSource

ko-fi

Why DI in Go? Why IoC at all?

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BackoffStrategy

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

BackoffStrategy describes the behaviour of backing off upon errors.

func NewBackoffStrategy

func NewBackoffStrategy(generator Generator, durationUnit time.Duration) BackoffStrategy

NewBackoffStrategy is a constructor for BackoffStrategy. It accepts Generator, which is responsible for generating the backoff sequence, and time.Duration, which is a duration of one step/iteration (this duration will be multiplied by numbers from the sequence, generated by Generator).

func (BackoffStrategy) Delay

func (b BackoffStrategy) Delay() (time.Duration, error)

Delay is the main method that upon each subsequent execution, returns the next delay for this strategy.

func (BackoffStrategy) WithCappedDuration

func (b BackoffStrategy) WithCappedDuration(cappedDuration time.Duration) BackoffStrategy

WithCappedDuration adds a possibility to specify a cap for the duration, i.e. the highest value that will never be exceeded.

func (BackoffStrategy) WithJitter

func (b BackoffStrategy) WithJitter(jitter time.Duration) BackoffStrategy

WithJitter adds a possibility to add some randomized variation to the delays: +- some duration, less than `jitter` parameter.

func (BackoffStrategy) WithMaxRetries

func (b BackoffStrategy) WithMaxRetries(maxRetries int64) BackoffStrategy

WithMaxRetries adds a possibility to specify the max number of retries for this strategy.

type Caller

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

Caller is the main type of the library, responsible for calling a specified function, following the specified retry strategy.

func NewCaller

func NewCaller() Caller

NewCaller is just a constructor for Caller.

func (Caller) Call

func (c Caller) Call(ctx context.Context, f func() error) (err error)

Call is the main method that accepts a context.Context (which can be used to terminate retrying) and the function, which is essentially a wrapper around some actual function.

func (Caller) WithMaxDuration

func (c Caller) WithMaxDuration(maxDuration time.Duration) Caller

WithMaxDuration allow one to specify maximum duration (total for all retries).

func (Caller) WithRetriableError

func (c Caller) WithRetriableError(err error, strategy BackoffStrategy) Caller

WithRetriableError allows one to register an error along with the backoff strategy for it.

func (Caller) WithRetryOnAnyError

func (c Caller) WithRetryOnAnyError(strategy BackoffStrategy) Caller

WithRetryOnAnyError acts like WithRetriableError, but the specified strategy will be used upon getting _any_ error ( i.e. it makes all errors retriable).

type Generator

type Generator interface {
	// Next method returns a next number in the sequence.
	Next() int64
}

Generator interface describes an abstraction for implementing sequence generators (used in the back-off strategy).

func NewConstant

func NewConstant(c int64) Generator

NewConstant creates a Generator that always return the same number (specified as `c` parameter).

func NewExponential

func NewExponential(factor int64) Generator

NewExponential creates a Generator where every next number is the previous number multiplied by some `factor` (starts with 0).

func NewFibonacci

func NewFibonacci() Generator

NewFibonacci creates a Generator where every next number is a sum of two previous numbers (Fibonacci sequence). Starts with 1.

func NewLinear

func NewLinear(delta int64) Generator

NewLinear creates a Generator where every next number is the previous number plus some `delta` (starts with 0).

func NewRandom

func NewRandom(max int64) Generator

NewRandom creates a Generator based on math.rand, i.e. all the numbers are random (within a specified interval from 0 to `max`).

Jump to

Keyboard shortcuts

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