retryutils

package
v11.3.4 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

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 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.

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

Jump to

Keyboard shortcuts

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