Skip to content

Commit

Permalink
Prevent side effects in init of supervisors
Browse files Browse the repository at this point in the history
  • Loading branch information
juhlig authored and essen committed Feb 10, 2020
1 parent 4ffe0c9 commit dfd0c82
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/ranch_acceptors_sup.erl
Expand Up @@ -28,7 +28,14 @@ init([Ref, Transport, Logger]) ->
TransOpts = ranch_server:get_transport_options(Ref),
NumAcceptors = maps:get(num_acceptors, TransOpts, 10),
NumListenSockets = maps:get(num_listen_sockets, TransOpts, 1),
LSockets = start_listen_sockets(Ref, NumListenSockets, Transport, TransOpts, Logger),
LSockets = case get(lsockets) of
undefined ->
LSockets1 = start_listen_sockets(Ref, NumListenSockets, Transport, TransOpts, Logger),
put(lsockets, LSockets1),
LSockets1;
LSockets1 ->
LSockets1
end,
Procs = [begin
LSocketId = (AcceptorId rem NumListenSockets) + 1,
{_, LSocket} = lists:keyfind(LSocketId, 1, LSockets),
Expand Down
2 changes: 2 additions & 0 deletions src/ranch_app.erl
Expand Up @@ -22,6 +22,8 @@
-spec start(application:start_type(), term()) -> {ok, pid()} | {error, term()}.
start(_, _) ->
_ = consider_profiling(),
ranch_server = ets:new(ranch_server, [
ordered_set, public, named_table]),
ranch_sup:start_link().

-spec stop(term()) -> ok.
Expand Down
7 changes: 6 additions & 1 deletion src/ranch_listener_sup.erl
Expand Up @@ -32,7 +32,12 @@ start_link(Ref, Transport, TransOpts, Protocol, ProtoOpts) ->
-spec init({ranch:ref(), module(), module(), module()})
-> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
init({Ref, Transport, Protocol, Logger}) ->
ok = ranch_server:set_listener_sup(Ref, self()),
case put(running, true) of
undefined ->
ok = ranch_server:set_listener_sup(Ref, self());
true ->
ok
end,
ChildSpecs = [
#{
id => ranch_conns_sup_sup,
Expand Down
2 changes: 0 additions & 2 deletions src/ranch_sup.erl
Expand Up @@ -32,8 +32,6 @@ init([]) ->
{ok, Value2} -> Value2;
undefined -> 5
end,
ranch_server = ets:new(ranch_server, [
ordered_set, public, named_table]),
Procs = [
#{id => ranch_server, start => {ranch_server, start_link, []}}
],
Expand Down

0 comments on commit dfd0c82

Please sign in to comment.