项目作者: kopera

项目描述 :
Alternative Erlang registry
高级语言: Erlang
项目地址: git://github.com/kopera/erlang-reg.git
创建时间: 2019-11-28T16:51:19Z
项目社区:https://github.com/kopera/erlang-reg

开源协议:Apache License 2.0

下载


reg

An Erlang process registry based on/inspired by the Elixir process registry.

This registry allows for the lookup of processes based on a given key. The
registry comes in two flavors/kinds: unique and duplicate. When a registry
is started as unique registry, only one process is allowed to register using
the given key, that is a given key will point to 0 or 1 processes.

Setup

You need to add reg as a dependency to your project. If you are using rebar3,
you can add the following to your rebar.config:

  1. {deps, [
  2. {reg, "0.1.0"}
  3. ]}.

Also ensure that reg is added as a dependency to your application, by updating
your .app.src file:

  1. {application, my_app, [
  2. {applications, [
  3. kernel,
  4. stdlib,
  5. reg % <- You need this in your applications list
  6. ]}
  7. ]}.

Usage

Starting a registry

You can start one or more registries in your application, it is recommended to start
reg under your supervision tree:

  1. init([]) ->
  2. Flags = #{strategy => rest_for_one},
  3. Children = [
  4. reg:child_spec(my_registry, #{
  5. kind => duplicate % <- This should be set to `unique` for unique registries
  6. }),
  7. % ...
  8. ],
  9. {ok, {Flags, Children}}.

Registering a process

To register a process with the key key1 and the value value, you just need to
call register/3 from the registering process

  1. > reg:register(my_registry, key1, value).
  2. ok

Looking up processes

To find all processes registered under the key key1:

  1. > reg:lookup(my_registry, key1).
  2. [{<0.145.0>, value}]

Unregistering

A process can unregister by calling the reg:unregister/2 function. A process is
also automatically unregistered when it terminates.

  1. > reg:unregister(my_registry, key1).
  2. ok