Sagents.ProcessRegistry (Sagents v0.4.0)

Copy Markdown

Abstraction over process registry implementations.

Supports two backends:

  • :local — Elixir's built-in Registry (single-node, zero extra deps)
  • :hordeHorde.Registry (distributed, requires the :horde dependency)

Configuration

# config/config.exs (or runtime.exs)

# Single-node (default — no config needed)
config :sagents, :distribution, :local

# Distributed cluster
config :sagents, :distribution, :horde

When :horde is selected, Horde.Registry is started with members: :auto so it automatically discovers other nodes in the Erlang cluster.

Summary

Functions

Returns the child spec for the configured registry backend.

Returns the count of all registered entries.

Returns the keys for the given process pid.

Look up a process by key.

Returns the registry module (Registry or Horde.Registry).

Returns the registry name atom (Sagents.Registry).

Select processes matching a match specification.

Returns a :via tuple for registering or looking up a process by key.

Functions

child_spec(opts)

Returns the child spec for the configured registry backend.

Used in Sagents.Application supervision tree.

count()

Returns the count of all registered entries.

keys(pid)

Returns the keys for the given process pid.

Examples

Sagents.ProcessRegistry.keys(pid)
# => [{:agent_supervisor, "agent-123"}]

lookup(key)

Look up a process by key.

Returns [{pid, value}] if found, [] otherwise.

Examples

[{pid, _}] = Sagents.ProcessRegistry.lookup({:agent_server, "agent-123"})

registry_module()

Returns the registry module (Registry or Horde.Registry).

registry_name()

Returns the registry name atom (Sagents.Registry).

select(match_spec)

Select processes matching a match specification.

The match spec format is the same as Registry.select/2.

Examples

Sagents.ProcessRegistry.select([
  {{{:agent_server, :"$1"}, :_, :_}, [], [:"$1"]}
])

via_tuple(key)

Returns a :via tuple for registering or looking up a process by key.

Examples

Sagents.ProcessRegistry.via_tuple({:agent_server, "agent-123"})
# => {:via, Registry, {Sagents.Registry, {:agent_server, "agent-123"}}}
#    or
# => {:via, Horde.Registry, {Sagents.Registry, {:agent_server, "agent-123"}}}