Snakepit.Pool.Registry (Snakepit v0.6.10)

View Source

Registry for pool worker processes.

This is a thin wrapper around Elixir's Registry that provides:

  • Consistent naming for worker processes
  • Easy migration path to distributed registry (Horde)
  • Helper functions for worker lookup

Canonical Metadata

All workers store a metadata map containing the following canonical keys:

  • :worker_module – module that owns the worker implementation (usually Snakepit.GRPCWorker)
  • :pool_name – atom name of the logical pool (e.g. :default)
  • :pool_identifier – optional human-friendly identifier used in docs/metrics
  • :adapter_module – adapter used to launch the Python worker

Higher-level helpers (pool, diagnostics, worker profiles) should prefer Snakepit.Pool.Registry.fetch_worker/1 so these keys stay authoritative.

Summary

Functions

Returns the child spec for the registry.

Returns {pid, metadata} for a registered worker.

Get worker_id from PID for O(1) lookups in :DOWN messages.

Returns only the metadata for a worker.

Gets the PID for a worker ID.

Lists all registered worker IDs.

Returns the list of canonical metadata keys maintained for each worker.

Adds or updates metadata for a registered worker.

Register a worker with metadata for O(1) reverse lookups. This is only used for manual registration - workers started with via_tuple are already registered.

Returns a via tuple for registering/looking up a worker process.

Counts the number of registered workers.

Checks if a worker is registered.

Functions

child_spec(opts)

Returns the child spec for the registry.

fetch_worker(worker_id)

Returns {pid, metadata} for a registered worker.

get_worker_id_by_pid(pid)

Get worker_id from PID for O(1) lookups in :DOWN messages.

get_worker_metadata(worker_id)

Returns only the metadata for a worker.

get_worker_pid(worker_id)

Gets the PID for a worker ID.

list_workers()

Lists all registered worker IDs.

metadata_keys()

Returns the list of canonical metadata keys maintained for each worker.

put_metadata(worker_id, metadata)

Adds or updates metadata for a registered worker.

Accepts maps to keep metadata consistent across callers. When Registry has nil metadata (the default when using :via tuples), this function replaces it with the provided map. Future updates merge with the existing map.

Returns :ok on success or {:error, :not_registered} if the worker has not been registered yet (best-effort semantics).

register_worker(worker_id, pid)

Register a worker with metadata for O(1) reverse lookups. This is only used for manual registration - workers started with via_tuple are already registered.

via_tuple(worker_id)

Returns a via tuple for registering/looking up a worker process.

Examples

iex> Snakepit.Pool.Registry.via_tuple("worker_123")
{:via, Registry, {Snakepit.Pool.Registry, "worker_123"}}

worker_count()

Counts the number of registered workers.

worker_exists?(worker_id)

Checks if a worker is registered.