0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Ensure the listening socket gets closed with the listener supervisor.

Moved the Transport:listen call to cowboy_acceptors_sup. This make it
depend on a child of cowboy_listener_sup instead of cowboy_sup, which
isn't getting shut down when calling cowboy:stop_listener/1 and thus
didn't close the listening socket.
This commit is contained in:
Loïc Hoguin 2011-04-03 01:44:52 +02:00
parent 718baffa3c
commit ccf7a242b4
2 changed files with 14 additions and 26 deletions

View file

@ -25,14 +25,15 @@
-spec start_link(NbAcceptors::non_neg_integer(), Transport::module(),
TransOpts::term(), Protocol::module(), ProtoOpts::term(), ReqsPid::pid())
-> {ok, Pid::pid()}.
start_link(LSocket, NbAcceptors, Transport, Protocol, ProtoOpts, ReqsPid) ->
supervisor:start_link(?MODULE, [LSocket, NbAcceptors,
Transport, Protocol, ProtoOpts, ReqsPid]).
start_link(NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts, ReqsPid) ->
supervisor:start_link(?MODULE, [NbAcceptors,
Transport, TransOpts, Protocol, ProtoOpts, ReqsPid]).
%% supervisor.
-spec init(list(term())) -> term(). %% @todo These specs should be improved.
init([LSocket, NbAcceptors, Transport, Protocol, ProtoOpts, ReqsPid]) ->
init([NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts, ReqsPid]) ->
{ok, LSocket} = Transport:listen(TransOpts),
Procs = [{{acceptor, self(), N}, {cowboy_acceptor, start_link, [
LSocket, Transport, Protocol, ProtoOpts, ReqsPid
]}, permanent, brutal_kill, worker, dynamic}

View file

@ -24,12 +24,15 @@
TransOpts::term(), Protocol::module(), ProtoOpts::term())
-> {ok, Pid::pid()}.
start_link(NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts) ->
case Transport:listen(TransOpts) of
{ok, LSocket} ->
start_sup(LSocket, NbAcceptors, Transport, Protocol, ProtoOpts);
{error, Reason} ->
{error, Reason}
end.
{ok, SupPid} = supervisor:start_link(?MODULE, []),
{ok, ReqsPid} = supervisor:start_child(SupPid,
{cowboy_requests_sup, {cowboy_requests_sup, start_link, []},
permanent, 5000, supervisor, [cowboy_requests_sup]}),
{ok, _PoolPid} = supervisor:start_child(SupPid,
{cowboy_acceptors_sup, {cowboy_acceptors_sup, start_link, [
NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts, ReqsPid
]}, permanent, 5000, supervisor, [cowboy_acceptors_sup]}),
{ok, SupPid}.
%% supervisor.
@ -37,19 +40,3 @@ start_link(NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts) ->
-spec init([]) -> term().
init([]) ->
{ok, {{one_for_one, 0, 1}, []}}.
%% Internal.
-spec start_sup(NbAcceptors::non_neg_integer(), Transport::module(),
TransOpts::term(), Protocol::module(), ProtoOpts::term())
-> {ok, Pid::pid()}.
start_sup(LSocket, NbAcceptors, Transport, Protocol, ProtoOpts) ->
{ok, SupPid} = supervisor:start_link(?MODULE, []),
{ok, ReqsPid} = supervisor:start_child(SupPid,
{cowboy_requests_sup, {cowboy_requests_sup, start_link, []},
permanent, 5000, supervisor, [cowboy_requests_sup]}),
{ok, _PoolPid} = supervisor:start_child(SupPid,
{cowboy_acceptors_sup, {cowboy_acceptors_sup, start_link, [
LSocket, NbAcceptors, Transport, Protocol, ProtoOpts, ReqsPid
]}, permanent, 5000, supervisor, [cowboy_acceptors_sup]}),
{ok, SupPid}.