package backoff

import "github.com/Rican7/retry/backoff"

Package backoff provides stateless methods of calculating durations based on a number of attempts made.

Copyright © 2016 Trevor N. Suarez (Rican7)

Index

Examples

Types

type Algorithm

type Algorithm func(attempt uint) time.Duration

Algorithm defines a function that calculates a time.Duration based on the given retry attempt number.

func BinaryExponential

func BinaryExponential(factor time.Duration) Algorithm

BinaryExponential creates a Algorithm that multiplies the factor duration by an exponentially increasing factor for each attempt, where the factor is calculated as `2` raised to the attempt number (2^attempt).

Example

Code:

{
	algorithm := BinaryExponential(15 * time.Millisecond)

	for i := uint(1); i <= 5; i++ {
		duration := algorithm(i)

		fmt.Printf("#%d attempt: %s\n", i, duration)
	}

	// Output:
	// #1 attempt: 30ms
	// #2 attempt: 60ms
	// #3 attempt: 120ms
	// #4 attempt: 240ms
	// #5 attempt: 480ms
}

Output:

#1 attempt: 30ms
#2 attempt: 60ms
#3 attempt: 120ms
#4 attempt: 240ms
#5 attempt: 480ms

func Exponential

func Exponential(factor time.Duration, base float64) Algorithm

Exponential creates a Algorithm that multiplies the factor duration by an exponentially increasing factor for each attempt, where the factor is calculated as the given base raised to the attempt number.

Example

Code:

{
	algorithm := Exponential(15*time.Millisecond, 3)

	for i := uint(1); i <= 5; i++ {
		duration := algorithm(i)

		fmt.Printf("#%d attempt: %s\n", i, duration)
	}

	// Output:
	// #1 attempt: 45ms
	// #2 attempt: 135ms
	// #3 attempt: 405ms
	// #4 attempt: 1.215s
	// #5 attempt: 3.645s
}

Output:

#1 attempt: 45ms
#2 attempt: 135ms
#3 attempt: 405ms
#4 attempt: 1.215s
#5 attempt: 3.645s

func Fibonacci

func Fibonacci(factor time.Duration) Algorithm

Fibonacci creates a Algorithm that multiplies the factor duration by an increasing factor for each attempt, where the factor is the Nth number in the Fibonacci sequence.

Example

Code:

{
	algorithm := Fibonacci(15 * time.Millisecond)

	for i := uint(1); i <= 5; i++ {
		duration := algorithm(i)

		fmt.Printf("#%d attempt: %s\n", i, duration)
	}

	// Output:
	// #1 attempt: 15ms
	// #2 attempt: 15ms
	// #3 attempt: 30ms
	// #4 attempt: 45ms
	// #5 attempt: 75ms
}

Output:

#1 attempt: 15ms
#2 attempt: 15ms
#3 attempt: 30ms
#4 attempt: 45ms
#5 attempt: 75ms

func Incremental

func Incremental(initial, increment time.Duration) Algorithm

Incremental creates a Algorithm that increments the initial duration by the given increment for each attempt.

Example

Code:

{
	algorithm := Incremental(15*time.Millisecond, 10*time.Millisecond)

	for i := uint(1); i <= 5; i++ {
		duration := algorithm(i)

		fmt.Printf("#%d attempt: %s\n", i, duration)
	}

	// Output:
	// #1 attempt: 25ms
	// #2 attempt: 35ms
	// #3 attempt: 45ms
	// #4 attempt: 55ms
	// #5 attempt: 65ms
}

Output:

#1 attempt: 25ms
#2 attempt: 35ms
#3 attempt: 45ms
#4 attempt: 55ms
#5 attempt: 65ms

func Linear

func Linear(factor time.Duration) Algorithm

Linear creates a Algorithm that linearly multiplies the factor duration by the attempt number for each attempt.

Example

Code:

{
	algorithm := Linear(15 * time.Millisecond)

	for i := uint(1); i <= 5; i++ {
		duration := algorithm(i)

		fmt.Printf("#%d attempt: %s\n", i, duration)
	}

	// Output:
	// #1 attempt: 15ms
	// #2 attempt: 30ms
	// #3 attempt: 45ms
	// #4 attempt: 60ms
	// #5 attempt: 75ms
}

Output:

#1 attempt: 15ms
#2 attempt: 30ms
#3 attempt: 45ms
#4 attempt: 60ms
#5 attempt: 75ms

Source Files

backoff.go

Version
v0.3.1 (latest)
Published
Aug 12, 2021
Platform
js/wasm
Imports
2 packages
Last checked
21 hours ago

Tools for package owners.