retrier

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: BSD-3-Clause Imports: 3 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BackoffCalculator

type BackoffCalculator interface {
	CalculateBackoff(attempt int) time.Duration
}

BackoffCalculator defines how much time to backoff based on the current attempt number.

type ErrorWrapper

type ErrorWrapper interface {
	WrapError(attempt int, err error) error
}

ErrorWrapper defines how the retry middleware should wrap errors on each successive call. Note that implementation of this interface are likely to be NOT-REENTRANT.

type EvaluationPolicy

type EvaluationPolicy int

EvaluationPolicy allows indicating to the generic retry evaluator if it should work with blacklists or whitelists

const (
	// EvaluationPolicyBlacklist indicates that an evaluation will only succeed if
	// the information being verified is NOT present in a given list
	EvaluationPolicyBlacklist EvaluationPolicy = iota
	// EvaluationPolicyWhitelist indicates that an evaluation will only succeed if
	// the information being verified is present in a given list
	EvaluationPolicyWhitelist
)

type ExponentialBackoffCalculator

type ExponentialBackoffCalculator struct {
	BaseBackoff        time.Duration
	RandomExtraBackoff time.Duration
	Multiplier         float64
}

ExponentialBackoffCalculator is a simple backoff calculator that multiplies a base backoff time n times against a multiplier. The multiplier value must be higher than 1.0. The multiplier is self-multiplied for each attempt over 2. For example, if multiplier is 2, the backoff for each attempt will be multiplied by: Attempt 1 - multiplied by 1 Attempt 2 - multiplied by 2 Attempt 3 - multiplied by 4 Attempt 4 - multiplied by 8

func NewExponentialBackoffCalculator

func NewExponentialBackoffCalculator(settings ExponentialBackoffCalculatorSettings) *ExponentialBackoffCalculator

NewExponentialBackoffCalculator return a new instance of ExponentialBackoffCalculator configured with the given @settings

func (ExponentialBackoffCalculator) CalculateBackoff

func (c ExponentialBackoffCalculator) CalculateBackoff(attempt int) time.Duration

CalculateBackoff returns a backoff calculator calculated by multiplying a base backoff time @attempt-1 times against a multiplier . The multiplier value must be higher than 1.0. If @attempt is 1, return the base backoff. See type definition for more information

Disables gosec lint here because it complains about math/rand instead of crypto/rand, but here it's ok. nolint: gosec

type ExponentialBackoffCalculatorSettings

type ExponentialBackoffCalculatorSettings struct {
	// BaseBackoff is the base time of how much to wait between attempts. Defaults to
	// 50ms
	BaseBackoff time.Duration
	// RandomExtraBackoff is an amount of extra time between retries that is randomized up to
	// this value. This helps with avoid too many retries happening at once. Defaults to 0 (disabled)
	RandomExtraBackoff time.Duration
	// Multiplier is how much to multiply the backoff time each attempt. It multiplies itself for
	// each attempt above 2. Defaults to 2 if set to a value < 1
	Multiplier float64
}

ExponentialBackoffCalculatorSettings holds information for how to calculate the backoff. Zero values will be turned into sane defaults

type GenericRetryEvaluator

type GenericRetryEvaluator struct {
	MaxAttempts            int
	ErrorsCodesPolicy      EvaluationPolicy
	ErrorsCodes            []errors.Code
	ErrorsSeveritiesPolicy EvaluationPolicy
	ErrorsSeverities       []errors.Severity
}

GenericRetryEvaluator is a retry evaluator that works with max attempts and both error codes and errors severity. Error codes and errors severity are both based on whitelist or blacklists, and are evaluated individually. If a match happens on blacklists, the error is assumed as non retryable. If a match happens on a whitelist, the errors is assumed as retryable. Both errors codes and errors severity must pass the list tests in order of the error to be retryable.

func NewGenericRetryEvaluator

func NewGenericRetryEvaluator(settings GenericRetryEvaluatorSettings) *GenericRetryEvaluator

NewGenericRetryEvaluator will return an instance of generic retry evaluator

func (*GenericRetryEvaluator) IsRetryable

func (e *GenericRetryEvaluator) IsRetryable(attempt int, attemptError error) bool

IsRetryable will return true if the @err in the given @attempt can be retried, or false otherwise.

This function works with preset black or white lists to error codes and errors severity. See type definition for more information.

type GenericRetryEvaluatorSettings

type GenericRetryEvaluatorSettings struct {
	// MaxAttempts is how many attempts to allow, starting at 1. Defaults to 5.
	MaxAttempts int
	// ErrorCodesPolicy indicates if error codes should be held with
	// blacklists or whitelists. Defaults to blacklist.
	ErrorsCodesPolicy EvaluationPolicy
	// ErrorsCodesList is the list of errors codes to use as a base. Defaults to empty.
	ErrorsCodes []errors.Code
	// ErrorSeveritiesPolicy indicates if error severity should be held with
	// blacklists or whitelists. Defaults to blacklist.
	ErrorsSeveritiesPolicy EvaluationPolicy
	// ErrorsSeveritiesList is the list of errors severity to use as a base. Defaults to empty.
	ErrorsSeverities []errors.Severity
}

GenericRetryEvaluatorSettings is used to construct GenericRetryEvaluator instances. All fields with zero value with receive default values

type LastErrorWrapper

type LastErrorWrapper struct {
}

LastErrorWrapper always return the last error received, unmodified. If the last error was nil, it will return nil as well

func NewLastErrorWrapper

func NewLastErrorWrapper() *LastErrorWrapper

NewLastErrorWrapper returns an instance of LastErrorWrapper

func (*LastErrorWrapper) WrapError

func (w *LastErrorWrapper) WrapError(_ int, err error) error

WrapError will always return @err unmodified. If @err is nil, returns nil.

type Retrier

type Retrier struct {
	RetryEvaluator    RetryEvaluator
	BackoffCalculator BackoffCalculator
	ErrorWrapper      ErrorWrapper
}

Retrier is a configurable retry helper based on the use of the Strategy pattern.

func NewRetrier

func NewRetrier(settings Settings) *Retrier

NewRetrier returns a new instance of Retrier, configured with the strategies passed by parameter by @settings

func (Retrier) ExecuteOperation

func (r Retrier) ExecuteOperation(operation func() error) error

ExecuteOperation runs the @operation with retry logic. It return the operation error according to the set errors wrapper strategy.

type RetryEvaluator

type RetryEvaluator interface {
	IsRetryable(attempt int, err error) bool
}

RetryEvaluator defines the logic to decide if an operation should be retried or not. Returns bool if the operation should be retried.

type Settings

type Settings struct {
	// Evaluator is the ruleset that decides if an error can be retried or not.
	// Defaults to GenericRetryEvaluator with retry only on SeverityRuntime and
	// 5 attempts.
	RetryEvaluator RetryEvaluator
	// BackoffCalculator is how long to wait between each attempt. Defaults
	// to ExponentialBackoffCalculator with 100ms +-20ms and multiplier of 2
	BackoffCalculator BackoffCalculator
	// ErrorWrapper is how to handle the error returned when all attempts have failed.
	// Defaults to LastErrorWrapper
	ErrorWrapper ErrorWrapper
}

Settings holds the settings to instantiate a new Retrier. If a field has zero-value, a sane default is assumed.

Jump to

Keyboard shortcuts

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