MqttX.Client.Backoff (MqttX v0.10.0)

Copy Markdown View Source

Exponential backoff calculator for reconnection delays.

Usage

backoff = MqttX.Client.Backoff.new(initial: 1000, max: 30_000)
{delay, backoff} = MqttX.Client.Backoff.next(backoff)
# delay = 1000

{delay, backoff} = MqttX.Client.Backoff.next(backoff)
# delay = 2000

backoff = MqttX.Client.Backoff.reset(backoff)

Summary

Functions

Get current delay without advancing.

Create a new backoff calculator.

Get the next delay and update the backoff state.

Reset the backoff to initial state.

Types

t()

@type t() :: %MqttX.Client.Backoff{
  current: pos_integer(),
  initial: pos_integer(),
  jitter: float(),
  max: pos_integer(),
  multiplier: float()
}

Functions

current(backoff)

@spec current(t()) :: pos_integer()

Get current delay without advancing.

new(opts \\ [])

@spec new(keyword()) :: t()

Create a new backoff calculator.

Options

  • :initial - Initial delay in milliseconds (default: 1000)
  • :max - Maximum delay in milliseconds (default: 30000)
  • :multiplier - Multiplier for each retry (default: 2.0)
  • :jitter - Random jitter factor 0-1 (default: 0.1)

next(backoff)

@spec next(t()) :: {pos_integer(), t()}

Get the next delay and update the backoff state.

Returns {delay_ms, new_backoff}.

reset(backoff)

@spec reset(t()) :: t()

Reset the backoff to initial state.