diff --git a/lib/cachex/services/courier.ex b/lib/cachex/services/courier.ex index 04f03fa..b0a3772 100644 --- a/lib/cachex/services/courier.ex +++ b/lib/cachex/services/courier.ex @@ -39,8 +39,10 @@ defmodule Cachex.Services.Courier do call which will wait until a result can be loaded. """ @spec dispatch(Cachex.t(), any, (-> any)) :: any - def dispatch(cache() = cache, key, task) when is_function(task, 0), - do: service_call(cache, :courier, {:dispatch, key, task, local_stack()}) + def dispatch(cache() = cache, key, task) when is_function(task, 0) do + callers = [self()] ++ Process.get(:"$callers", []) + service_call(cache, :courier, {:dispatch, key, task, local_stack(), callers}) + end #################### # Server Callbacks # @@ -64,7 +66,7 @@ defmodule Cachex.Services.Courier do # Due to the nature of the async behaviour, this call will return before # the task has been completed, and the :notify callback will receive the # results from the task after completion (regardless of outcome). - def handle_call({:dispatch, key, task, stack}, caller, {cache, tasks} = state) do + def handle_call({:dispatch, key, task, stack, callers}, caller, {cache, tasks} = state) do case Map.get(tasks, key) do {pid, listeners} -> {:noreply, {cache, Map.put(tasks, key, {pid, [caller | listeners]})}} @@ -76,6 +78,7 @@ defmodule Cachex.Services.Courier do worker = spawn_link(fn -> + Process.put(:"$callers", callers) result = try do task.()