exreg v0.0.2 ExReg

A simple process name registry using :pg2. Uses :pg2 (running by default when starting the EVM) to associate a name (any Elixir term) to a process.

Example

A simple ping-pong server:

defmodule Server do
  use GenServer

  def start_link(opts \ []) do
    GenServer.start_link(__MODULE__, nil, opts)
  end

  def stop(name, reason \ :normal) do
    GenServer.stop(name, reason)
  end

  def ping(name) do
    GenServer.call(name, :ping)
  end

  def handle_call(:ping, _from, _) do
    {:reply, :pong, nil}
  end
end

And using ExReg as name registry:

iex(1)> name = {:name, make_ref()}
iex(2)> {:ok, pid} = Server.start_link({:via, ExReg, name})
iex(3)> Server.ping({:via, ExReg, name})
:pong
iex(4)> Server.stop({:via, ExReg, name})
:ok

Summary

Functions

Registers the process pid with the term name

Sends a message to the PID associated with name

Unregisters a name

Searches for the PID associated with the name

Functions

register_name(name, pid)

Specs

register_name(name :: term, pid :: pid) :: :yes | :no

Registers the process pid with the term name.

send(name, message)

Specs

send(name :: term, message :: term) :: pid

Sends a message to the PID associated with name.

unregister_name(name)

Specs

unregister_name(name :: term) :: term

Unregisters a name.

whereis_name(name)

Specs

whereis_name(name :: term) :: pid | :undefined

Searches for the PID associated with the name.