Is Node.list or :erlang.nodes expensive?

Hi,

I have a simple Elixir module with a public set of functions that must behave differently depending on whether the node is connected or not to an specific remote node. Example:

  def get_agents do
    if connected_to_agenthub?() do
      # do something
    else
      # do something else
    end
  end

  defp connected_to_agenthub? do
    Node.list()
    |> Enum.any?(&is_agenthub/1)
  end

  defp is_agenthub(node) do
    node
    |> Atom.to_string()
    |> String.starts_with?("agenthub")
  end

Question: is Node.list (or :erlang.nodes) expensive?

Should I wrap my code in a GenServer, subscribe to node status changes (:net_kernel.monitor_nodes) and keep the connection state myself?

Best regards

No, it is cheap, because it works similarly to what your gen_server would do.

2 Likes