retry

package
v1.20210402.2 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2021 License: MIT Imports: 3 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) (interface{}, error) {
	return http.Get("https://google.com")
})

You can also add additional parameters to the retry:

res, err := retry.Retry(ctx, func(_ context.Context) (interface{}, error) {
	return http.Get("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 DefaultOptions

func DefaultOptions(o *Options)

DefaultOptions applies defaults to a given options.

func Retry

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

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

Types

type Action

type Action func(ctx context.Context) (interface{}, error)

Action is a function you can retry.

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(*Options)

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 Options

type Options struct {
	MaxAttempts         uint
	DelayProvider       DelayProvider
	ShouldRetryProvider ShouldRetryProvider
}

Options are the options for the retry.

func (Options) Retry

func (options Options) Retry(ctx context.Context, action Action) (res interface{}, err error)

Retry 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