ranch_acceptors_sup.erl 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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_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, NumAcceptors, Transport, TransOpts) ->
  21. supervisor:start_link(?MODULE, [Ref, NumAcceptors, Transport, TransOpts]).
  22. init([Ref, NumAcceptors, Transport, TransOpts]) ->
  23. ConnsSup = ranch_server:get_connections_sup(Ref),
  24. LSocket = case proplists:get_value(socket, TransOpts) of
  25. undefined ->
  26. TransOpts2 = proplists:delete(ack_timeout,
  27. proplists:delete(connection_type,
  28. proplists:delete(max_connections,
  29. proplists:delete(num_acceptors,
  30. proplists:delete(shutdown,
  31. proplists:delete(socket, TransOpts)))))),
  32. case Transport:listen(TransOpts2) of
  33. {ok, Socket} -> Socket;
  34. {error, Reason} -> listen_error(Ref, Transport, TransOpts2, Reason)
  35. end;
  36. Socket ->
  37. Socket
  38. end,
  39. {ok, Addr} = Transport:sockname(LSocket),
  40. ranch_server:set_addr(Ref, Addr),
  41. Procs = [
  42. {{acceptor, self(), N}, {ranch_acceptor, start_link, [
  43. LSocket, Transport, ConnsSup
  44. ]}, permanent, brutal_kill, worker, []}
  45. || N <- lists:seq(1, NumAcceptors)],
  46. {ok, {{one_for_one, 1, 5}, Procs}}.
  47. -spec listen_error(any(), module(), any(), atom()) -> no_return().
  48. listen_error(Ref, Transport, TransOpts0, Reason) ->
  49. TransOpts1 = lists:keyreplace(cert, 1, TransOpts0, {cert, '...'}),
  50. TransOpts = lists:keyreplace(key, 1, TransOpts1, {key, '...'}),
  51. error_logger:error_msg(
  52. "Failed to start Ranch listener ~p in ~p:listen(~999999p) for reason ~p (~s)~n",
  53. [Ref, Transport, TransOpts, Reason, format_error(Reason)]),
  54. exit({listen_error, Ref, Reason}).
  55. format_error(no_cert) ->
  56. "no certificate provided; see cert, certfile, sni_fun or sni_hosts options";
  57. format_error(Reason) ->
  58. inet:format_error(Reason).