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 withDispatch.Registry.start_link/1
andDispatch.Supervisor.start_hash_ring/2
. Defaults tofalse
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 a service to the registry. The service is set as online.
type
- The type of the service. Can be any elixir termpid
- The pid that provides the service
Examples
iex> Dispatch.Registry.add_service(:downloader, self())
{:ok, "g20AAAAIlB7XfDdRhmk="}
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 termpid
- The pid that provides the service
Examples
iex> Dispatch.Registry.disable_service(:downloader, self())
{:ok, "g20AAAAI4oU3ICYcsoQ="}
Set a service as online. When a service is online it can be used.
type
- The type of the service. Can be any elixir termpid
- The pid that provides the service
Examples
iex> Dispatch.Registry.enable_service(:downloader, self())
{:ok, "g20AAAAI9+IQ28ngDfM="}
Find a list of count
service instances to use for a particular key
count
- The number of service instances to retrievetype
- The type of services to retrievekey
- 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 service to retrievekey
- 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>}
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}}]
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 a service from the registry.
type
- The type of the service. Can be any elixir termpid
- 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>}