ranch_acceptors_sup.erl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/2]).
  17. -export([init/1]).
  18. -spec start_link(ranch:ref(), module())
  19. -> {ok, pid()}.
  20. start_link(Ref, Transport) ->
  21. supervisor:start_link(?MODULE, [Ref, Transport]).
  22. init([Ref, Transport]) ->
  23. ConnsSup = ranch_server:get_connections_sup(Ref),
  24. TransOpts = ranch_server:get_transport_options(Ref),
  25. NumAcceptors = maps:get(num_acceptors, TransOpts, 10),
  26. Logger = maps:get(logger, TransOpts, error_logger),
  27. SocketOpts = maps:get(socket_opts, TransOpts, []),
  28. %% We temporarily put the logger in the process dictionary
  29. %% so that it can be used from ranch:filter_options. The
  30. %% interface as it currently is does not allow passing it
  31. %% down otherwise.
  32. put(logger, Logger),
  33. LSocket = case Transport:listen(SocketOpts) of
  34. {ok, Socket} ->
  35. erase(logger),
  36. Socket;
  37. {error, Reason} ->
  38. listen_error(Ref, Transport, SocketOpts, Reason, Logger)
  39. end,
  40. {ok, Addr} = Transport:sockname(LSocket),
  41. ranch_server:set_addr(Ref, Addr),
  42. Procs = [
  43. {{acceptor, self(), N}, {ranch_acceptor, start_link, [
  44. LSocket, Transport, Logger, ConnsSup
  45. ]}, permanent, brutal_kill, worker, []}
  46. || N <- lists:seq(1, NumAcceptors)],
  47. {ok, {{one_for_one, 1, 5}, Procs}}.
  48. -spec listen_error(any(), module(), any(), atom(), module()) -> no_return().
  49. listen_error(Ref, Transport, SocketOpts0, Reason, Logger) ->
  50. SocketOpts1 = [{cert, '...'}|proplists:delete(cert, SocketOpts0)],
  51. SocketOpts2 = [{key, '...'}|proplists:delete(key, SocketOpts1)],
  52. SocketOpts = [{cacerts, '...'}|proplists:delete(cacerts, SocketOpts2)],
  53. ranch:log(error,
  54. "Failed to start Ranch listener ~p in ~p:listen(~999999p) for reason ~p (~s)~n",
  55. [Ref, Transport, SocketOpts, Reason, format_error(Reason)], Logger),
  56. exit({listen_error, Ref, Reason}).
  57. format_error(no_cert) ->
  58. "no certificate provided; see cert, certfile, sni_fun or sni_hosts options";
  59. format_error(Reason) ->
  60. inet:format_error(Reason).