From aeb64b6297e4d36714be3e5d646e1706f32348cc Mon Sep 17 00:00:00 2001 From: Flavio Grossi Date: Mon, 8 Apr 2024 12:30:19 +0200 Subject: [PATCH] avoid a reduce by using a precise :ets.select using a custom implementation of syn's group_names function --- lib/syn_supervisor/distribution.ex | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/syn_supervisor/distribution.ex b/lib/syn_supervisor/distribution.ex index 5004059..af72466 100644 --- a/lib/syn_supervisor/distribution.ex +++ b/lib/syn_supervisor/distribution.ex @@ -161,13 +161,7 @@ defmodule SynSupervisor.Distribution do defp create_ring(scope, opts \\ []) do maybe_create_hash_ring(scope) - nodes = - scope - |> :syn.group_names() - |> Enum.reduce([], fn - {:member, node}, acc -> [node | acc] - _, acc -> acc - end) + nodes = get_nodes(scope) if Keyword.get(opts, :check_members, false) do current_nodes = MapSet.new(nodes) @@ -267,4 +261,24 @@ defmodule SynSupervisor.Distribution do defp child_group(%Child{} = c) do {:child, c} end + + defp get_nodes(scope) do + members_group_names(scope) + end + + defp members_group_names(scope) do + node_param = :_ + case :syn_backbone.get_table_name(:syn_pg_by_name, scope) do + :undefined -> + raise "cannot get table name" + table_by_name -> + duplicated_groups = :ets.select(table_by_name, [{ + {{{:"$1", :"$2"}, :_}, :_, :_, :_, node_param}, + [{:==, :"$1", :member}], + [:"$2"] + }]) + + duplicated_groups |> :ordsets.from_list() |> :ordsets.to_list() + end + end end