retry

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2018 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package retry is a library for retrying operations and backing off between tries

Index

Examples

Constants

View Source
const (
	// DefaultAttempts is the default number of retry attempts if left unconfigured
	DefaultAttempts = 10
	// DefaultBackoffTime is the default wait time for retry attempts
	DefaultBackoffTime = 100 * time.Millisecond
)

Variables

This section is empty.

Functions

func Do

func Do(fn RetryableFunc, opts ...Option) error

Do executes the function provided and retries based on the options provided. The Default Options are:

- `Attempts(DefaultAttempts)` - `Delay(ConstantBackoff(DefaultBackoffTime))`

If all tries failed, the last erorr the function encountered is returned.

Example
var i int
err := Do(func() error {
	i++
	if i > 3 {
		fmt.Println("Success!")
		return nil
	}
	return fmt.Errorf("failed")
},
	Attempts(5),
	Delay(ConstantBackoff(100*time.Millisecond)),
	WithContext(context.Background()),
	OnRetry(func(try uint, err error) {
		fmt.Printf("onretry: %v on try %d\n", err, try)
		return
	}),
	ForErrors(func(err error) bool {
		return true // always retry
	}),
)
if err != nil {
	fmt.Println("error!")
}
Output:

onretry: failed on try 1
onretry: failed on try 2
onretry: failed on try 3
Success!

Types

type BackoffFunc

type BackoffFunc func(try uint) time.Duration

BackoffFunc Computes the delay between retries on the given current retry number It may return 0 if try == 0, but try should always be called with try > 0

func ConstantBackoff

func ConstantBackoff(delay time.Duration) BackoffFunc

ConstantBackoff always returns the same duration

func ExpontentialRandomBackoff

func ExpontentialRandomBackoff(delay time.Duration, maxExp uint) BackoffFunc

ExpontentialRandomBackoff implements Exponential Random Backoff See: https://en.wikipedia.org/wiki/Exponential_backoff

Takes a delay (w) and a maxExp (E)
Given: Try (n)
Then:  Delay (d) = w * RAND[0 to (2^MIN(n,E) - 1)]

type ErrorTestFunc

type ErrorTestFunc func(err error) bool

ErrorTestFunc is the signature of a function that returns true if the next retry should be attempted, after inspection of the error

type OnRetryFunc

type OnRetryFunc func(try uint, err error)

OnRetryFunc is a function signature that is called before every retry with the current try number (starting from 1) and the last error from the previous attempt.

type Option

type Option func(cfg *config)

Option configures retry options

func Attempts

func Attempts(attempts uint) Option

Attempts option configure the maximum number of retries beyond the initial attempt If attempts = 0, only the initial attempt will be run - this effectively disables retries

func Delay

func Delay(backoff BackoffFunc) Option

Delay configures the backoff strategy for retries

func ForErrors

func ForErrors(fn ErrorTestFunc) Option

ForErrors option configures which errors will be retried

func OnRetry

func OnRetry(fn OnRetryFunc) Option

OnRetry option configures the function which is called after every failed attempt

func WithContext

func WithContext(ctx context.Context) Option

WithContext option configures the retry context This is used for early cancellation. On each round of try, the context object is check to see if it has expired. - If the context expires before the first try, then the ctx.Err() is returned - If the context expires at any other time, then the last error is returned

type RetryableFunc

type RetryableFunc func() error

RetryableFunc is the signature of a function which can be retried by `retry.Do`

Jump to

Keyboard shortcuts

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