Current section

Files

Jump to
glisten src glisten.erl
Raw

src/glisten.erl

-module(glisten).
-compile(no_auto_import).
-export([serve/2]).
-export_type([start_error/0]).
-type start_error() :: listener_closed |
listener_timeout |
acceptor_timeout |
{acceptor_failed, gleam@erlang@process:exit_reason()} |
{acceptor_crashed, gleam@dynamic:dynamic()}.
-spec serve(
integer(),
fun((glisten@tcp:listen_socket()) -> glisten@tcp:acceptor_pool(any()))
) -> {ok, nil} | {error, start_error()}.
serve(Port, With_pool) ->
case begin
_pipe = Port,
_pipe@1 = glisten@tcp:listen(_pipe, []),
_pipe@2 = gleam@result:map_error(_pipe@1, fun(Err) -> case Err of
closed ->
listener_closed;
timeout ->
listener_timeout
end end),
gleam@result:then(
_pipe@2,
fun(Socket) ->
_pipe@3 = Socket,
_pipe@4 = With_pool(_pipe@3),
_pipe@5 = glisten@tcp:start_acceptor_pool(_pipe@4),
gleam@result:map_error(_pipe@5, fun(Err@1) -> case Err@1 of
init_timeout ->
acceptor_timeout;
{init_failed, Reason} ->
{acceptor_failed, Reason};
{init_crashed, Reason@1} ->
{acceptor_crashed, Reason@1}
end end)
end
)
end of
{error, _try} -> {error, _try};
{ok, _@1} ->
{ok, nil}
end.