Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate $callers field for callback functions #397

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/cachex/services/courier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
Expand All @@ -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]})}}
Expand All @@ -76,6 +78,7 @@ defmodule Cachex.Services.Courier do

worker =
spawn_link(fn ->
Process.put(:"$callers", callers)
result =
try do
task.()
Expand Down
Loading