View Source Nebulex.RPC (Nebulex v2.6.4)
RPC utilities for distributed task execution.
This module uses supervised tasks underneath Task.Supervisor
.
NOTE: The approach by using distributed tasks will be deprecated in the future in favor of
:erpc
.
Summary
Types
Task callback
Group entry: node -> callback
Node group
Reducer spec
Reducer function spec
Task supervisor
Functions
Evaluates apply(mod, fun, args)
on node node
and returns the corresponding
evaluation result, or {:badrpc, reason}
if the call fails.
In contrast to a regular single-node RPC, a multicall is an RPC that is sent
concurrently from one client to multiple servers. The function evaluates
apply(mod, fun, args)
on each node_group
entry and collects the answers.
Then, evaluates the reducer
function (set in the opts
) on each answer.
Similar to multi_call/3
but the same node_callback
(given by module
,
fun
, args
) is executed on all nodes
; Internally it creates a
node_group
with the same node_callback
for each node.
Types
Task callback
Group entry: node -> callback
@type node_group() :: %{optional(node()) => callback()} | [node_callback()]
Node group
@type reducer() :: {acc :: term(), reducer_fun()}
Reducer spec
@type reducer_fun() :: ({:ok, term()} | {:error, term()}, node_callback() | node(), term() -> term())
Reducer function spec
@type task_sup() :: Supervisor.supervisor()
Task supervisor
Functions
Evaluates apply(mod, fun, args)
on node node
and returns the corresponding
evaluation result, or {:badrpc, reason}
if the call fails.
A timeout, in milliseconds or :infinity
, can be given with a default value
of 5000
. It uses Task.await/2
internally.
Example
iex> Nebulex.RPC.call(:my_task_sup, :node1, Kernel, :to_string, [1])
"1"
@spec multi_call(task_sup(), node_group(), Keyword.t()) :: term()
In contrast to a regular single-node RPC, a multicall is an RPC that is sent
concurrently from one client to multiple servers. The function evaluates
apply(mod, fun, args)
on each node_group
entry and collects the answers.
Then, evaluates the reducer
function (set in the opts
) on each answer.
This function is similar to :rpc.multicall/5
.
Options
:timeout
- A timeout, in milliseconds or:infinity
, can be given with a default value of5000
. It usesTask.yield_many/2
internally.:reducer
- Reducer function to be executed on each collected result. (check outreducer
type).
Example
iex> Nebulex.RPC.multi_call(
...> :my_task_sup,
...> %{
...> node1: {Kernel, :to_string, [1]},
...> node2: {Kernel, :to_string, [2]}
...> },
...> timeout: 10_000,
...> reducer: {
...> [],
...> fn
...> {:ok, res}, _node_callback, acc ->
...> [res | acc]
...>
...> {:error, _}, _node_callback, acc ->
...> acc
...> end
...> }
...> )
["1", "2"]
Similar to multi_call/3
but the same node_callback
(given by module
,
fun
, args
) is executed on all nodes
; Internally it creates a
node_group
with the same node_callback
for each node.
Options
Same options as multi_call/3
.
Example
iex> Nebulex.RPC.multi_call(
...> :my_task_sup,
...> [:node1, :node2],
...> Kernel,
...> :to_string,
...> [1],
...> timeout: 5000,
...> reducer: {
...> [],
...> fn
...> {:ok, res}, _node_callback, acc ->
...> [res | acc]
...>
...> {:error, _}, _node_callback, acc ->
...> acc
...> end
...> }
...> )
["1", "1"]