dispatch v0.4.1 Dispatch.Registry View Source

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 must be a binary.

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

Link to this section 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.

Link to this section Functions

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

  • type - The type of the service. Must be a binary.
  • pid - The pid that provides the service

Examples

iex> Dispatch.Registry.add_service("downloader", self())
{:ok, "g20AAAAIlB7XfDdRhmk="}
Link to this function

disable_service(type, pid)

View Source

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

  • type - The type of the service. Must be a binary.
  • pid - The pid that provides the service

Examples

iex> Dispatch.Registry.disable_service("downloader", self())
{:ok, "g20AAAAI4oU3ICYcsoQ="}
Link to this function

enable_service(type, pid)

View Source

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

  • type - The type of the service. Must be a binary.
  • pid - The pid that provides the service

Examples

iex> Dispatch.Registry.enable_service("downloader", self())
{:ok, "g20AAAAI9+IQ28ngDfM="}
Link to this function

find_multi_service(count, type, key)

View Source

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 a service to use for a particular key

  • type - The type of the service. Must be a binary.
  • 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>}
Link to this function

get_online_services(type)

View Source

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

  • type - The type of the service. Must be a binary.

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}}]

List all of the services for a particular type.

  • type - The type of the service. Must be a binary.

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}}]
Link to this function

remove_service(type, pid)

View Source

Remove a service from the registry.

  • type - The type of the service. Must be a binary.
  • pid - The pid that provides the service

Examples

iex> Dispatch.Registry.remove_service("downloader", self())
{:ok, "g20AAAAI4oU3ICYcsoQ="}

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

Examples

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