View Source GenRegistry (gen_registry v1.3.0)

GenRegistry provides a Registry like interface for managing processes.

Link to this section Summary

Types

t()

GenRegistry State.

Functions

Return the number of running processes in this registry.

Callback implementation for GenServer.init/1.

Lookup a running a process.

Attempts to lookup a running process by id.

Loop over all the processes and return result.

Return a sample entry from the registry.

Start a registry instance.

Safely stops a process managed by the GenRegistry

Returns all the entries of the GenRegistry as a list.

Link to this section Types

@type t() :: %GenRegistry{
  worker_module: module(),
  worker_type: :supervisor | :worker,
  workers: :ets.tab()
}

GenRegistry State.

  • worker_module: Module to spawn
  • worker_type: :supervisor if the worker_module is a supervisor, :worker otherwise
  • workers: ETS table id holding the worker tracking records.

Link to this section Functions

@spec child_spec(opts :: Keyword.t()) :: Supervisor.child_spec()

Callback called by Supervisor.init/2

It is required that you provide a :worker_module argument or the call will fail.

@spec count(table :: :ets.tab()) :: non_neg_integer()

Return the number of running processes in this registry.

Callback implementation for GenServer.init/1.

@spec lookup(table :: :ets.tab(), id :: GenRegistry.Types.id()) ::
  {:ok, pid()} | {:error, :not_found}

Lookup a running a process.

This is a fast path to the ETS table.

Link to this function

lookup_or_start(registry, id, args \\ [], timeout \\ 5000)

View Source
@spec lookup_or_start(
  registry :: GenServer.server(),
  id :: GenRegistry.Types.id(),
  args :: [any()],
  timeout :: integer()
) :: {:ok, pid()} | {:error, any()}

Attempts to lookup a running process by id.

If the id is not associated with a running process then it is spawned, the optional third argument will be passed to start_link of the worker_module to spawn a new process.

Link to this function

reduce(table, acc, func)

View Source
@spec reduce(
  table :: :ets.tab(),
  acc :: any(),
  ({GenRegistry.Types.id(), pid()}, any() -> any())
) :: any()

Loop over all the processes and return result.

The function will be called with two arguments, a two-tuple of {id, pid} and then accumulator, the function should return the accumulator.

There is no ordering guarantee when reducing.

@spec sample(table :: :ets.tab()) :: {GenRegistry.Types.id(), pid()} | nil

Return a sample entry from the registry.

If the registry is empty returns nil.

Link to this function

start(registry, id, args \\ [], timeout \\ 5000)

View Source
@spec start(
  registry :: GenServer.server(),
  id :: GenRegistry.Types.id(),
  args :: [any()],
  timeout :: integer()
) :: {:ok, pid()} | {:error, {:already_started, pid()}} | {:error, any()}

Starts a process by id

If the id is already associated with a running process {:error, {:already_started, pid}} is returned.

If the id is not associated with a running process then it is spawned, the optional third argument will be passed to start_link of the worker_module to spawn a new process.

Link to this function

start_link(module, opts \\ [])

View Source
@spec start_link(module(), Keyword.t()) :: {:ok, pid()} | {:error, any()}

Start a registry instance.

GenRegistry should be run under a supervision tree, it is not recommended to call this directly.

@spec stop(registry :: GenServer.server(), id :: GenRegistry.Types.id()) ::
  :ok | {:error, :not_found}

Safely stops a process managed by the GenRegistry

In addition to stopping the process, the id is also removed from the GenRegistry

If the id provided is not registered this will return {:error, :not_found}

@spec to_list(table :: :ets.tab()) :: [{GenRegistry.Types.id(), pid()}]

Returns all the entries of the GenRegistry as a list.

There is no ordering guarantee for the list.