WebsockexNova.Transport.Reconnection (WebsockexNova v0.1.1)

View Source

Provides various reconnection strategies for WebSocket connections.

This module contains implementations of different backoff algorithms for reconnection:

  • LinearBackoff - Constant delay between reconnection attempts
  • ExponentialBackoff - Exponentially increasing delay (with jitter)
  • JitteredBackoff - Linear increase with random jitter

Usage

# Get a strategy
strategy = WebsockexNova.Transport.Reconnection.get_strategy(:exponential,
  initial_delay: 1000,
  max_delay: 30_000,
  max_retries: 10
)

# Calculate delay for attempt number
delay = WebsockexNova.Transport.Reconnection.calculate_delay(strategy, attempt)

# Check if we should try again
if WebsockexNova.Transport.Reconnection.should_retry?(strategy, attempt) do
  # Schedule reconnect after delay
end

Summary

Functions

Calculates the delay for a reconnection attempt.

Returns a reconnection strategy based on the given type and options.

Determines if a reconnection should be attempted.

Functions

calculate_delay(strategy, attempt)

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

Calculates the delay for a reconnection attempt.

Delegates to the appropriate strategy's calculate_delay function.

Parameters

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

Returns

  • Delay in milliseconds

get_strategy(type, opts \\ [])

@spec get_strategy(
  atom(),
  keyword()
) :: struct()

Returns a reconnection strategy based on the given type and options.

Parameters

  • type - The type of backoff strategy (:linear, :exponential, or :jittered)
  • opts - Options specific to the chosen strategy

Returns

  • A strategy struct of the corresponding type

should_retry?(strategy, attempt)

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

Determines if a reconnection should be attempted.

Delegates to the appropriate strategy's should_retry? function.

Parameters

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

Returns

  • true if reconnection should be attempted
  • false otherwise