Current section

Files

Jump to
distribute src distribute@actor.erl
Raw

src/distribute@actor.erl

-module(distribute@actor).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/distribute/actor.gleam").
-export([start/3, child_spec/3, start_registered/3, start_supervised/3, pool/4]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/distribute/actor.gleam", 17).
?DOC(" Start a named actor.\n").
-spec start(
distribute@registry:typed_name(GPR),
GPT,
fun((GPR, GPT) -> distribute@receiver:next(GPT))
) -> {ok, distribute@global:global_subject(GPR)} |
{error, gleam@otp@actor:start_error()}.
start(Typed_name, Initial_state, Handler) ->
case distribute@receiver:start_distributed_worker(
distribute@registry:typed_name_to_string(Typed_name),
Initial_state,
distribute@registry:typed_name_encoder(Typed_name),
distribute@registry:typed_name_decoder(Typed_name),
Handler
) of
{ok, Started} ->
{ok, erlang:element(3, Started)};
{error, Err} ->
{error, Err}
end.
-file("src/distribute/actor.gleam", 37).
?DOC(" OTP child spec for a named actor.\n").
-spec child_spec(
distribute@registry:typed_name(GPY),
GQA,
fun((GPY, GQA) -> distribute@receiver:next(GQA))
) -> gleam@otp@supervision:child_specification(distribute@global:global_subject(GPY)).
child_spec(Typed_name, Initial_state, Handler) ->
gleam@otp@supervision:worker(
fun() ->
distribute@receiver:start_distributed_worker(
distribute@registry:typed_name_to_string(Typed_name),
Initial_state,
distribute@registry:typed_name_encoder(Typed_name),
distribute@registry:typed_name_decoder(Typed_name),
Handler
)
end
).
-file("src/distribute/actor.gleam", 55).
?DOC(
" Start an actor and register it globally. Kills the actor if\n"
" registration fails.\n"
).
-spec start_registered(
distribute@registry:typed_name(GQE),
GQG,
fun((GQE, GQG) -> distribute@receiver:next(GQG))
) -> {ok, distribute@global:global_subject(GQE)} |
{error, distribute@registry:register_error()}.
start_registered(Typed_name, Initial_state, Handler) ->
Name = distribute@registry:typed_name_to_string(Typed_name),
case distribute@receiver:start_distributed_worker(
Name,
Initial_state,
distribute@registry:typed_name_encoder(Typed_name),
distribute@registry:typed_name_decoder(Typed_name),
Handler
) of
{ok, Started} ->
case distribute@registry:register_global(
Typed_name,
erlang:element(3, Started)
) of
{ok, _} ->
{ok, erlang:element(3, Started)};
{error, Err} ->
gleam@erlang@process:kill(erlang:element(2, Started)),
{error, Err}
end;
{error, _} ->
{error, {registration_failed, <<"actor failed to start"/utf8>>}}
end.
-file("src/distribute/actor.gleam", 85).
?DOC(" Start a supervised actor that auto-registers on (re)start.\n").
-spec start_supervised(
distribute@registry:typed_name(GQL),
GQN,
fun((GQL, GQN) -> distribute@receiver:next(GQN))
) -> {ok, gleam@erlang@process:pid_()} | {error, gleam@otp@actor:start_error()}.
start_supervised(Typed_name, Initial_state, Handler) ->
Name = distribute@registry:typed_name_to_string(Typed_name),
Spec = gleam@otp@supervision:worker(
fun() ->
case distribute@receiver:start_distributed_worker(
Name,
Initial_state,
distribute@registry:typed_name_encoder(Typed_name),
distribute@registry:typed_name_decoder(Typed_name),
Handler
) of
{ok, Started} ->
_ = distribute@registry:unregister(Name),
_ = distribute@registry:register_global(
Typed_name,
erlang:element(3, Started)
),
{ok, Started};
{error, Err} ->
{error, Err}
end
end
),
Builder = begin
_pipe = gleam@otp@static_supervisor:new(one_for_one),
gleam@otp@static_supervisor:add(_pipe, Spec)
end,
case gleam@otp@static_supervisor:start(Builder) of
{ok, Sup} ->
{ok, erlang:element(2, Sup)};
{error, Err@1} ->
{error, Err@1}
end.
-file("src/distribute/actor.gleam", 123).
?DOC(" Start N supervised actors, registered as name_1 .. name_N.\n").
-spec pool(
distribute@registry:typed_name(GQR),
integer(),
GQT,
fun((GQR, GQT) -> distribute@receiver:next(GQT))
) -> {ok, gleam@erlang@process:pid_()} | {error, gleam@otp@actor:start_error()}.
pool(Typed_name, Size, Initial_state, Handler) ->
Name_prefix = distribute@registry:typed_name_to_string(Typed_name),
Specs = begin
_pipe = gleam@list:range(1, Size),
gleam@list:map(
_pipe,
fun(I) ->
Worker_name = <<<<Name_prefix/binary, "_"/utf8>>/binary,
(erlang:integer_to_binary(I))/binary>>,
Worker_tn = distribute@registry:pool_member(Typed_name, I),
gleam@otp@supervision:worker(
fun() ->
case distribute@receiver:start_distributed_worker(
Worker_name,
Initial_state,
distribute@registry:typed_name_encoder(Typed_name),
distribute@registry:typed_name_decoder(Typed_name),
Handler
) of
{ok, Started} ->
_ = distribute@registry:unregister(Worker_name),
_ = distribute@registry:register_global(
Worker_tn,
erlang:element(3, Started)
),
{ok, Started};
{error, Err} ->
{error, Err}
end
end
)
end
)
end,
Builder = gleam@list:fold(
Specs,
gleam@otp@static_supervisor:new(one_for_one),
fun(Acc, Spec) -> gleam@otp@static_supervisor:add(Acc, Spec) end
),
case gleam@otp@static_supervisor:start(Builder) of
{ok, Sup} ->
{ok, erlang:element(2, Sup)};
{error, Err@1} ->
{error, Err@1}
end.