WebsockexNova.Transport.Reconnection.ExponentialBackoff (WebsockexNova v0.1.1)

View Source

Implements an exponential backoff reconnection strategy.

This strategy increases the delay exponentially with each attempt (2^n), with optional jitter to prevent thundering herd problems. It's well-suited for handling temporary network issues and server overload.

Options

  • :initial_delay - Base delay in milliseconds (default: 1000)
  • :max_delay - Maximum delay in milliseconds (default: 30_000)
  • :jitter_factor - Random factor to apply (0.0-1.0, default: 0.1)
  • :max_retries - Maximum number of retry attempts (default: 5, use :infinity for unlimited)

Summary

Functions

Calculate the delay for a reconnection attempt.

Determines if a reconnection should be attempted.

Types

t()

@type t() :: %WebsockexNova.Transport.Reconnection.ExponentialBackoff{
  initial_delay: non_neg_integer(),
  jitter_factor: float(),
  max_delay: non_neg_integer(),
  max_retries: pos_integer() | :infinity
}

Functions

calculate_delay(exponential_backoff, attempt)

@spec calculate_delay(t(), pos_integer()) :: non_neg_integer()

Calculate the delay for a reconnection attempt.

For ExponentialBackoff, this increases as 2^(attempt-1) * initial_delay, with optional jitter, and capped at max_delay.

Parameters

  • strategy - The ExponentialBackoff strategy struct
  • attempt - The current attempt number (1-based)

Returns

  • Calculated delay in milliseconds

should_retry?(exponential_backoff, attempt)

@spec should_retry?(t(), pos_integer()) :: boolean()

Determines if a reconnection should be attempted.

Parameters

  • strategy - The ExponentialBackoff strategy struct
  • attempt - The current attempt number (1-based)

Returns

  • true if the attempt number is within the maximum retries
  • false otherwise