bitwalker / libcluster

Automatic cluster formation/healing for Elixir applications
MIT License
1.97k stars 188 forks source link

Add local epmd strategy? #124

Closed rlipscombe closed 3 years ago

rlipscombe commented 4 years ago

The need to specify the node names for the epmd strategy is annoying when all the nodes are running on the same host.

So I wrote this, which uses erl_epmd.names() to find local nodes:

defmodule Cluster.Strategy.LocalEpmd do
  use Cluster.Strategy

  alias Cluster.Strategy.State

  def start_link([%State{} = state]) do
    suffix = get_host_suffix(Node.self())

    {:ok, nodes} = :erl_epmd.names()
    nodes = for {n, _} <- nodes, do: List.to_atom(n ++ suffix)
    Cluster.Strategy.connect_nodes(state.topology, state.connect, state.list_nodes, nodes)
    :ignore
  end

  defp get_host_suffix(self) do
    self = Atom.to_charlist(self)
    [_, suffix] = :string.split(self, '@')
    '@' ++ suffix
  end
end

...useful?

bitwalker commented 4 years ago

I'd be happy to merge this as a PR when you have the time to put one together :)

rlipscombe commented 4 years ago

Done.