retryutils

package
v0.0.0-...-27af30b Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: Apache-2.0 Imports: 10 Imported by: 38

Documentation

Overview

Package retryutils defines common retry and jitter logic.

Package retryutils defines common retry and jitter logic.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PermanentRetryError

func PermanentRetryError(err error) error

PermanentRetryError returns a new instance of a permanent retry error.

func RetryStaticFor

func RetryStaticFor(d time.Duration, w time.Duration, f func() error) error

RetryFastFor retries a function repeatedly for a set amount of time before returning an error.

Intended mostly for tests.

Types

type Driver

type Driver interface {
	// Duration calculates the step-specific delay for a given attempt. Excludes
	// base duration and jitter, which are applied by the outer retry instance.
	Duration(attempt int64) time.Duration

	// Check verifies the correctness of any driver-internal parameters.
	Check() error
}

driver is the underlying retry driver. determines the difference in behavior between linear/exponential retries.

NOTE: drivers must be stateless. If a stateful driver needs to be implemented in the future, this interface will need to be extended to support safe use of Retry.Clone.

func NewExponentialDriver

func NewExponentialDriver(base time.Duration) Driver

NewExponentialDriver creates a new exponential retry driver with the supplied base step value. Resulting retries double their base backoff on each increment.

func NewLinearDriver

func NewLinearDriver(step time.Duration) Driver

NewLinearDriver creates a linear retry driver with the supplied step value. Resulting retries have increase their backoff by a fixed step amount on each increment, with the first retry having a base step amount of zero.

type Jitter

type Jitter func(time.Duration) time.Duration

Jitter is a function which applies random jitter to a duration. Used to randomize backoff values. Must be safe for concurrent usage.

func NewFullJitter

func NewFullJitter() Jitter

NewFullJitter builds a new jitter on the range [0,d). Most use-cases are better served by a jitter with a meaningful minimum value, but if the *only* purpose of the jitter is to spread out retries to the greatest extent possible (e.g. when retrying a CompareAndSwap operation), a full jitter may be appropriate.

func NewHalfJitter

func NewHalfJitter() Jitter

NewHalfJitter returns a new jitter on the range [d/2,d). This is a large range and most suitable for jittering things like backoff operations where breaking cycles quickly is a priority.

func NewJitter

func NewJitter() Jitter

NewJitter builds a new default jitter (currently jitters on the range [d/2,d), but this is subject to change).

func NewSeventhJitter

func NewSeventhJitter() Jitter

NewSeventhJitter builds a new jitter on the range [6d/7,d). Prefer smaller jitters such as this when jittering periodic operations (e.g. cert rotation checks) since large jitters result in significantly increased load.

func NewShardedFullJitter

func NewShardedFullJitter() Jitter

NewShardedFullJitter is equivalent to NewFullJitter except that it performs better under high concurrency at the cost of having a larger footprint in memory.

func NewShardedHalfJitter

func NewShardedHalfJitter() Jitter

NewShardedHalfJitter is equivalent to NewHalfJitter except that it performs better under high concurrency at the cost of having a larger footprint in memory.

func NewShardedSeventhJitter

func NewShardedSeventhJitter() Jitter

NewShardedSeventhJitter is equivalent to NewSeventhJitter except that it performs better under high concurrency at the cost of having a larger footprint in memory.

type Linear

type Linear struct {
	// LinearConfig is a linear retry config
	LinearConfig
	// contains filtered or unexported fields
}

Linear is used to calculate retry period that follows the following logic: On the first error there is no delay on the next error, delay is FastLinear on all other errors, delay is SlowLinear

func NewConstant

func NewConstant(interval time.Duration) (*Linear, error)

NewConstant returns a new linear retry with constant interval.

func NewLinear

func NewLinear(cfg LinearConfig) (*Linear, error)

NewLinear returns a new instance of linear retry

func (*Linear) After

func (r *Linear) After() <-chan time.Time

After returns channel that fires with timeout defined in Duration method, as a special case if Duration is 0 returns a closed channel

func (*Linear) Clone

func (r *Linear) Clone() Retry

Clone creates an identical copy of Linear with fresh state.

func (*Linear) Duration

func (r *Linear) Duration() time.Duration

