Backoff (PlugAMQP v0.6.0) View Source

Functions to decrease the rate of some process.

A Backoff algorithm is commonly used to space out repeated retransmissions of the same block of data, avoiding congestion.

This module provides a data structure, Backoff, that holds the state and the configuration of the backoff algorithm. Then, we can use function step/1 to get the time to wait for repeating a process and a new state of the backoff algorithm.

Example

iex> backoff = Backoff.new(kind: :exp)
#Backoff<kind: exp, min: 200, max: 15000>
iex> {200, next_backoff} = Backoff.step(backoff)
iex> {400, _next_backoff} = Backoff.step(next_backoff)

Link to this section Summary

Types

The implementation used to provide the Backoff behaviour.

Available options to configure a Backoff.

A list of option/0.

t()

A Backoff state.

Functions

Creates a new Backoff.

Sets a Backoff to the initial state.

Computes the current delay.

Link to this section Types

Specs

kind() :: :rand | :exp | :rand_exp

The implementation used to provide the Backoff behaviour.

There are different ways to provide Backoff behaviour:

  • rand: on every step, the delay time is computed randomly between two values, min and max.
  • exp: every step the delay is increased exponentially.
  • rand_exp: a combination of the previous two.

Specs

option() ::
  {:kind, kind()} | {:min, non_neg_integer()} | {:max, non_neg_integer()}

Available options to configure a Backoff.

  • kind: the implementation to be used. Can be any of the available kind/0s. Defaults to rand_exp.
  • min: the minimum value that can return a Backoff. Defaults to 200.
  • max: the maximum value that can return a Backoff. Defaults to 15000.

Specs

options() :: [option()]

A list of option/0.

Specs

t()

A Backoff state.

An opaque data structure that holds the state and the configuration of a Backoff algorithm. Can be created with new/0 or new/1.

Link to this section Functions

Specs

new(options()) :: t()

Creates a new Backoff.

Returns a new Backoff configured by options/0.

Specs

reset(t()) :: t()

Sets a Backoff to the initial state.

Given a Backoff, sets its state back to the initial value. Note that for rand implementation this functions has no effect.

Specs

step(t()) :: {non_neg_integer(), t()}

Computes the current delay.

Given a Backoff, returns the current delay time a next state of the current Backoff.