Skip to content

Commit 6be4803

Browse files
committed
fix: special case for local abcast
1 parent 501ca32 commit 6be4803

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

lib/realtime/gen_rpc.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,15 @@ defmodule Realtime.GenRpc do
2525
@spec abcast([node], atom, any, keyword()) :: :ok
2626
def abcast(nodes, name, msg, opts) when is_list(nodes) and is_atom(name) and is_list(opts) do
2727
key = Keyword.get(opts, :key, nil)
28-
nodes = cast_rpc_nodes(nodes, key)
2928

30-
:gen_rpc.abcast(nodes, name, msg)
29+
{local, remote} = Enum.split_with(nodes, &(&1 == node()))
30+
31+
if local != [], do: send({name, node()}, msg)
32+
33+
remote
34+
|> cast_rpc_nodes(key)
35+
|> :gen_rpc.abcast(name, msg)
36+
3137
:ok
3238
end
3339

test/realtime/gen_rpc_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,20 @@ defmodule Realtime.GenRpcTest do
193193
refute_receive _any
194194
end
195195

196+
test "abcast to registered process on the local node" do
197+
name =
198+
System.unique_integer()
199+
|> to_string()
200+
|> String.to_atom()
201+
202+
:erlang.register(name, self())
203+
204+
assert GenRpc.abcast([node()], name, "a message", []) == :ok
205+
206+
assert_receive "a message"
207+
refute_receive _any
208+
end
209+
196210
@tag extra_config: [{:gen_rpc, :tcp_server_port, 9999}]
197211
test "tcp error" do
198212
Logger.put_process_level(self(), :debug)

0 commit comments

Comments
 (0)