ranch_listener_sup.erl 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. %% Copyright (c) 2011-2018, Loïc Hoguin <essen@ninenines.eu>
  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_listener_sup).
  15. -behaviour(supervisor).
  16. -export([start_link/5]).
  17. -export([init/1]).
  18. -spec start_link(ranch:ref(), module(), any(), module(), any())
  19. -> {ok, pid()}.
  20. start_link(Ref, Transport, TransOpts, Protocol, ProtoOpts) ->
  21. NumAcceptors = maps:get(num_acceptors, TransOpts, 10),
  22. NumConnsSups = maps:get(num_conns_sups, TransOpts, NumAcceptors),
  23. MaxConns = maps:get(max_connections, TransOpts, 1024),
  24. ranch_server:set_new_listener_opts(Ref, MaxConns, TransOpts, ProtoOpts,
  25. [Ref, Transport, TransOpts, Protocol, ProtoOpts]),
  26. supervisor:start_link(?MODULE, {
  27. Ref, NumAcceptors, NumConnsSups, Transport, Protocol
  28. }).
  29. init({Ref, NumAcceptors, NumConnsSups, Transport, Protocol}) ->
  30. ok = ranch_server:set_listener_sup(Ref, self()),
  31. ChildSpecs = [
  32. #{
  33. id => ranch_conns_sup_sup,
  34. start => {ranch_conns_sup_sup, start_link, [Ref, NumConnsSups, Transport, Protocol]},
  35. type => supervisor
  36. },
  37. #{
  38. id => ranch_acceptors_sup,
  39. start => {ranch_acceptors_sup, start_link, [Ref, NumAcceptors, Transport]},
  40. type => supervisor
  41. }
  42. ],
  43. {ok, {#{strategy => rest_for_one}, ChildSpecs}}.