Duration returns retry duration based on state

func (*Linear) For

func (r *Linear) For(ctx context.Context, retryFn func() error) error

For retries the provided function until it succeeds or the context expires.

func (*Linear) Inc

func (r *Linear) Inc()

Inc increments attempt counter

func (*Linear) Reset

func (r *Linear) Reset()

Reset resets retry period to initial state

func (*Linear) ResetToDelay

func (r *Linear) ResetToDelay()

ResetToDelay resets retry period and increments the number of attempts.

func (*Linear) String

func (r *Linear) String() string

String returns user-friendly representation of the LinearPeriod

type LinearConfig

type LinearConfig struct {
	// First is a first element of the progression,
	// could be 0
	First time.Duration
	// Step is a step of the progression, can't be 0
	Step time.Duration
	// Max is a maximum value of the progression,
	// can't be 0
	Max time.Duration
	// Jitter is an optional jitter function to be applied
	// to the delay.  Note that supplying a jitter means that
	// successive calls to Duration may return different results.
	Jitter Jitter `json:"-"`
	// AutoReset, if greater than zero, causes the linear retry to automatically
	// reset after Max * AutoReset has elapsed since the last call to Incr.
	AutoReset int64
	// Clock to override clock in tests
	Clock clockwork.Clock
}

LinearConfig sets up retry configuration using arithmetic progression

func (*LinearConfig) CheckAndSetDefaults

func (c *LinearConfig) CheckAndSetDefaults() error

CheckAndSetDefaults checks and sets defaults

type Retry

type Retry interface {
	// Reset resets retry state
	Reset()
	// Inc increments retry attempt
	Inc()
	// Duration returns retry duration,
	// could be 0
	Duration() time.Duration
	// After returns time.Time channel
	// that fires after Duration delay,
	// could fire right away if Duration is 0
	After() <-chan time.Time
	// Clone creates a copy of this retry in a
	// reset state.
	Clone() Retry
}

Retry is an interface that provides retry logic

type RetryV2

type RetryV2 struct {
	// RetryV2Config is a linear retry config
	RetryV2Config
	// contains filtered or unexported fields
}

RetryV2 is used to moderate the rate of retries by applying successively increasing delays. The nature of the progression is determined by the 'Driver', which generates the portion of the delay corresponding to the attempt number (e.g. Exponential(1s) might generate the sequence 0s, 1s, 2s, 4s, 8s, etc). This progression is can be modified through the use of a custom base/start value, jitters, etc.

func NewRetryV2

func NewRetryV2(cfg RetryV2Config) (*RetryV2, error)

NewRetryV2 returns a new retry instance.

func (*RetryV2) After

func (r *RetryV2) After() <-chan time.Time

After returns channel that fires with timeout defined in Duration method.

func (*RetryV2) Clone

func (r *RetryV2) Clone() Retry

Clone creates an identical copy of RetryV2 with fresh state.

func (*RetryV2) Duration

func (r *RetryV2) Duration() time.Duration

Duration returns retry duration based on state

func (*RetryV2) Inc

func (r *RetryV2) Inc()

Inc increments attempt counter

func (*RetryV2) Reset

func (r *RetryV2) Reset()

Reset resets retry period to initial state

type RetryV2Config

type RetryV2Config struct {
	// First is a first element of the progression,
	// could be 0
	First time.Duration
	// Driver generates the underlying progression of delays. Cannot be nil.
	Driver Driver
	// Max is a maximum value of the progression,
	// can't be 0
	Max time.Duration
	// Jitter is an optional jitter function to be applied
	// to the delay.  Note that supplying a jitter means that
	// successive calls to Duration may return different results.
	Jitter Jitter `json:"-"`
	// AutoReset, if greater than zero, causes the linear retry to automatically
	// reset after Max * AutoReset has elapsed since the last call to Incr.
	AutoReset int64
	// Clock to override clock in tests
	Clock clockwork.Clock
}

RetryV2Config sets up retry configuration using arithmetic progression

func (*RetryV2Config) CheckAndSetDefaults

func (c *RetryV2Config) CheckAndSetDefaults() error

CheckAndSetDefaults checks and sets defaults

Jump to

Keyboard shortcuts

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