Phoenix.SocketClient.BinaryPool (phoenix_socket_client v0.8.0)

View Source

High-performance binary pooling for commonly used JSON patterns.

This module implements a binary pooling mechanism to reduce memory allocation and garbage collection pressure for frequently used JSON patterns in Phoenix Channel messages. It provides:

  • Binary pooling for common message templates
  • Memory-efficient storage of repeated patterns
  • Fast lookup and retrieval of pooled binaries
  • Automatic cleanup of unused patterns

Usage

The pool is automatically started by the socket supervisor and can be used directly in message encoding/decoding operations.

Performance Benefits

  • Reduces memory allocation for repeated patterns
  • Lowers GC pressure from frequent binary creation
  • Improves encoding/decoding performance for common messages
  • Provides consistent memory usage patterns

Summary

Types

Pool configuration options

t()

Pool state structure

Functions

Returns a specification to start this module under a supervisor.

Creates common Phoenix Channel message patterns for warm-up.

Gets a pooled binary without storing if not found.

Gets a pooled binary if available, otherwise stores the provided binary.

Starts the binary pool server.

Gets pool statistics for monitoring.

Pre-populates the pool with common message patterns.

Types

opts()

@type opts() :: [
  pool_size: non_neg_integer(),
  cleanup_interval: non_neg_integer(),
  max_age: non_neg_integer(),
  registry_name: atom()
]

Pool configuration options

t()

@type t() :: %Phoenix.SocketClient.BinaryPool{
  cleanup_interval: non_neg_integer(),
  max_age: non_neg_integer(),
  patterns: %{
    required(binary()) => %{
      count: non_neg_integer(),
      last_used: integer(),
      size: non_neg_integer()
    }
  },
  pool_size: non_neg_integer(),
  registry_name: atom() | nil,
  total_count: non_neg_integer(),
  total_memory: non_neg_integer()
}

Pool state structure

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

common_patterns()

@spec common_patterns() :: [binary()]

Creates common Phoenix Channel message patterns for warm-up.

Returns a list of JSON binaries for common message types.

get(pool_pid, binary)

@spec get(pid(), binary()) :: {:ok, binary()} | :error

Gets a pooled binary without storing if not found.

Returns {:ok, binary} if found, :error otherwise.

get_or_pool(pool_pid, binary)

@spec get_or_pool(pid(), binary()) :: binary()

Gets a pooled binary if available, otherwise stores the provided binary.

Returns the pooled binary reference.

start_link(opts \\ [])

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

Starts the binary pool server.

Options

  • :pool_size - Maximum number of patterns to pool (default: 1000)
  • :cleanup_interval - Cleanup interval in milliseconds (default: 30000)
  • :max_age - Maximum age for patterns in milliseconds (default: 300000)
  • :registry_name - Registry name for process registration

stats(pool_pid)

@spec stats(pid()) :: map()

Gets pool statistics for monitoring.

warm_up(pool_pid, patterns)

@spec warm_up(pid(), [binary()]) :: :ok

Pre-populates the pool with common message patterns.

Useful for warming up the pool with known patterns.