aboutsummaryrefslogtreecommitdiffstats
path: root/src/ranch_acceptor.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/ranch_acceptor.erl')
-rw-r--r--src/ranch_acceptor.erl94
1 files changed, 18 insertions, 76 deletions
diff --git a/src/ranch_acceptor.erl b/src/ranch_acceptor.erl
index 63d24c8..44cf52d 100644
--- a/src/ranch_acceptor.erl
+++ b/src/ranch_acceptor.erl
@@ -16,89 +16,31 @@
-module(ranch_acceptor).
%% API.
--export([start_link/6]).
+-export([start_link/3]).
%% Internal.
--export([init/7]).
--export([loop/7]).
+-export([loop/3]).
%% API.
--spec start_link(any(), inet:socket(), module(), module(), pid(), pid())
+-spec start_link(inet:socket(), module(), pid())
-> {ok, pid()}.
-start_link(Ref, LSocket, Transport, Protocol, ListenerPid, ConnsSup) ->
- {ok, MaxConns} = ranch_listener:get_max_connections(ListenerPid),
- {ok, Opts} = ranch_listener:get_protocol_options(ListenerPid),
- Pid = spawn_link(?MODULE, init,
- [LSocket, Transport, Protocol, MaxConns, Opts, ListenerPid, ConnsSup]),
- ok = ranch_server:add_acceptor(Ref, Pid),
+start_link(LSocket, Transport, ConnsSup) ->
+ Pid = spawn_link(?MODULE, loop, [LSocket, Transport, ConnsSup]),
{ok, Pid}.
%% Internal.
--spec init(inet:socket(), module(), module(),
- non_neg_integer(), any(), pid(), pid()) -> no_return().
-init(LSocket, Transport, Protocol, MaxConns, Opts, ListenerPid, ConnsSup) ->
- async_accept(LSocket, Transport),
- loop(LSocket, Transport, Protocol, MaxConns, Opts, ListenerPid, ConnsSup).
-
--spec loop(inet:socket(), module(), module(),
- ranch:max_conns(), any(), pid(), pid()) -> no_return().
-loop(LSocket, Transport, Protocol, MaxConns, Opts, ListenerPid, ConnsSup) ->
- receive
- %% We couldn't accept the socket but it's safe to continue.
- {accept, continue} ->
- ?MODULE:init(LSocket, Transport, Protocol,
- MaxConns, Opts, ListenerPid, ConnsSup);
- %% Found my sockets!
- {accept, CSocket} ->
- {ok, ConnPid} = supervisor:start_child(ConnsSup,
- [ListenerPid, CSocket, Transport, Protocol, Opts]),
- Transport:controlling_process(CSocket, ConnPid),
- ConnPid ! {shoot, ListenerPid},
- {ok, MaxConns2} = case MaxConns of
- infinity ->
- {ok, infinity};
- _ ->
- NbConns = ranch_listener:add_connection(ListenerPid, ConnPid),
- maybe_wait(ListenerPid, MaxConns, NbConns)
- end,
- ?MODULE:init(LSocket, Transport, Protocol,
- MaxConns2, Opts, ListenerPid, ConnsSup);
- %% Upgrade the max number of connections allowed concurrently.
- {set_max_conns, MaxConns2} ->
- ?MODULE:loop(LSocket, Transport, Protocol,
- MaxConns2, Opts, ListenerPid, ConnsSup);
- %% Upgrade the protocol options.
- {set_opts, Opts2} ->
- ?MODULE:loop(LSocket, Transport, Protocol,
- MaxConns, Opts2, ListenerPid, ConnsSup)
- end.
-
--spec maybe_wait(pid(), MaxConns, non_neg_integer())
- -> {ok, MaxConns} when MaxConns::ranch:max_conns().
-maybe_wait(_, MaxConns, NbConns) when MaxConns > NbConns ->
- {ok, MaxConns};
-maybe_wait(ListenerPid, MaxConns, NbConns) ->
- receive
- {set_max_conns, MaxConns2} ->
- maybe_wait(ListenerPid, MaxConns2, NbConns)
- after 0 ->
- NbConns2 = ranch_server:count_connections(ListenerPid),
- maybe_wait(ListenerPid, MaxConns, NbConns2)
- end.
-
--spec async_accept(inet:socket(), module()) -> ok.
-async_accept(LSocket, Transport) ->
- AcceptorPid = self(),
- _ = spawn_link(fun() ->
- case Transport:accept(LSocket, infinity) of
- {ok, CSocket} ->
- Transport:controlling_process(CSocket, AcceptorPid),
- AcceptorPid ! {accept, CSocket};
- %% We want to crash if the listening socket got closed.
- {error, Reason} when Reason =/= closed ->
- AcceptorPid ! {accept, continue}
- end
- end),
- ok.
+-spec loop(inet:socket(), module(), pid()) -> no_return().
+loop(LSocket, Transport, ConnsSup) ->
+ _ = case Transport:accept(LSocket, infinity) of
+ {ok, CSocket} ->
+ Transport:controlling_process(CSocket, ConnsSup),
+ %% This call will not return until process has been started
+ %% AND we are below the maximum number of connections.
+ ranch_conns_sup:start_protocol(ConnsSup, CSocket);
+ %% We want to crash if the listening socket got closed.
+ {error, Reason} when Reason =/= closed ->
+ ok
+ end,
+ ?MODULE:loop(LSocket, Transport, ConnsSup).