retry

package
v1.77.5 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2023 License: Apache-2.0, BSD-2-Clause, MIT, + 1 more Imports: 5 Imported by: 15

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 IsRetriable added in v1.25.0

func IsRetriable(err error) bool

IsRetriable checks if any error in err's chain is marked as an retriableError.

func MinorError

func MinorError(minorErr error) (bool, error)

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

func MinorOrSevereError added in v1.5.1

func MinorOrSevereError(retryCountUntilSevere, threshold int, err error) (bool, error)

MinorOrSevereError returns a "severe" error in case the retry count exceeds the threshold. Otherwise, it returns a "minor" error.

func NewError added in v1.16.0

func NewError(ctxError, err error) error

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

func NotOk

func NotOk() (bool, 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() (bool, error)

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

func RetriableError added in v1.25.0

func RetriableError(err error) error

RetriableError marks a given error as retriable.

func SevereError

func SevereError(severeErr error) (bool, 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 Error added in v1.16.0

type Error struct {
	// contains filtered or unexported fields
}

Error is an error that occurred during a retry operation.

func (*Error) Error added in v1.16.0

func (e *Error) Error() string

Error implements error.

func (*Error) Unwrap added in v1.16.0

func (e *Error) Unwrap() error

Unwrap implements the Unwrap function https://golang.org/pkg/errors/#Unwrap

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 contextutils.Ops) IntervalFactory

NewIntervalFactory returns a new IntervalFactory using the given contextutils.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
	// UntilTimeout 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 contextutils.DefaultOps.

func NewOps

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

NewOps returns the new ops with the given IntervalFactory, ErrorAggregatorFactory and contextutils.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.

Directories

Path Synopsis
Package mock is a generated GoMock package.
Package mock is a generated GoMock package.

Jump to

Keyboard shortcuts

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