registry (ex_stdlib v0.3.0)

View Source

A local, decentralized and scalable key-value process storage.

It allows developers to lookup one or more processes with a given key. If the registry has unique keys, a key points to 0 or 1 process. If the registry allows duplicate keys, a single key may point to any number of processes. In both cases, different keys could identify the same process.

Each entry in the registry is associated to the process that has registered the key. If the process crashes, the keys associated to that process are automatically removed. All key comparisons in the registry are done using the match operation (=:=).

The registry can be used for different purposes, such as name lookups (using the via option), storing properties, custom dispatching rules, or a pubsub implementation.

Using in via

Once the registry is started with a given name using start_link/1, it can be used to register and access named processes using the {via, registry, {registry_name, key}} tuple, or {via, registry, {registry_name, key, value}} to also store a value:

   {ok, _} = registry:start_link([{keys, unique}, {name, my_registry}]),
   Name = {via, registry, {my_registry, "agent"}},
   {ok, _} = agent:start_link(fun() -> 0 end, [{name, Name}]),
   0 = agent:get(Name, fun(State) -> State end).

Using as a dispatcher

Registry has a dispatch mechanism that allows developers to implement custom dispatch logic triggered from the caller. For example:

   {ok, _} = registry:start_link([{keys, duplicate}, {name, dispatcher_test}]),
   {ok, _} = registry:register(dispatcher_test, "hello", {io, format}),
   registry:dispatch(dispatcher_test, "hello", fun(Entries) ->
       [apply(M, F, [Pid, "Hello ~p~n"]) || {Pid, {M, F}} <- Entries]
   end).

Summary

Functions

Invokes the callback with all entries under the given key.

Returns all keys registered by the given process.

Looks up processes registered under the given key.

Registers the current process under the given key with an associated value.

Registers Pid under a {Registry, Key} or {Registry, Key, Value} name.

Sends Msg to the process registered under a {via, registry, Name} name. Exits with {badarg, {Name, Msg}} if no process is registered.

Starts a registry with the given options.

Unregisters the current process from the given key.

Unregisters a {via, registry, Name} name.

Updates the value for the given key for the current process.

Returns the values registered by the given process under the given key.

Deprecated: use {via, registry, Name} or register_name/2.

Deprecated: use unregister_name/1.

Deprecated: use whereis_name/1.

Returns the process registered under a {via, registry, Name} name, or undefined.

Types

dispatcher/0

-type dispatcher() :: fun(([entry()]) -> term()).

entry/0

-type entry() :: {pid(), value()}.

key/0

-type key() :: term().

keys/0

-type keys() :: unique | duplicate.

registry/0

-type registry() :: atom().

value/0

-type value() :: term().

via_name/0

-type via_name() :: {registry(), key()} | {registry(), key(), value()}.

Functions

dispatch(Registry, Key, Callback)

-spec dispatch(registry(), key(), dispatcher()) -> ok.

Invokes the callback with all entries under the given key.

The callback receives a list of {Pid, Value} tuples. If there are no entries for the given key, the callback is not invoked.

dispatch(Registry, Key, Callback, Options)

-spec dispatch(registry(), key(), dispatcher(), list()) -> ok.

keys(Registry, Pid)

-spec keys(registry(), pid()) -> [key()].

Returns all keys registered by the given process.

lookup(Registry, Key)

-spec lookup(registry(), key()) -> [entry()].

Looks up processes registered under the given key.

Returns a list of {Pid, Value} tuples. For unique registries, the list will have at most one element.

register(Registry, Key, Value)

-spec register(registry(), key(), value()) -> {ok, pid()} | {error, term()}.

Registers the current process under the given key with an associated value.

Returns {ok, Owner} on success where Owner is the registry process PID. Returns {error, {already_registered, Pid}} if the key is already taken in a unique registry.

register_name(_, Pid)

-spec register_name(via_name(), pid()) -> yes | no.

Registers Pid under a {Registry, Key} or {Registry, Key, Value} name.

This is the {via, registry, Name} callback used by OTP behaviours (gen_server, gen_statem, supervisor...). The value defaults to nil. Only unique registries are supported.

send(Name, Msg)

-spec send(via_name(), term()) -> pid().

Sends Msg to the process registered under a {via, registry, Name} name. Exits with {badarg, {Name, Msg}} if no process is registered.

start_link(Options)

-spec start_link([{keys, keys()} | {name, atom()}]) -> {ok, pid()} | {error, term()}.

Starts a registry with the given options.

The supported options are:

- {keys, Keys} - either unique or duplicate. Defaults to unique. - {name, Name} - the name of the registry. Required.

If keys is unique, a key can only be registered by one process. If keys is duplicate, multiple processes can register under the same key.

Returns {ok, Pid} on success.

unregister(Registry, Key)

-spec unregister(registry(), key()) -> ok.

Unregisters the current process from the given key.

Always returns ok.

unregister_name(_)

-spec unregister_name(via_name()) -> ok.

Unregisters a {via, registry, Name} name.

update_value(Registry, Key, Callback)

-spec update_value(registry(), key(), fun((value()) -> value())) -> {value(), value()} | error.

Updates the value for the given key for the current process.

Returns {NewValue, OldValue} on success or error if there is no such key registered by the current process.

Only works with unique registries.

values(Registry, Key, Pid)

-spec values(registry(), key(), pid()) -> [value()].

Returns the values registered by the given process under the given key.

For unique registries, returns a list with at most one element. For duplicate registries, returns a list with zero or more elements.

via_register(RegistryName, Key, Pid)

-spec via_register(atom(), term(), pid()) -> yes | no.

Deprecated: use {via, registry, Name} or register_name/2.

via_send(RegistryName, Name, Msg, SendOpts)

-spec via_send(atom(), term(), term(), term()) -> pid().

Deprecated: use send/2.

via_unregister(RegistryName, Key, Pid)

-spec via_unregister(atom(), term(), pid()) -> ok.

Deprecated: use unregister_name/1.

via_whereis(RegistryName, Key)

-spec via_whereis(atom(), term()) -> pid() | undefined.

Deprecated: use whereis_name/1.

whereis_name(_)

-spec whereis_name(via_name()) -> pid() | undefined.

Returns the process registered under a {via, registry, Name} name, or undefined.