From bcfc9d8e35b786a61ac32cf7d0cf2a34daab52dd Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Thu, 2 Nov 2023 16:51:12 -0400 Subject: [PATCH] Support SRV records Priority is not taken into consideration at this time and ports are ignored as there is no mechanism to specify the port used when connecting to a node with net_kernel:connect_node/1 --- lib/dns_cluster.ex | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/dns_cluster.ex b/lib/dns_cluster.ex index 8f59f4c..b2c104e 100644 --- a/lib/dns_cluster.ex +++ b/lib/dns_cluster.ex @@ -38,6 +38,11 @@ defmodule DNSCluster do def list_nodes, do: Node.list(:visible) + def lookup(query, type = :srv) when is_binary(query) do + :inet_res.lookup(~c"#{query}", :in, type) + |> Enum.map(&elem(&1, 3)) + end + def lookup(query, type) when is_binary(query) and type in [:a, :aaaa] do :inet_res.lookup(~c"#{query}", :in, type) end @@ -145,10 +150,13 @@ defmodule DNSCluster do end defp discover_ips(%{resolver: resolver, query: query}) do - [:a, :aaaa] + [:a, :aaaa, :srv] |> Enum.flat_map(&resolver.lookup(query, &1)) |> Enum.uniq() - |> Enum.map(&to_string(:inet.ntoa(&1))) + |> Enum.map(fn + [_] = result -> to_string(result) + result -> to_string(:inet.ntoa(result)) + end) end defp warn_on_invalid_dist do