retry

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2019 License: MIT Imports: 3 Imported by: 2

Documentation

Overview

Package retry provides util functions to retry fail actions.

Index

Constants

This section is empty.

Variables

View Source
var ErrNeedRetry = errors.New("need retry")

ErrNeedRetry is a placholder helper, in case you have no error to return, such as bool status, etc.

Functions

func ConstantBackoffs

func ConstantBackoffs(n int, backoff time.Duration) []time.Duration

ConstantBackoffs creates a list of backoffs with constant values.

func ExponentialBackoffs

func ExponentialBackoffs(n int, backoff time.Duration) []time.Duration

ExponentialBackoffs creates a list of backoffs with values are calculated by backoff*2^[0 1 2 .. n).

func Retry

func Retry(backoffs []time.Duration, try func() (State, error)) error

Retry is a shortcut for Retrier.Run with context.Background().

func ZeroBackoffs

func ZeroBackoffs(n int) []time.Duration

ZeroBackoffs creates a list of backoffs with zero values.

Types

type Retrier

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

Retrier retrys fail actions with backoff.

func New

func New(backoffs []time.Duration) Retrier

New creates a new Retrier with backoffs, the backoffs is the wait time before each retrying. The count of retrying will be len(backoffs), the first call is not counted in retrying.

func (Retrier) Run

func (r Retrier) Run(ctx context.Context, try func() (State, error)) (err error)

Run keeps calling the RetryFunc if it returns (Continue, non-nil-err), otherwise it will stop retrying. It is goroutine safe unless you do something wrong ^_^.

type State

type State uint8

State controls whether the fail action should continue retrying.

const (
	// Continue continues retrying the fail action.
	Continue State = iota
	// StopWithErr stops retrying the fail action,
	// returns the error which the RetryFunc returns.
	StopWithErr
	// StopWithNil stops retrying the fail action, returns nil.
	StopWithNil
)

Jump to

Keyboard shortcuts

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