retry

package
v1.20210720.3 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package retry implements a generic retry provider.

Basic Example

You can use the retry provider to wrap a potentially flaky call:

res, err := http.Get("https://google.com")
...
res, err := retry.Retry(ctx, func(_ context.Context, args interface{}) (interface{}, error) {
	return http.Get(args.(string))
}, "https://google.com") // note: the return type here is `(interface{}, error)`

You can also add additional parameters to the retry:

res, err := retry.Retry(ctx, func(_ context.Context, args interface{}) (interface{}, error) {
	return http.Get(args.(string))
}, "https://google.com", retry.OptMaxAttempts(10), retry.OptExponentialBackoff(time.Second))

Index

Constants

View Source
const (
	DefaultMaxAttempts = 5
	DefaultRetryDelay  = time.Second
)

Defaults

Variables

This section is empty.

Functions

func Defaults added in v1.20210615.7

func Defaults(r *Retrier)

Defaults applies defaults to a given options.

func Retry

func Retry(ctx context.Context, action Actioner, args interface{}, opts ...Option) (interface{}, error)

Retry calls an actioner with retries.

Types

type Actioner added in v1.20210615.7

type Actioner = async.Actioner

Actioner is an alias to async.Actioner.

type ActionerFunc added in v1.20210615.7

type ActionerFunc = async.ActionerFunc

ActionerFunc is an alias to async.ActionerFunc.

type DelayProvider

type DelayProvider func(context.Context, uint) time.Duration

DelayProvider is a provider for retry delays.

func ConstantDelay

func ConstantDelay(d time.Duration) DelayProvider

ConstantDelay returns a constant delay provider.

func ExponentialBackoff

func ExponentialBackoff(d time.Duration) DelayProvider

ExponentialBackoff is a backoff provider that doubles the base delay each attempt.

type Option

type Option func(*Retrier)

Option mutates retry options.

func OptConstantDelay

func OptConstantDelay(d time.Duration) Option

OptConstantDelay sets the retry delay provider.

func OptDelayProvider

func OptDelayProvider(delayProvider DelayProvider) Option

OptDelayProvider sets the retry delay provider.

func OptExponentialBackoff

func OptExponentialBackoff(d time.Duration) Option

OptExponentialBackoff sets the retry delay provider.

func OptMaxAttempts

func OptMaxAttempts(maxAttempts uint) Option

OptMaxAttempts sets the max attempts.

func OptShouldRetryProvider

func OptShouldRetryProvider(provider ShouldRetryProvider) Option

OptShouldRetryProvider sets the should retry provider.

type Retrier added in v1.20210615.7

type Retrier struct {
	MaxAttempts         uint
	DelayProvider       DelayProvider
	ShouldRetryProvider ShouldRetryProvider
}

Retrier is the retry agent.

func New added in v1.20210615.7

func New(opts ...Option) *Retrier

New wraps an actioner with retries.

func (Retrier) Intercept added in v1.20210615.7

func (r Retrier) Intercept(action Actioner) Actioner

Intercept calls a function and retries on error or if a should retry provider is set, based on the should retry result.

type ShouldRetryProvider

type ShouldRetryProvider func(error) bool

ShouldRetryProvider is a function that returns if we should retry on an error or abort retries. Return `true` to continue to retry, and `false` otherwise to abort retries. If you do not specify a provider, all errors will be retried (`true` by default)

Jump to

Keyboard shortcuts

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