Provides process registration utilities.
This module offers a simple way to register and look up processes by name, supporting both unique and duplicate name registrations. It is useful for managing worker processes within a service.
See the Storage and State guide for more details.
Summary
Functions
Counts all processes matching a pattern and guards.
Finds the process and value previously registered with register/2.
Registers the current process with a key and optional value.
Registers the current process with a key and optional value.
Unregisters the current process from a key in Malla.ProcStore.
Finds all processes and values previously registered with store/2.
Finds all processes and values matching a pattern and guards.
Helper utility to register servers with arbitrary names.
Finds a process previously registered with register/2 or via/1.
Functions
Counts all processes matching a pattern and guards.
Returns the number of processes registered under the given key that match the provided pattern and guards.
Examples
Malla.Registry.count(:my_key)
#=> 2
Malla.Registry.count(:my_key, {:_, :specific_value})
#=> 1
Finds the process and value previously registered with register/2.
Returns a tuple of {pid, value} if found, or nil otherwise.
Examples
Malla.Registry.lookup(:my_key)
#=> {#PID<0.123.0>, %{some: :data}}
Malla.Registry.lookup(:unknown_key)
#=> nil
Registers the current process with a key and optional value.
Uses Malla.ProcRegistry which does not allow duplicate registrations.
If a process is already registered with the given key, returns an error.
Examples
Malla.Registry.register(:my_key)
#=> {:ok, #PID<0.123.0>}
Malla.Registry.register(:my_key, %{some: :data})
#=> {:error, {:already_registered, #PID<0.123.0>}}
Registers the current process with a key and optional value.
Uses Malla.ProcStore which allows duplicate registrations.
Multiple processes can register with the same key.
Examples
Malla.Registry.store(:my_key)
#=> {:ok, #PID<0.123.0>}
Malla.Registry.store(:my_key, %{some: :data})
#=> {:ok, #PID<0.124.0>}
@spec unstore(term()) :: :ok
Unregisters the current process from a key in Malla.ProcStore.
Examples
Malla.Registry.unstore(:my_key)
#=> :ok
Finds all processes and values previously registered with store/2.
Returns a list of {pid, value} tuples for all processes registered under the given key.
Examples
Malla.Registry.values(:my_key)
#=> [{#PID<0.123.0>, :value1}, {#PID<0.124.0>, :value2}]
Finds all processes and values matching a pattern and guards.
Allows pattern matching on the stored values using Erlang match specifications.
Examples
Malla.Registry.values(:my_key, {:_, :_, :"$1"})
#=> [{#PID<0.123.0>, :value1}]
Helper utility to register servers with arbitrary names.
Returns a via tuple that can be used with GenServer and other OTP behaviors
for process registration. Uses Malla.ProcRegistry which does not allow duplicates.
Example
GenServer.start_link(__MODULE__, [], name: Malla.Registry.via("my_name"))
Finds a process previously registered with register/2 or via/1.
Returns the PID if found, or nil otherwise.
Examples
Malla.Registry.whereis(:my_key)
#=> #PID<0.123.0>
Malla.Registry.whereis(:unknown_key)
#=> nil