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

@type callback() :: {module(), atom(), [term()]}

Task callback

@type node_callback() :: {node(), 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

Link to this function

call(supervisor, node, mod, fun, args, timeout \\ 5000)

View Source
@spec call(task_sup(), node(), module(), atom(), [term()], timeout()) ::
  term() | {:badrpc, term()}

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"
Link to this function

multi_call(supervisor, node_group, opts \\ [])

View Source
@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 of 5000. It uses Task.yield_many/2 internally.

  • :reducer - Reducer function to be executed on each collected result. (check out reducer 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"]
Link to this function

multi_call(supervisor, nodes, mod, fun, args, opts \\ [])

View Source
@spec multi_call(task_sup(), [node()], module(), atom(), [term()], Keyword.t()) ::
  term()

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"]
Link to this function

rpc_multi_call(supervisor, node_group, opts)

View Source
Link to this function

rpc_multi_call(supervisor, nodes, mod, fun, args, opts)

View Source