Milvex.Backoff (milvex v0.10.2)

Copy Markdown

Exponential backoff calculator with jitter support.

Provides delay calculation for connection retry logic to prevent thundering herd problems when multiple clients reconnect simultaneously.

Summary

Functions

Calculates the next delay using exponential backoff with optional jitter.

Functions

calculate(attempt, base_delay, max_delay, multiplier \\ 2.0, jitter \\ 0.1)

@spec calculate(
  non_neg_integer(),
  pos_integer(),
  pos_integer(),
  float(),
  float()
) :: pos_integer()

Calculates the next delay using exponential backoff with optional jitter.

The formula is: min(base_delay * multiplier^attempt, max_delay) +/- jitter

Parameters

  • attempt - Current retry attempt number (0-based, first retry is attempt 0)
  • base_delay - Base delay in milliseconds
  • max_delay - Maximum delay cap in milliseconds
  • multiplier - Exponential multiplier (default: 2.0)
  • jitter - Jitter factor 0.0-1.0 (default: 0.1)

Examples

iex> Milvex.Backoff.calculate(0, 1000, 60000, 2.0, 0.0)
1000

iex> Milvex.Backoff.calculate(3, 1000, 60000, 2.0, 0.0)
8000

iex> Milvex.Backoff.calculate(10, 1000, 60000, 2.0, 0.0)
60000