DeribitEx.ResubscriptionHandler (deribit_ex v0.2.0)

View Source

Handles automatic resubscription to channels after token changes.

This module is responsible for:

  • Tracking channels that need to be resubscribed after token changes
  • Providing a mechanism to resubscribe to channels automatically
  • Handling both public and private channel resubscriptions
  • Managing resubscription failures and retries

Integrates with the Adapter and Client to ensure subscriptions are maintained during token operations.

Summary

Types

t()

Represents channel resubscription state tracking.

Functions

Notifies the resubscription handler of a session transition.

Determines if a channel requires authentication.

Creates a new resubscription handler state.

Performs resubscription for all tracked channels.

Registers a channel subscription for potential resubscription.

Performs token-aware subscription to a channel.

Removes a channel subscription from tracking.

Types

t()

@type t() :: %DeribitEx.ResubscriptionHandler{
  active_session_id: String.t() | nil,
  channels: %{optional(String.t()) => map()},
  max_retries: non_neg_integer(),
  resubscribe_after_auth: boolean(),
  resubscription_in_progress: boolean(),
  retry_count: non_neg_integer()
}

Represents channel resubscription state tracking.

  • channels: Map of channel name to subscription parameters
  • active_session_id: ID of the currently active session
  • resubscription_in_progress: Flag to track when resubscription is happening
  • resubscribe_after_auth: Flag to indicate resubscription needed after authentication
  • retry_count: Counter for resubscription attempts
  • max_retries: Maximum number of resubscription retries

Functions

handle_session_transition(state, prev_session, new_session)

@spec handle_session_transition(
  t(),
  DeribitEx.SessionContext.t(),
  DeribitEx.SessionContext.t()
) ::
  {:ok, t()}

Notifies the resubscription handler of a session transition.

Parameters

  • state: Current resubscription state
  • prev_session: Previous session
  • new_session: New session after token change

Returns

  • {:ok, updated_state}: Updated state with resubscription flag set

is_private_channel?(channel)

@spec is_private_channel?(String.t()) :: boolean()

Determines if a channel requires authentication.

Parameters

  • channel: Channel name to check

Returns

  • true if the channel is private (requires authentication)
  • false if the channel is public

new(opts \\ [])

@spec new(keyword()) :: t()

Creates a new resubscription handler state.

Parameters

  • opts: Options for the handler
    • :max_retries - Maximum resubscription retry attempts (default: 3)

Returns

  • A new empty resubscription state

perform_resubscription(state, conn)

@spec perform_resubscription(t(), pid()) :: {:ok, t(), map()} | {:error, any(), t()}

Performs resubscription for all tracked channels.

Should be called after authentication completes with a new token.

Parameters

  • state: Current resubscription state
  • conn: WebsockexNova client connection

Returns

  • {:ok, updated_state, results}: Updated state and resubscription results
  • {:error, reason, state}: Error information if resubscription fails

register_subscription(state, channel, params, session_id)

@spec register_subscription(t(), String.t(), map(), String.t()) :: {:ok, t()}

Registers a channel subscription for potential resubscription.

Parameters

  • state: Current resubscription state
  • channel: Channel name or topic
  • params: Subscription parameters used for this channel
  • session_id: ID of the session that created the subscription

Returns

  • {:ok, updated_state}: Updated state with the registered channel

subscribe_to_channel(conn, channel, params)

@spec subscribe_to_channel(pid(), String.t(), map() | nil) ::
  {:ok, map() | String.t()} | {:error, any()}

Performs token-aware subscription to a channel.

Parameters

  • conn: WebsockexNova client connection
  • channel: Channel to subscribe to
  • params: Subscription parameters

Returns

  • {:ok, subscription}: Successful subscription
  • {:error, reason}: If subscription fails

unregister_subscription(state, channel)

@spec unregister_subscription(t(), String.t()) :: {:ok, t()}

Removes a channel subscription from tracking.

Parameters

  • state: Current resubscription state
  • channel: Channel to unregister

Returns

  • {:ok, updated_state}: Updated state without the channel