WebsockexNova.Transport.Reconnection.JitteredBackoff (WebsockexNova v0.1.0)

View Source

Implements a linear backoff with jitter reconnection strategy.

This strategy increases the delay linearly with each attempt and adds a random jitter to prevent thundering herd problems. It provides a more gradual increase than exponential backoff while still spreading out reconnections.

Options

  • :base_delay - Base delay in milliseconds (default: 1000)
  • :jitter_factor - Random factor to apply (0.0-1.0, default: 0.2)
  • :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.JitteredBackoff{
  base_delay: non_neg_integer(),
  jitter_factor: float(),
  max_retries: pos_integer() | :infinity
}

Functions

calculate_delay(jittered_backoff, attempt)

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

Calculate the delay for a reconnection attempt.

For JitteredBackoff, this increases linearly as attempt * base_delay, with a random jitter applied.

Parameters

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

Returns

  • Calculated delay in milliseconds

should_retry?(jittered_backoff, attempt)

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

Determines if a reconnection should be attempted.

Parameters

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

Returns

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