Packages

Typed distributed messaging for Gleam on the BEAM.

Retired package: Deprecated - The project needs to be redesigned around a much smaller and clearer core.

Current section

Files

Jump to
distribute src distribute@remote_call.erl
Raw

src/distribute@remote_call.erl

-module(distribute@remote_call).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/distribute/remote_call.gleam").
-export([call/4, call_with_timeout/5]).
-export_type([rpc_error/0, atom_/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type rpc_error() :: rpc_timeout |
{rpc_bad_rpc, binary()} |
{rpc_failed, binary()}.
-type atom_() :: any().
-file("src/distribute/remote_call.gleam", 38).
?DOC(
" Perform a remote procedure call on a node.\n"
" Calls module:function(args) on the given node.\n"
).
-spec call(binary(), binary(), binary(), list(gleam@dynamic:dynamic_())) -> {ok,
gleam@dynamic:dynamic_()} |
{error, rpc_error()}.
call(Node, Module, Function, Args) ->
Res = rpc_ffi:call_with_timeout(
rpc_ffi:to_atom(Node),
rpc_ffi:to_atom(Module),
rpc_ffi:to_atom(Function),
Args,
5000
),
case rpc_ffi:is_badrpc(Res) of
true ->
{error, {rpc_bad_rpc, rpc_ffi:get_badrpc_reason(Res)}};
false ->
{ok, Res}
end.
-file("src/distribute/remote_call.gleam", 62).
?DOC(
"\n"
" Uses a default timeout of 5000ms\n"
" Call with an explicit timeout in milliseconds\n"
).
-spec call_with_timeout(
binary(),
binary(),
binary(),
list(gleam@dynamic:dynamic_()),
integer()
) -> {ok, gleam@dynamic:dynamic_()} | {error, rpc_error()}.
call_with_timeout(Node, Module, Function, Args, Timeout_ms) ->
Res = rpc_ffi:call_with_timeout(
rpc_ffi:to_atom(Node),
rpc_ffi:to_atom(Module),
rpc_ffi:to_atom(Function),
Args,
Timeout_ms
),
case rpc_ffi:is_badrpc(Res) of
true ->
Reason = rpc_ffi:get_badrpc_reason(Res),
case Reason =:= <<"timeout"/utf8>> of
true ->
{error, rpc_timeout};
false ->
{error, {rpc_bad_rpc, Reason}}
end;
false ->
{ok, Res}
end.