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
Current section
Files
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, call_typed_with_timeout/8, call_typed/7]).
-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()} |
{decode_failed, distribute@codec:decode_error()} |
{not_binary, binary()}.
-type atom_() :: any().
-file("src/distribute/remote_call.gleam", 64).
?DOC(
" Perform a remote procedure call on a node.\n"
" Calls module:function(args) on the given node.\n"
"\n"
" @deprecated Use `call_typed` or `call_typed_with_timeout` with codecs for type-safe RPC.\n"
" This function bypasses all type checking and encoding validation.\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", 91).
?DOC(
"\n"
" Uses a default timeout of 5000ms\n"
" Call with an explicit timeout in milliseconds\n"
"\n"
" @deprecated Use `call_typed_with_timeout` with codecs for type-safe RPC.\n"
" This function bypasses all type checking and encoding validation.\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.
-file("src/distribute/remote_call.gleam", 152).
?DOC(
" Perform a typed remote procedure call with an explicit timeout.\n"
" The remote function must return envelope-wrapped binary data.\n"
" The result is decoded using the provided `Decoder` with tag and version validation.\n"
).
-spec call_typed_with_timeout(
binary(),
binary(),
binary(),
list(gleam@dynamic:dynamic_()),
fun((bitstring()) -> {ok, IOZ} | {error, distribute@codec:decode_error()}),
binary(),
integer(),
integer()
) -> {ok, IOZ} | {error, rpc_error()}.
call_typed_with_timeout(
Node,
Module,
Function,
Args,
Decoder,
Expected_tag,
Expected_version,
Timeout_ms
) ->
Res = rpc_ffi:call_binary_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 ->
case erlang:is_binary(Res) of
false ->
{error, {not_binary, <<"RPC result is not binary"/utf8>>}};
true ->
Binary = gleam_stdlib:identity(Res),
case distribute@codec:receive_with_decoder(
Decoder,
Expected_tag,
Expected_version,
Binary
) of
{ok, Value} ->
{ok, Value};
{error, Decode_error} ->
{error, {decode_failed, Decode_error}}
end
end
end.
-file("src/distribute/remote_call.gleam", 128).
?DOC(
" Perform a typed remote procedure call on a node.\n"
" The remote function must return envelope-wrapped binary data.\n"
" The result is decoded using the provided `Decoder` with tag and version validation.\n"
"\n"
" Uses a default timeout of 5000ms.\n"
).
-spec call_typed(
binary(),
binary(),
binary(),
list(gleam@dynamic:dynamic_()),
fun((bitstring()) -> {ok, IOU} | {error, distribute@codec:decode_error()}),
binary(),
integer()
) -> {ok, IOU} | {error, rpc_error()}.
call_typed(
Node,
Module,
Function,
Args,
Decoder,
Expected_tag,
Expected_version
) ->
call_typed_with_timeout(
Node,
Module,
Function,
Args,
Decoder,
Expected_tag,
Expected_version,
5000
).