View Source Gnat.ConnectionSupervisor (gnat v1.9.1)
A process that can supervise a named connection for you
If you would like to supervise a Gnat connection and have it automatically re-connect in case of failure you can use this module in your supervision tree. It takes a map with the following data:
gnat_supervisor_settings = %{
name: :gnat, # (required) the registered named you want to give the Gnat connection
backoff_period: 4_000, # number of milliseconds to wait between consecutive reconnect attempts (default: 2_000)
connection_settings: [
%{host: '10.0.0.100', port: 4222},
%{host: '10.0.0.101', port: 4222},
]
}
The connection settings can specify all of the same values that you pass to Gnat.start_link/1
. Each time a connection is attempted we will use one of the provided connection settings to open the connection. This is a simplistic way of load balancing your connections across a cluster of nats nodes and allowing failover to other nodes in the cluster if one goes down.
To use this in your supervision tree add an entry like this:
import Supervisor.Spec
worker(Gnat.ConnectionSupervisor, [gnat_supervisor_settings, [name: :my_connection_supervisor]])
The second argument is used as GenServer options so you can give the supervisor a registered name as well if you like. Now in the rest of your code you can call things like:
:ok = Gnat.pub(:gnat, "subject", "message")
And it will use your supervised connection. If the connection is down when you call that function (or dies during that function) it will raise an error.
Summary
Functions
Returns a specification to start this module under a supervisor.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor
.
@spec start_link( map(), keyword() ) :: GenServer.on_start()