From b767c9ae92b5943ce219174125947d33ee50826f Mon Sep 17 00:00:00 2001 From: Thomas Letan Date: Thu, 8 Mar 2018 21:34:16 +0100 Subject: refactor: Rely on the Instance to notify puppeteers Previously, we were using a Registry with duplicated keys, but they are not very error-friendly. In particular, the documentation states: > For example, if a process crashes, its keys are automatically > removed from the registry but the change may not propagate > immediately. In lkn, we rely on keys rather than PID to identify and reach processes. The Instance already remembers its registered Puppeteers key. As a consequence, we can rely on the Instance to dispatch notification, we do not need the Registry. --- lib/lkn/core.ex | 6 ------ lib/lkn/core/instance.ex | 19 ++++++++++--------- lib/lkn/core/pool.ex | 6 +----- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/lib/lkn/core.ex b/lib/lkn/core.ex index 2719c4a..0033ecd 100644 --- a/lib/lkn/core.ex +++ b/lib/lkn/core.ex @@ -52,11 +52,6 @@ defmodule Lkn.Core do {:via, Registry, {Lkn.Core.Registry, {:engine, instance_key, sys}}} end - @spec notify_group(Instance.k) :: t - def notify_group(instance_key) do - {:engine, instance_key, :notifier} - end - @spec instance(Instance.k) :: t def instance(instance_key) do {:via, Registry, {Lkn.Core.Registry, {:instance, instance_key}}} @@ -94,7 +89,6 @@ defmodule Lkn.Core do def init(_) do children = [ supervisor(Registry, [:unique, Lkn.Core.Registry], id: :core_registry), - supervisor(Registry, [:duplicate, Lkn.Core.Notifier], id: :core_notifier), supervisor(Lkn.Core.Pool.Supervisor, []) ] diff --git a/lib/lkn/core/instance.ex b/lib/lkn/core/instance.ex index 40d5f9a..6174998 100644 --- a/lib/lkn/core/instance.ex +++ b/lib/lkn/core/instance.ex @@ -274,8 +274,6 @@ defmodule Lkn.Core.Instance do @spec unregister_puppeteer(k, Puppeteer.k) :: :ok def unregister_puppeteer(instance_key, puppeteer_key) do GenServer.cast(Name.instance(instance_key), {:unregister_puppeteer, puppeteer_key}) - - Registry.unregister(Lkn.Core.Notifier, Name.notify_group(instance_key)) end def handle_call(:lock, _from, state) do @@ -284,7 +282,7 @@ defmodule Lkn.Core.Instance do def handle_call({:register_puppeteer, puppeteer_key, puppeteer_module}, _from, state) do if !State.closed?(state) do # we remember this new puppeteer - s2 = State.register_puppeteer(state, puppeteer_key, puppeteer_module) + state = State.register_puppeteer(state, puppeteer_key, puppeteer_module) # we compute a digest of the map and each puppets map = Lkn.Core.Entity.digest(state.map_key) @@ -299,7 +297,7 @@ defmodule Lkn.Core.Instance do puppets ) - {:reply, true, s2} + {:reply, true, state} else {:reply, false, state} end @@ -380,12 +378,15 @@ defmodule Lkn.Core.Instance do def handle_cast({:unregister_puppeteer, puppeteer_key}, state) do {:noreply, State.unregister_puppeteer(state, puppeteer_key)} end + def handle_cast({:notify_group, notif}, state) do + Enum.map(state.puppeteers, fn {key, _} -> + notif.(key) + end) + + {:noreply, state} + end def notify_puppeteers(instance_key, notif) do - Registry.dispatch(Lkn.Core.Notifier, Name.notify_group(instance_key), fn entries -> - for {_, key} <- entries do - notif.(key) - end - end) + GenServer.cast(Name.instance(instance_key), {:notify_group, notif}) end end diff --git a/lib/lkn/core/pool.ex b/lib/lkn/core/pool.ex index aabef1d..686f18a 100644 --- a/lib/lkn/core/pool.ex +++ b/lib/lkn/core/pool.ex @@ -176,11 +176,7 @@ defmodule Lkn.Core.Pool do """ @spec register_puppeteer(Lkn.Core.Map.k, Lkn.Core.Puppeteer.k, Lkn.Core.Puppeteer.m) :: Lkn.Core.Instance.k def register_puppeteer(map_key, puppeteer_key, puppeteer_module) do - instance_key = GenServer.call(Name.pool(map_key), {:register, puppeteer_key, puppeteer_module}) - - Registry.register(Lkn.Core.Notifier, Name.notify_group(instance_key), puppeteer_key) - - instance_key + GenServer.call(Name.pool(map_key), {:register, puppeteer_key, puppeteer_module}) end @doc """ -- cgit v1.2.3