delays

package
v0.0.0-...-87a4e12 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const Stop time.Duration = -1

Stop indicates that no more retries should be made.

Variables

This section is empty.

Functions

This section is empty.

Types

type DelayDecider

type DelayDecider func(attempt uint, startTime time.Time) time.Duration

DelayDecider returns the duration to wait before the next retry based on the current attempt and when the action being retried started.

- attempt is the current attempt, starting at 1. - startTime is the time when the first attempt was made.

May return Stop to indicate that no more retries should be made.

func Constant

func Constant(d time.Duration) DelayDecider

Constant returns a delay decider that always returns the same delay.

func Exponential

func Exponential(initial time.Duration, multiplier float64) DelayDecider

Exponential returns a delay decider that returns an exponential delay. The delay is calculated as initial * multiplier^(attempt-1).

Example:

decider := Exponential(1*time.Second, 2)
decider(1, ...) // 1s
decider(2, ...) // 2s
decider(3, ...) // 4s

func Never

func Never() DelayDecider

Never returns a delay decider that never retries.

func StopAfterMaxAttempts

func StopAfterMaxAttempts(decider DelayDecider, maxAttempts uint) DelayDecider

StopAfterMaxAttempts creates a DelayDecider that stops the retrying after a certain number of attempts.

Example:

decider := StopAfterMaxAttempts(Exponential(1*time.Second, 2), 3)
decider(1, ...) // 1s
decider(2, ...) // 2s
decider(3, ...) // 4s
decider(4, ...) // backoff.Stop

func StopAfterMaxDelay

func StopAfterMaxDelay(decider DelayDecider, maxDelay time.Duration) DelayDecider

StopAfterMaxDelay creates a DelayDecider that stops the retrying after a certain delay has been reached.

Example:

decider := StopAfterMaxDelay(Exponential(1*time.Second, 2), 5*time.Second)
decider(1) // 1s
decider(2) // 2s
decider(3) // 4s
decider(4) // backoff.Stop

func StopAfterMaxTime

func StopAfterMaxTime(decider DelayDecider, maxTime time.Duration) DelayDecider

StopAfterMaxTime creates a DelayDecider that stops the retrying after a certain amount of time has passed.

Example:

decider := StopAfterMaxTime(Exponential(1*time.Second, 2), 5*time.Second)

startTime := time.Now()
decider(1, startTime) // 1s

time.Sleep(5 * time.Second)
decider(2, startTime) // backoff.Stop

func WithJitter

func WithJitter(decider DelayDecider, jitter time.Duration) DelayDecider

WithJitter add a jitter to the delay returned by another delay decider. The jitter will be a random value between -jitter and +jitter.

Example:

WithJitter(Exponential(1*time.Second, 2), 100*time.Millisecond)

func WithJitterFactor

func WithJitterFactor(decider DelayDecider, jitterScale float64) DelayDecider

Example:

WithJitterFactor(Exponential(1*time.Second, 2), 0.1)

func WithMaxDelay

func WithMaxDelay(decider DelayDecider, maxDelay time.Duration) DelayDecider

WithMaxDelay applies a maximum delay to an existing delay decider. This will not limit the number of attempts, only the delay between attempts.

Example:

decider := WithMaxDelay(Exponential(1*time.Second, 2), 5*time.Second)
decider(1, ...) // 1s
decider(2, ...) // 2s
decider(3, ...) // 4s
decider(4, ...) // 5s (instead of 8s)

Jump to

Keyboard shortcuts

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