ranch_acceptors_sup.erl 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. %% Copyright (c) 2011-2013, 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_acceptors_sup).
  15. -behaviour(supervisor).
  16. -export([start_link/4]).
  17. -export([init/1]).
  18. -spec start_link(ranch:ref(), non_neg_integer(), module(), any())
  19. -> {ok, pid()}.
  20. start_link(Ref, NbAcceptors, Transport, TransOpts) ->
  21. supervisor:start_link(?MODULE, [Ref, NbAcceptors, Transport, TransOpts]).
  22. init([Ref, NbAcceptors, Transport, TransOpts]) ->
  23. ConnsSup = ranch_server:get_connections_sup(Ref),
  24. LSocket = case proplists:get_value(socket, TransOpts) of
  25. undefined ->
  26. {ok, Socket} = Transport:listen(TransOpts),
  27. Socket;
  28. Socket ->
  29. Socket
  30. end,
  31. {ok, {_, Port}} = Transport:sockname(LSocket),
  32. ranch_server:set_port(Ref, Port),
  33. Procs = [
  34. {{acceptor, self(), N}, {ranch_acceptor, start_link, [
  35. LSocket, Transport, ConnsSup
  36. ]}, permanent, brutal_kill, worker, []}
  37. || N <- lists:seq(1, NbAcceptors)],
  38. {ok, {{one_for_one, 10, 10}, Procs}}.