ranch_acceptors_sup.erl 1.7 KB

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