dispatch v0.2.0 Dispatch.Registry

Provides a distributes registry for services.

This module implements the Phoenix.Tracker behaviour to provide a distributed registry of services. Services can be added and removed to and from the registry.

Services are identified by their type. The type can be any valid Elixir term, such as an atom or string.

When a node goes down, all associated services will be removed from the registry when the CRDT syncs.

A hash ring is used to determine which service to use for a particular term. The term is arbitrary, however the same node and service pid will always be returned for a term unless the number of services for the type changes.

Optional

  • :test - If set to true then a registry and hashring will not be started when the application starts. They should be started manually with Dispatch.Registry.start_link/1 and Dispatch.Supervisor.start_hash_ring/2. Defaults to false

Summary

Functions

Add a service to the registry. The service is set as online

Set a service as offline. When a service is offline it can’t be used

Set a service as online. When a service is online it can be used

Find a list of count service instances to use for a particular key

Find a service to use for a particular key

List all of the services that are online for a particular type

List all of the services for a particular type

Remove a service from the registry

Start a new registry. The pubsub config value from Dispatch will be used

Functions

add_service(type, pid)

Add a service to the registry. The service is set as online.

  • type - The type of the service. Can be any elixir term
  • pid - The pid that provides the service

Examples

iex> Dispatch.Registry.add_service(:downloader, self())
{:ok, "g20AAAAIlB7XfDdRhmk="}
disable_service(type, pid)

Set a service as offline. When a service is offline it can’t be used.

  • type - The type of the service. Can be any elixir term
  • pid - The pid that provides the service

Examples

iex> Dispatch.Registry.disable_service(:downloader, self())
{:ok, "g20AAAAI4oU3ICYcsoQ="}
enable_service(type, pid)

Set a service as online. When a service is online it can be used.

  • type - The type of the service. Can be any elixir term
  • pid - The pid that provides the service

Examples

iex> Dispatch.Registry.enable_service(:downloader, self())
{:ok, "g20AAAAI9+IQ28ngDfM="}
find_multi_service(count, type, key)

Find a list of count service instances to use for a particular key

  • count - The number of service instances to retrieve
  • type - The type of services to retrieve
  • key - The key to lookup the service. Can be any elixir term

Examples

iex> Dispatch.Registry.find_multi_service(2, :uploader, "file.png")
[{:ok, :"slave1@127.0.0.1", #PID<0.153.0>}, {:ok, :"slave2@127.0.0.1", #PID<0.145.0>}]
find_service(type, key)

Find a service to use for a particular key

  • type - The type of service to retrieve
  • key - The key to lookup the service. Can be any elixir term

Examples

iex> Dispatch.Registry.find_service(:uploader, "file.png")
{:ok, :"slave1@127.0.0.1", #PID<0.153.0>}
get_online_services(type)

List all of the services that are online for a particular type.

  • type - The type of the service. Can be any elixir term

Examples

iex> Dispatch.Registry.get_online_services(:downloader)
[{#PID<0.166.0>,
  %{node: :"slave2@127.0.0.1", phx_ref: "g20AAAAIHAHuxydO084=",
  phx_ref_prev: "g20AAAAI4oU3ICYcsoQ=", state: :online}}]
get_services(type)

List all of the services for a particular type.

  • type - The type of the service. Can be any elixir term

Examples

iex> Dispatch.Registry.get_services(:downloader)
[{#PID<0.166.0>,
  %{node: :"slave2@127.0.0.1", phx_ref: "g20AAAAIHAHuxydO084=",
  phx_ref_prev: "g20AAAAI4oU3ICYcsoQ=", state: :online}}]
remove_service(type, pid)

Remove a service from the registry.

  • type - The type of the service. Can be any elixir term
  • pid - The pid that provides the service

Examples

iex> Dispatch.Registry.remove_service(:downloader, self())
{:ok, "g20AAAAI4oU3ICYcsoQ="}
start_link(opts \\ [])

Start a new registry. The pubsub config value from Dispatch will be used.

Examples

iex> Dispatch.Registry.start_link()
{:ok, #PID<0.168.0>}