RaRegistry (ra_registry v0.1.2)

View Source

A distributed registry for Elixir GenServers using Ra (RabbitMQ's Raft implementation).

RaRegistry provides similar functionality to Elixir's built-in Registry module, but with distributed consensus via Ra, making it suitable for distributed applications across multiple nodes.

It supports both :unique and :duplicate registration modes.

Usage

# Register a process with a unique key
RaRegistry.register(:my_registry, "unique_key", :some_value)

# Look up a process by key
RaRegistry.lookup(:my_registry, "unique_key")

# Register with a duplicate key
RaRegistry.register(:my_duplicate_registry, "shared_key", :value1)
RaRegistry.register(:my_duplicate_registry, "shared_key", :value2)

# Look up all processes with the duplicate key
RaRegistry.lookup(:my_duplicate_registry, "shared_key")

Via Registration

You can also use RaRegistry as a process registry with GenServer and other OTP processes by using the :via tuple:

# Start a GenServer with RaRegistry
name = {:via, RaRegistry, {MyRegistry, "my_server"}}
{:ok, pid} = GenServer.start_link(MyServer, arg, name: name)

# Call the server using the same via tuple
GenServer.call(name, :some_request)

Summary

Functions

See start_link/2 for options.

Returns the number of processes registered under the given key.

Returns the registry key type, either :unique or :duplicate.

Looks up the given key in the registry and returns the associated processes.

Registers the given process under the given key in the registry. If no pid is provided, registers the current process.

Implementation for the :via registration mechanism. Registers the current process under the given key in the registry name. Returns :yes if registration succeeds, :no otherwise.

Implementation for the :via registration mechanism. Sends a message to the process registered under the given key in registry.

Starts a new registry with the given name and options.

Unregisters the current process for the given key in the registry.

Implementation for the :via registration mechanism. Unregisters the given key from the registry name.

Updates the value associated with the key for the current process.

Implementation for the :via registration mechanism. Returns the pid associated with the given key in the registry name. Returns pid if successful, :undefined otherwise.

Functions

child_spec(options)

See start_link/2 for options.

count(name, key)

Returns the number of processes registered under the given key.

Examples

RaRegistry.count(MyRegistry, "key")

keys(name)

Returns the registry key type, either :unique or :duplicate.

lookup(name, key)

Looks up the given key in the registry and returns the associated processes.

Examples

RaRegistry.lookup(MyRegistry, "key")

register(name, key, value \\ nil, pid \\ nil)

Registers the given process under the given key in the registry. If no pid is provided, registers the current process.

Examples

RaRegistry.register(MyRegistry, "key", :value)
RaRegistry.register(MyRegistry, "key", :value, other_pid)

register_name(arg, pid)

@spec register_name(
  {registry :: term(), key :: term()},
  pid()
) :: :yes | :no

Implementation for the :via registration mechanism. Registers the current process under the given key in the registry name. Returns :yes if registration succeeds, :no otherwise.

send(arg, message)

@spec send(
  {registry :: term(), key :: term()},
  message :: term()
) :: pid()

Implementation for the :via registration mechanism. Sends a message to the process registered under the given key in registry.

start_link(opts)

Starts a new registry with the given name and options.

Options

  • :keys - The kind of keys in this registry. Can be either :unique or :duplicate (required).
  • :name - A local or global name for the registry (required).

Examples

RaRegistry.start_link(keys: :unique, name: MyRegistry)
RaRegistry.start_link(keys: :duplicate, name: {MyRegistry, self()})

unregister(name, key, pid \\ nil)

Unregisters the current process for the given key in the registry.

Examples

RaRegistry.unregister(MyRegistry, "key")

unregister_name(arg)

@spec unregister_name({registry :: term(), key :: term()}) :: :ok

Implementation for the :via registration mechanism. Unregisters the given key from the registry name.

update_value(name, key, callback)

Updates the value associated with the key for the current process.

Examples

RaRegistry.update_value(MyRegistry, "key", fn value -> value + 1 end)

whereis_name(arg)

@spec whereis_name({registry :: term(), key :: term()}) :: pid() | :undefined

Implementation for the :via registration mechanism. Returns the pid associated with the given key in the registry name. Returns pid if successful, :undefined otherwise.