forked from bitwalker/libcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow Epmd strategy to reconnect after connection failures (bitwalker…
…#183) Co-authored-by: Paul Schoenfelder <[email protected]>
- Loading branch information
1 parent
d908239
commit fcf8a4c
Showing
3 changed files
with
86 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,21 +10,56 @@ defmodule Cluster.Strategy.Epmd do | |
epmd_example: [ | ||
strategy: #{__MODULE__}, | ||
config: [ | ||
timeout: 30_000, | ||
hosts: [:"[email protected]", :"[email protected]"]]]] | ||
An optional timeout can be specified in the config. This is the timeout that | ||
will be used in the GenServer to connect the nodes. This defaults to | ||
`:infinity` meaning that the connection process will only happen when the | ||
worker is started. Any integer timeout will result in the connection process | ||
being triggered. In the example above, it has been configured for 30 seconds. | ||
""" | ||
use GenServer | ||
use Cluster.Strategy | ||
|
||
alias Cluster.Strategy.State | ||
|
||
@impl true | ||
def start_link([%State{config: config} = state]) do | ||
case Keyword.get(config, :hosts, []) do | ||
[] -> | ||
:ignore | ||
|
||
nodes when is_list(nodes) -> | ||
Cluster.Strategy.connect_nodes(state.topology, state.connect, state.list_nodes, nodes) | ||
:ignore | ||
GenServer.start_link(__MODULE__, [state]) | ||
end | ||
end | ||
|
||
@impl true | ||
def init([state]) do | ||
connect_hosts(state) | ||
{:ok, state, configured_timeout(state)} | ||
end | ||
|
||
@impl true | ||
def handle_info(:timeout, state) do | ||
handle_info(:connect, state) | ||
end | ||
|
||
def handle_info(:connect, state) do | ||
connect_hosts(state) | ||
{:noreply, state, configured_timeout(state)} | ||
end | ||
|
||
@spec configured_timeout(State.t()) :: integer() | :infinity | ||
defp configured_timeout(%State{config: config}) do | ||
Keyword.get(config, :timeout, :infinity) | ||
end | ||
|
||
@spec connect_hosts(State.t()) :: State.t() | ||
defp connect_hosts(%State{config: config} = state) do | ||
nodes = Keyword.get(config, :hosts, []) | ||
Cluster.Strategy.connect_nodes(state.topology, state.connect, state.list_nodes, nodes) | ||
state | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters