ranch_acceptors_sup.erl 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/3]).
  17. -export([init/1]).
  18. -spec start_link(ranch:ref(), pos_integer(), module())
  19. -> {ok, pid()}.
  20. start_link(Ref, NumAcceptors, Transport) ->
  21. supervisor:start_link(?MODULE, [Ref, NumAcceptors, Transport]).
  22. init([Ref, NumAcceptors, Transport]) ->
  23. TransOpts = ranch_server:get_transport_options(Ref),
  24. Logger = maps:get(logger, TransOpts, logger),
  25. NumListenSockets = maps:get(num_listen_sockets, TransOpts, 1),
  26. %% We temporarily put the logger in the process dictionary
  27. %% so that it can be used from ranch:filter_options. The
  28. %% interface as it currently is does not allow passing it
  29. %% down otherwise.
  30. LSockets = start_listen_sockets(Ref, NumListenSockets, Transport, TransOpts, Logger),
  31. Procs = [begin
  32. LSocketId = (AcceptorId rem NumListenSockets) + 1,
  33. {_, LSocket} = lists:keyfind(LSocketId, 1, LSockets),
  34. #{
  35. id => {acceptor, self(), AcceptorId},
  36. start => {ranch_acceptor, start_link, [Ref, AcceptorId, LSocket, Transport, Logger]},
  37. shutdown => brutal_kill
  38. }
  39. end || AcceptorId <- lists:seq(1, NumAcceptors)],
  40. {ok, {#{}, Procs}}.
  41. -spec start_listen_sockets(any(), pos_integer(), module(), map(), module())
  42. -> [{pos_integer(), inet:socket()}].
  43. start_listen_sockets(Ref, NumListenSockets, Transport, TransOpts0, Logger) when NumListenSockets > 0 ->
  44. BaseSocket = start_listen_socket(Ref, Transport, TransOpts0, Logger),
  45. {ok, Addr} = Transport:sockname(BaseSocket),
  46. ExtraSockets = case Addr of
  47. {local, _} when NumListenSockets > 1 ->
  48. listen_error(Ref, Transport, TransOpts0, reuseport_local, Logger);
  49. {local, _} ->
  50. [];
  51. {_, Port} ->
  52. SocketOpts = maps:get(socket_opts, TransOpts0, []),
  53. SocketOpts1 = case lists:keyfind(port, 1, SocketOpts) of
  54. {port, Port} ->
  55. SocketOpts;
  56. _ ->
  57. [{port, Port}|lists:keydelete(port, 1, SocketOpts)]
  58. end,
  59. TransOpts1 = TransOpts0#{socket_opts => SocketOpts1},
  60. [{N, start_listen_socket(Ref, Transport, TransOpts1, Logger)}
  61. || N <- lists:seq(2, NumListenSockets)]
  62. end,
  63. ranch_server:set_addr(Ref, Addr),
  64. [{1, BaseSocket}|ExtraSockets].
  65. -spec start_listen_socket(any(), module(), map(), module()) -> inet:socket().
  66. start_listen_socket(Ref, Transport, TransOpts, Logger) ->
  67. case Transport:listen(TransOpts) of
  68. {ok, Socket} ->
  69. Socket;
  70. {error, Reason} ->
  71. listen_error(Ref, Transport, TransOpts, Reason, Logger)
  72. end.
  73. -spec listen_error(any(), module(), any(), atom(), module()) -> no_return().
  74. listen_error(Ref, Transport, TransOpts0, Reason, Logger) ->
  75. SocketOpts0 = maps:get(socket_opts, TransOpts0, []),
  76. SocketOpts1 = [{cert, '...'}|proplists:delete(cert, SocketOpts0)],
  77. SocketOpts2 = [{key, '...'}|proplists:delete(key, SocketOpts1)],
  78. SocketOpts = [{cacerts, '...'}|proplists:delete(cacerts, SocketOpts2)],
  79. TransOpts = TransOpts0#{socket_opts => SocketOpts},
  80. ranch:log(error,
  81. "Failed to start Ranch listener ~p in ~p:listen(~999999p) for reason ~p (~s)~n",
  82. [Ref, Transport, TransOpts, Reason, format_error(Reason)], Logger),
  83. exit({listen_error, Ref, Reason}).
  84. format_error(no_cert) ->
  85. "no certificate provided; see cert, certfile, sni_fun or sni_hosts options";
  86. format_error(reuseport_local) ->
  87. "num_listen_sockets must be set to 1 for local sockets";
  88. format_error(Reason) ->
  89. inet:format_error(Reason).