retrier

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2025 License: MIT Imports: 4 Imported by: 0

README

🤦‍♂️ Retrier

License Release GoDoc GoReport

Retrier is a tiny zero-dependency golang library to do some valuable things which may fail with retrying policy.

Installation

The go get is a path.

go get github.com/kukymbr/retrier

Usage

Linear retrier

Linear retrier attempts to execute function with a fixed delay.

rt := retrier.NewLinear(5, 10*time.Second)
err := rt.Do(context.Background(), someNiceButUnstableFunc())
Progressive retrier

Progressive retrier attempts to execute a function with a progressively increasing delay and a random jitter.

rt := retrier.NewProgressive(5, 10*time.Second, 1.5)
err := rt.Do(context.Background(), someNiceButUnstableFunc())
No-op retrier

Noop retrier allow to disable retrying policy using the same Retrier interface.

rt := retrier.NewNoop()
err := rt.Do(context.Background(), someFuncToRunWithoutRetries())
Custom retrier

To use a Retrier with some custom logic, use a retrier.New function:

rt := retrier.New(
	// Function, calculating the delay before next attempt.
    func(attemptN int, lastDelay time.Duration) time.Duration {
	    return 10*time.Second 	
    },
	// Function, deciding give it another try or not.
    func(attemptN int, lastDelay time.Duration) bool {
	    return lastDelay > time.Minute
    }
)
err := rt.Do(context.Background(), someNiceButUnstableFunc())
Predefined sugars

There are some predefined sugar functions to create custom retriers. For example:

rt := retrier.New(
    retrier.WithJitter(retrier.WithMaxDelay(retrier.ProgressiveDelay(10*time.Second, 1.5), 10*time.Minute)),
    retrier.LimitAttemptsCount(100),
)
err := rt.Do(context.Background(), someNiceButUnstableFunc())
Predefined CalcDelayFunc functions
  • retrier.LimitAttemptsCount adds attempt count limit.
Predefined AllowNextAttemptFunc functions
  • retrier.FixedDelay sets fixed delay before every attempt;
  • retrier.ProgressiveDelay sets a progressively increasing delay;
  • retrier.WithDelayJitter adds random jitter to the delay;
  • retrier.WithMaxDelay adds max delay limit.

License

MIT.

Documentation

Overview

Package retrier designed to execute some valuable functionality with retrying policy.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AllowNextAttemptFunc

type AllowNextAttemptFunc func(attemptN int, lastDelay time.Duration) bool

AllowNextAttemptFunc is a function to decide if next attempt is allowed.

func LimitAttemptsCount

func LimitAttemptsCount(attempts int) AllowNextAttemptFunc

LimitAttemptsCount returns as AllowNextAttemptFunc with attempts count limit.

type CalcDelayFunc

type CalcDelayFunc func(attemptN int, lastDelay time.Duration) time.Duration

CalcDelayFunc is a function to calculate a delay before next attempt.

func FixedDelay

func FixedDelay(delay time.Duration) CalcDelayFunc

FixedDelay returns a CalcDelayFunc with a fixed delay value.

func ProgressiveDelay

func ProgressiveDelay(initialDelay time.Duration, multiplier float64) CalcDelayFunc

ProgressiveDelay returns a CalcDelayFunc with a progressively increasing delay.

func WithDelayJitter

func WithDelayJitter(calcDelayFn CalcDelayFunc) CalcDelayFunc

WithDelayJitter adds random jitter to the delay.

func WithMaxDelay

func WithMaxDelay(calcDelayFn CalcDelayFunc, maxDelay time.Duration) CalcDelayFunc

WithMaxDelay adds max delay check to the existing CalcDelayFunc.

type Fn

type Fn func() error

Fn is a function to call.

type Retrier

type Retrier interface {
	// Do execute a Fn and retry it in case of error.
	Do(ctx context.Context, fn Fn) error
}

Retrier is a tool to execute Fn with retries.

func New

func New(calcDelayFn CalcDelayFunc, allowNextAttemptFn AllowNextAttemptFunc) Retrier

New returns custom Retrier.

func NewLinear

func NewLinear(attempts uint, delay time.Duration) Retrier

NewLinear returns a Retrier, attempting to execute a Fn with a fixed delay.

func NewNoop

func NewNoop() Retrier

NewNoop returns a Retrier without retries.

func NewProgressive

func NewProgressive(attempts uint, initialDelay time.Duration, multiplier float64) Retrier

NewProgressive returns a Retrier, attempting to execute a Fn with a progressively increasing delay and a random jitter.

Jump to

Keyboard shortcuts

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