WebsockexAdapter.Reconnection (WebsockexAdapter v0.1.1)
View SourceInternal reconnection helper for Client GenServer.
This module provides reconnection logic that runs within the Client GenServer process to maintain Gun message ownership. It handles:
- Connection establishment with retry logic
- Exponential backoff calculations
- Subscription restoration after reconnection
Architecture
This module is called by the Client GenServer during its handle_continue and handle_info callbacks. All functions run in the Client GenServer process to ensure the new Gun connection sends messages to the correct process.
Not for External Use
This module is internal to WebsockexAdapter. External code should use
WebsockexAdapter.Client.connect/2
which handles initial connection attempts
and automatic reconnection.
Summary
Functions
Calculate exponential backoff delay for reconnection attempts.
Attempt to establish a Gun connection with the given configuration.
Check if maximum retry attempts have been exceeded.
Restore subscriptions after successful reconnection.
Determine if a connection error should trigger reconnection.
Functions
@spec calculate_backoff( attempt :: non_neg_integer(), base_delay :: pos_integer(), max_backoff :: pos_integer() | nil ) :: pos_integer()
Calculate exponential backoff delay for reconnection attempts.
Examples
iex> calculate_backoff(0, 1000)
1000
iex> calculate_backoff(1, 1000)
2000
iex> calculate_backoff(5, 1000, 30000)
30000 # Capped at max_backoff
@spec establish_connection(WebsockexAdapter.Config.t()) :: {:ok, gun_pid :: pid(), stream_ref :: reference(), monitor_ref :: reference()} | {:error, term()}
Attempt to establish a Gun connection with the given configuration.
This function must be called from within the Client GenServer process to ensure Gun sends messages to the correct process.
@spec max_retries_exceeded?( attempt :: non_neg_integer(), max_retries :: non_neg_integer() ) :: boolean()
Check if maximum retry attempts have been exceeded.
@spec restore_subscriptions( gun_pid :: pid(), stream_ref :: reference(), subscriptions :: [String.t()] ) :: :ok
Restore subscriptions after successful reconnection.
This should be called after the WebSocket upgrade is complete and the connection is ready to receive subscription messages.
Determine if a connection error should trigger reconnection.
Returns true for recoverable errors like network issues, false for unrecoverable errors like invalid credentials.