NSQ.Consumer.Connections (elixir_nsq v1.2.0)
Functions for connecting, disconnecting, managing connections, etc.
Summary
Functions
When a connection is terminated or dies, we must do some extra cleanup.
First, a terminated process isn't necessarily removed from the supervisor's
list; therefore we call Supervisor.delete_child/2
. And info about this
connection like RDY must be removed so it doesn't contribute to total_rdy
.
Not for external use.
Create a connection to NSQD and add it to the consumer's supervised list. Not for external use.
Given a list of NSQD hosts, open a connection for each.
We may have open connections which nsqlookupd stops reporting. This function tells us which connections we have stored in state but not in nsqlookupd. Not for external use.
Iterate over all listed connections and delete the ones that are dead. This exists because it is difficult to reliably clean up a connection immediately after it is terminated (it might still be running). This function runs in the discovery loop to provide consistency.
Frequently, when testing, we publish a message then immediately want a consumer to process it, but this doesn't work if the consumer doesn't discover the nsqd first. Only meant for testing.
Finds and updates list of live NSQDs using either NSQ.Config.nsqlookupds or NSQ.Config.nsqds, depending on what's configured. Preference is given to nsqlookupd. Not for external use.
Initialized from NSQ.Consumer.Supervisor, sends the consumer a message on a fixed interval.
Returns all live connections for a consumer. This function, which takes a consumer's entire state as an argument, is for convenience. Not for external use.
Returns all live connections for a consumer. Used in tests. Not for external use.
When nsqlookupd reports available producers, there are some that may not already be in our connection list. This function reports which ones are new so we can connect to them.
Given a single connection, immediately terminate its process (and all descendant processes, such as message handlers) and remove its info from the ConnInfo agent. Not for external use.
Given a list of connections, force them to stop. Return the new state without those connections.
Any inactive connections will be killed and any newly discovered connections will be added. Existing connections with no change are left alone. Not for external use.
Functions
cleanup_connection(cons, conn_id, cons_state)
@spec cleanup_connection(pid(), NSQ.Consumer.host_with_port(), NSQ.Consumer.state()) :: {:ok, NSQ.Consumer.state()}
When a connection is terminated or dies, we must do some extra cleanup.
First, a terminated process isn't necessarily removed from the supervisor's
list; therefore we call Supervisor.delete_child/2
. And info about this
connection like RDY must be removed so it doesn't contribute to total_rdy
.
Not for external use.
close(cons_state)
close!(cons_state)
connect_to_nsqd(nsqd, cons, cons_state)
@spec connect_to_nsqd(NSQ.Consumer.host_with_port(), pid(), NSQ.Consumer.state()) :: {:ok, NSQ.Consumer.state()}
Create a connection to NSQD and add it to the consumer's supervised list. Not for external use.
connect_to_nsqds(nsqds, cons, cons_state \\ nil)
@spec connect_to_nsqds([NSQ.Consumer.host_with_port()], pid(), NSQ.Consumer.state()) :: {:ok, NSQ.Consumer.state()}
Given a list of NSQD hosts, open a connection for each.
count(cons_state)
@spec count(NSQ.Consumer.state()) :: integer()
dead_connections(discovered_nsqds, cons, cons_state)
@spec dead_connections([NSQ.Consumer.host_with_port()], pid(), NSQ.Consumer.state()) :: [ NSQ.Consumer.connection() ]
We may have open connections which nsqlookupd stops reporting. This function tells us which connections we have stored in state but not in nsqlookupd. Not for external use.
delete_dead(state)
@spec delete_dead(NSQ.Consumer.state()) :: {:ok, NSQ.Consumer.state()}
Iterate over all listed connections and delete the ones that are dead. This exists because it is difficult to reliably clean up a connection immediately after it is terminated (it might still be running). This function runs in the discovery loop to provide consistency.
delete_dead!(state)
discover_nsqds(sup_pid)
@spec discover_nsqds(pid()) :: :ok
Frequently, when testing, we publish a message then immediately want a consumer to process it, but this doesn't work if the consumer doesn't discover the nsqd first. Only meant for testing.
discover_nsqds_and_connect(cons, cons_state)
@spec discover_nsqds_and_connect(pid(), NSQ.Consumer.state()) :: {:ok, NSQ.Consumer.state()}
Finds and updates list of live NSQDs using either NSQ.Config.nsqlookupds or NSQ.Config.nsqds, depending on what's configured. Preference is given to nsqlookupd. Not for external use.
discovery_loop(cons)
Initialized from NSQ.Consumer.Supervisor, sends the consumer a message on a fixed interval.
get(map)
@spec get(NSQ.Consumer.state()) :: [NSQ.Consumer.connection()]
Returns all live connections for a consumer. This function, which takes a consumer's entire state as an argument, is for convenience. Not for external use.
get(cons, cons_state \\ nil)
@spec get(pid(), NSQ.Consumer.state()) :: [NSQ.Consumer.connection()]
Returns all live connections for a consumer. Used in tests. Not for external use.
idle_with_rdy(cons_state)
@spec idle_with_rdy(NSQ.Consumer.state()) :: [NSQ.Consumer.connection()]
new_nsqds(discovered_nsqds, cons, cons_state)
@spec new_nsqds([NSQ.Consumer.host_with_port()], pid(), NSQ.Consumer.state()) :: [ NSQ.Consumer.host_with_port() ]
When nsqlookupd reports available producers, there are some that may not already be in our connection list. This function reports which ones are new so we can connect to them.
reconnect_failed(state)
refresh(cons_state)
refresh!(cons_state)
stop_connection(cons, conn_id, cons_state)
@spec stop_connection(pid(), NSQ.Consumer.host_with_port(), NSQ.Consumer.state()) :: {:ok, NSQ.Consumer.state()}
Given a single connection, immediately terminate its process (and all descendant processes, such as message handlers) and remove its info from the ConnInfo agent. Not for external use.
stop_connections(dead_conns, cons, cons_state)
@spec stop_connections([NSQ.Consumer.connection()], pid(), NSQ.Consumer.state()) :: {:ok, NSQ.Consumer.state()}
Given a list of connections, force them to stop. Return the new state without those connections.
update(discovered_nsqds, cons, cons_state)
@spec update([NSQ.Consumer.host_with_port()], pid(), NSQ.Consumer.state()) :: {:ok, NSQ.Consumer.state()}
Any inactive connections will be killed and any newly discovered connections will be added. Existing connections with no change are left alone. Not for external use.