strategy

package
v5.0.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2020 License: MIT Imports: 2 Imported by: 5

Documentation

Overview

Package strategy provides a way to define how retry is performed.

Index

Constants

View Source
const (
	Skip   = true
	Strict = false
)

Variables

This section is empty.

Functions

func NetworkError

func NetworkError(defaults bool) func(error) bool

NetworkError creates an error Handler that checks an error and returns true if an error is the temporary network error. The Handler returns the defaults if an error is not a network error.

Types

type Breaker

type Breaker interface {
	// Done returns a channel that's closed when a cancellation signal occurred.
	Done() <-chan struct{}
	// Err returns a non-nil error if Done is closed and nil otherwise.
	// After Err returns a non-nil error, successive calls to Err return the same error.
	Err() error
}

A Breaker carries a cancellation signal to break an action execution.

It is a subset of context.Context and github.com/kamilsk/breaker.Breaker.

type Error

type Error interface {
	error
	Retriable() bool // Is the error retriable?
}

An Error represents a retriable error.

type ErrorHandler

type ErrorHandler func(error) bool

ErrorHandler defines a function that CheckError can use to determine whether it should make the next attempt or not. Returning true allows for the next attempt to be made. Returning false halts the retrying process and returns the last error returned by the called Action.

type Strategy

type Strategy func(breaker Breaker, attempt uint, err error) bool

Strategy defines a function that Retry calls before every successive attempt to determine whether it should make the next attempt or not. Returning true allows for the next attempt to be made. Returning false halts the retrying process and returns the last error returned by the called Action.

The strategy will be passed an "attempt" number on each successive retry iteration, starting with a 0 value before the first attempt is actually made. This allows for a pre-action delay, etc.

func Backoff

func Backoff(algorithm func(attempt uint) time.Duration) Strategy

Backoff creates a Strategy that waits before each attempt, with a duration as defined by the given backoff.Algorithm.

func BackoffWithJitter

func BackoffWithJitter(
	algorithm func(attempt uint) time.Duration,
	transformation func(duration time.Duration) time.Duration,
) Strategy

BackoffWithJitter creates a Strategy that waits before each attempt, with a duration as defined by the given backoff.Algorithm and jitter.Transformation.

func CheckError

func CheckError(handlers ...func(error) bool) Strategy

CheckError creates a Strategy that checks an error and returns if an error is retriable or not. Otherwise, it returns the defaults.

func Delay

func Delay(duration time.Duration) Strategy

Delay creates a Strategy that waits the given duration before the first attempt is made.

func Limit

func Limit(value uint) Strategy

Limit creates a Strategy that limits the number of attempts that Retry will make.

func Wait

func Wait(durations ...time.Duration) Strategy

Wait creates a Strategy that waits the given durations for each attempt after the first. If the number of attempts is greater than the number of durations provided, then the strategy uses the last duration provided.

Jump to

Keyboard shortcuts

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