retrier

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2023 License: MIT Imports: 4 Imported by: 4

README

Retrier

Build Coverage Go Report Card Go Reference

Retrier is a small package that makes retrying anything easier with custom or predefined retry functions.

Usage

Create a retrier by providing an upper limit to retries and a delay function.

ret := retrier.NewRetrier(
    10, // -1 for no limit
    retrier.ConstantDelay(time.Second),
)

You can use one of the predefined delay functions, or provide your own.

ret := retrier.NewRetrier(
    -1,
    func(count int) time.Duration {
        m := rand.Intn(60)
        return time.Second * time.Duration(m)
    },
)

Use the Run or RunCtx functions to run any task. The retrier will retry the task if it returns true ("should retry"). The retrier will not retry the task if it returns false ("should not retry"), if the retry cap is reached or if the context is canceled.

ret.RunCtx(
    context.TODO(),
    func(ctx context.Context) (error, bool) {
        resp, err := http.Get("https://some-api.com")
        if err != nil {
            fmt.Println("request failed")
            return err, true
        } else {
            fmt.Println(resp)
            return nil, false
        }
    },
)

Delay Functions

Function Delay Example
No Delay 0 0, 0, 0, 0, 0
Constant Delay c 1, 1, 1, 1, 1
Linear Delay r*c 1, 2, 3, 4, 5
Capped Linear Delay min(r*c, cap) 1, 2, 3, 3, 3
Exponential Delay a*b^r 2, 4, 8, 16, 32
Capped Exponential Delay min(a*b^r, cap) 2, 4, 8, 10, 10

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CappedExponentialDelay

func CappedExponentialDelay(
	coef time.Duration,
	base int,
	cap time.Duration,
) func(int) time.Duration

CappedExponentialDelay returns a delay function that creates an exponentially increasing wait duration between retries up to a specific limit where delay can not be longer.. The delay is calculated by (coef*base^retries).

func CappedLinearDelay

func CappedLinearDelay(
	step time.Duration,
	cap time.Duration,
) func(int) time.Duration

CappedLinearDelay returns a delay function that creates a linearly increasing wait duration between retries up to a specific limit where delay can not be longer. The delay is calculated by min((delay*retries), cap)

func ConstantDelay

func ConstantDelay(
	delay time.Duration,
) func(int) time.Duration

ConstantDelay returns a delay function that creates a constant wait duration between retries. The delay will be the same between the retries.

func ExponentialDelay

func ExponentialDelay(
	coef time.Duration,
	base int,
) func(int) time.Duration

ExponentialDelay returns a delay function that creates an exponentially increasing wait duration between retries. The delay is calculated by (coef*base^retries).

func LinearDelay

func LinearDelay(
	step time.Duration,
) func(int) time.Duration

LinearDelay returns a delay function that creates a linearly increasing wait duration between retries. The delay is calculated by (step*retries).

func NoDelay

func NoDelay() func(int) time.Duration

NoDelay returns a delay function that has no delay between retries.

Types

type Retrier

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

Retrier controls how to to run the retry function. A task will be retried up to a set retry count with some delay between the retries defined by a delay function.

func NewRetrier

func NewRetrier(
	max int,
	delayf func(int) time.Duration,
) *Retrier

NewRetrier creates a retrier from max retries and a delay function.

func (*Retrier) Run

func (r *Retrier) Run(work func() (error, bool)) error

Run executes a work task with the background context.

func (*Retrier) RunCtx

func (r *Retrier) RunCtx(
	ctx context.Context,
	work func(ctx context.Context) (error, bool),
) error

RunCtx executes a work task in the context of a retrier until the task decides not to retry, or if the maximum retries have been reached, or if the context has been canceled and retrying should stop.

Jump to

Keyboard shortcuts

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