Skip to content

Elixir node registration and discovery using redis

License

Notifications You must be signed in to change notification settings

jeffutter/rediscovery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ReDiscovery

Node service discovery using a Redis backend.

Usage

Listener

Rediscovery requires a listener. This module gets called whenever node changes are detected. The module must provide the following callbacks:

  • list/0 - This returns the list of nodes that the listener knows about.
  • add/1 - This receives [{node, metadata}] whenever Rediscovery detects that a node has been added.
  • remove/1 - This receives [node] whenever Rediscovery detects that a node should be removed.

An example listener could look like the following:

defmodule MyApp.Listener do
  use Rediscovery.Listener
  
  @impl true
  def list do
    Node.list()
  end
  
  @impl true
  def add(nodes) do
    Enum.each(nodes, fn {node, _metadata} ->
      Node.connect(node)
    end
  end
  
  @impl true
  def remove(nodes) do
    Enum.each(&Node.disconnect/1)
  end
end

Supervision Tree

Add Rediscovery, followed by any Listener modules to your supervision tree (ideally near the end to make sure other processes that your app requires are started):

children = [
  {Rediscovery, [
    host: "my.redis-host.com",
    port: 6379,
    prefix: "my_app:my_environment",
  ]},
  MyApp.Listener
]

See lib/rediscovery.ex for other options for Rediscovery.

About

Elixir node registration and discovery using redis

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages