WebsockexAdapter.Examples.BatchSubscriptionManager (WebsockexAdapter v0.1.1)

View Source

Simple batch subscription manager for efficiently subscribing to multiple channels.

Prevents overwhelming the API by batching subscription requests with configurable batch size and delay between batches.

Summary

Functions

Cancels a batch subscription request.

Returns a specification to start this module under a supervisor.

Gets the status of all batch requests.

Gets the status of a batch subscription request.

Starts the batch subscription manager.

Subscribes to multiple channels in batches.

Types

batch_status()

@type batch_status() :: %{
  completed: non_neg_integer(),
  pending: non_neg_integer(),
  total: non_neg_integer()
}

Functions

cancel_batch(manager, request_id)

@spec cancel_batch(pid(), String.t()) :: :ok | {:error, :not_found}

Cancels a batch subscription request.

Parameters

  • manager - The batch manager process
  • request_id - The request ID to cancel

Returns

  • :ok if cancelled successfully
  • {:error, :not_found} if request_id is invalid

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_all_statuses(manager)

@spec get_all_statuses(pid()) :: {:ok, map()}

Gets the status of all batch requests.

Returns

{:ok, statuses} where statuses is a map of request_id => status.

get_status(manager, request_id)

@spec get_status(pid(), String.t()) :: {:ok, batch_status()} | {:error, :not_found}

Gets the status of a batch subscription request.

Parameters

  • manager - The batch manager process
  • request_id - The request ID returned by subscribe_batch/2

Returns

  • {:ok, status} with completed/pending/total counts
  • {:error, :not_found} if request_id is invalid

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Starts the batch subscription manager.

Options

  • :adapter - The Deribit adapter process (required)
  • :batch_size - Number of channels per batch (default: 10)
  • :batch_delay - Delay between batches in ms (default: 200)

Returns

{:ok, pid} on success.

subscribe_batch(manager, channels)

@spec subscribe_batch(pid(), [String.t()]) :: {:ok, String.t()} | {:error, term()}

Subscribes to multiple channels in batches.

Parameters

  • manager - The batch manager process
  • channels - List of channel names to subscribe to

Returns

{:ok, request_id} where request_id can be used to track progress.