Registry.register
register
, go back to Registry module for more information.
Specs
Registers the current process under the given key
in registry
.
A value to be associated with this registration must also be given. This value will be retrieved whenever dispatching or doing a key lookup.
This function returns {:ok, owner}
or {:error, reason}
.
The owner
is the PID in the registry partition responsible for
the PID. The owner is automatically linked to the caller.
If the registry has unique keys, it will return {:ok, owner}
unless
the key is already associated to a PID, in which case it returns
{:error, {:already_registered, pid}}
.
If the registry has duplicate keys, multiple registrations from the current process under the same key are allowed.
Examples
Registering under a unique registry does not allow multiple entries:
iex> Registry.start_link(keys: :unique, name: Registry.UniqueRegisterTest)
iex> {:ok, _} = Registry.register(Registry.UniqueRegisterTest, "hello", :world)
iex> Registry.register(Registry.UniqueRegisterTest, "hello", :later)
{:error, {:already_registered, self()}}
iex> Registry.keys(Registry.UniqueRegisterTest, self())
["hello"]
Such is possible for duplicate registries though:
iex> Registry.start_link(keys: :duplicate, name: Registry.DuplicateRegisterTest)
iex> {:ok, _} = Registry.register(Registry.DuplicateRegisterTest, "hello", :world)
iex> {:ok, _} = Registry.register(Registry.DuplicateRegisterTest, "hello", :world)
iex> Registry.keys(Registry.DuplicateRegisterTest, self())
["hello", "hello"]