ranch_conns_sup_sup.erl 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. %% Copyright (c) 2019-2021, Jan Uhlig <juhlig@hnc-agency.org>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(ranch_conns_sup_sup).
  15. -behaviour(supervisor).
  16. -export([start_link/4]).
  17. -export([init/1]).
  18. -spec start_link(ranch:ref(), module(), module(), module()) -> {ok, pid()}.
  19. start_link(Ref, Transport, Protocol, Logger) ->
  20. ok = ranch_server:cleanup_connections_sups(Ref),
  21. supervisor:start_link(?MODULE, {
  22. Ref, Transport, Protocol, Logger
  23. }).
  24. -spec init({ranch:ref(), module(), module(), module()})
  25. -> {ok, {supervisor:sup_flags(), [supervisor:child_spec()]}}.
  26. init({Ref, Transport, Protocol, Logger}) ->
  27. TransOpts = ranch_server:get_transport_options(Ref),
  28. NumAcceptors = maps:get(num_acceptors, TransOpts, 10),
  29. NumConnsSups = maps:get(num_conns_sups, TransOpts, NumAcceptors),
  30. StatsCounters = counters:new(2*NumConnsSups, []),
  31. ok = ranch_server:set_stats_counters(Ref, StatsCounters),
  32. ChildSpecs = [#{
  33. id => {ranch_conns_sup, N},
  34. start => {ranch_conns_sup, start_link, [Ref, N, Transport, TransOpts, Protocol, Logger]},
  35. type => supervisor
  36. } || N <- lists:seq(1, NumConnsSups)],
  37. {ok, {#{intensity => 1 + ceil(math:log2(NumConnsSups))}, ChildSpecs}}.