retry

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2020 License: Apache-2.0, BSD-2-Clause, MIT, + 1 more Imports: 4 Imported by: 14

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Until is an alias for `DefaultOps().Until`.
	Until = DefaultOps().Until

	// UntilTimeout is an alias for `DefaultOps().New`.
	UntilTimeout = DefaultOps().UntilTimeout

	// Interval is an alias for `DefaultIntervalFactory().New`.
	Interval = DefaultIntervalFactory().New
)

Functions

func MinorError

func MinorError(minorErr error) (done bool, err error)

MinorError indicates an operation was not successful due to the given error but can be retried.

func NewRetryError

func NewRetryError(ctxError, err error) error

NewRetryError returns a new error with the given context error and error. The non-context error is optional.

func NotOk

func NotOk() (done bool, err error)

NotOk indicates that an operation was not successful but can be retried. It does not indicate an error. For better error reporting, consider MinorError.

func Ok

func Ok() (done bool, err error)

Ok indicates that an operation was successful and does not need to be retried.

func SevereError

func SevereError(severeErr error) (done bool, err error)

SevereError indicates an operation was not successful due to the given error and cannot be retried.

func UntilFor

func UntilFor(ctx context.Context, waitFunc WaitFunc, agg ErrorAggregator, f Func) error

UntilFor keeps retrying the given Func until it either errors severely or the context expires. Between each try, it waits using the context of the given WaitFunc.

Types

type ErrorAggregator

type ErrorAggregator interface {
	// Minor records the given minor error.
	Minor(err error)
	// Severe records the given severe error.
	Severe(err error)
	// Error returns the aggregated error.
	Error() error
}

An ErrorAggregator aggregates minor and severe errors.

It's completely up to the ErrorAggregator how to aggregate the errors. Some may choose to only keep the most recent error they received. If no error was being recorded and the Error function is being called, the ErrorAggregator should return a proper zero value (in most cases, this will be nil).

func NewLastErrorAggregator

func NewLastErrorAggregator() ErrorAggregator

NewLastErrorAggregator returns an ErrorAggregator that only keeps the last error it recorded.

type ErrorAggregatorFactory

type ErrorAggregatorFactory interface {
	New() ErrorAggregator
}

ErrorAggregatorFactory is a factory that produces ErrorAggregators.

func DefaultErrorAggregatorFactory

func DefaultErrorAggregatorFactory() ErrorAggregatorFactory

DefaultErrorAggregatorFactory returns the default ErrorAggregatorFactory.

type ErrorAggregatorFactoryFunc

type ErrorAggregatorFactoryFunc func() ErrorAggregator

ErrorAggregatorFactoryFunc is a function that implements ErrorAggregatorFactory.

func (ErrorAggregatorFactoryFunc) New

New implements ErrorAggregatorFactory.

type Func

type Func func(ctx context.Context) (done bool, err error)

Func is a function that can be retried.

There four possible return combinations. For each of these, there's also a utility function: * Ok (true, nil): Execution succeeded without error. * NotOk (false, nil): Execution failed without error, can be retried. * MinorError (false, err): Execution failed with error, can be retried. * SevereError (true, err): Execution failed with error, cannot be retried.

type IntervalFactory

type IntervalFactory interface {
	New(interval time.Duration) WaitFunc
}

IntervalFactory is a factory that can produce WaitFuncs that wait for the given interval.

func DefaultIntervalFactory

func DefaultIntervalFactory() IntervalFactory

DefaultIntervalFactory returns the default IntervalFactory.

func NewIntervalFactory

func NewIntervalFactory(contextOps utilcontext.Ops) IntervalFactory

NewIntervalFactory returns a new IntervalFactory using the given utilcontext.Ops.

type IntervalFactoryFunc

type IntervalFactoryFunc func(interval time.Duration) WaitFunc

IntervalFactoryFunc is a function that implements IntervalFactory.

func (IntervalFactoryFunc) New

func (f IntervalFactoryFunc) New(interval time.Duration) WaitFunc

New implements IntervalFactory.

type Ops

type Ops interface {
	// Until keeps retrying the given Func until it either errors severely or the context expires.
	// Between each try, it waits for the given interval.
	Until(ctx context.Context, interval time.Duration, f Func) error
	// Until keeps retrying the given Func until it either errors severely or the context expires.
	// Between each try, it waits for the given interval.
	// It also passes down a modified context to the execution that times out after the given timeout.
	UntilTimeout(ctx context.Context, interval, timeout time.Duration, f Func) error
}

Ops are additional operations that can be done based on the UntilFor method.

func DefaultOps

func DefaultOps() Ops

DefaultOps returns the default Ops with the DefaultIntervalFactory, DefaultErrorAggregatorFactory and utilcontext.DefaultOps.

func NewOps

func NewOps(intervalFactory IntervalFactory, errorAggregatorFactory ErrorAggregatorFactory, contextOps utilcontext.Ops) Ops

NewOps returns the new ops with the given IntervalFactory, ErrorAggregatorFactory and utilcontext.Ops.

type WaitFunc

type WaitFunc func(context.Context) (context.Context, context.CancelFunc)

WaitFunc is a function that given context of a retry execution, returns a context that is closed after a predefined wait amount.

Jump to

Keyboard shortcuts

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