ranch_acceptor.erl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. %% Copyright (c) 2011-2021, 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_acceptor).
  15. -export([start_link/5]).
  16. -export([init/4]).
  17. -export([loop/5]).
  18. -spec start_link(ranch:ref(), pos_integer(), inet:socket(), module(), module())
  19. -> {ok, pid()}.
  20. start_link(Ref, AcceptorId, LSocket, Transport, Logger) ->
  21. ConnsSup = ranch_server:get_connections_sup(Ref, AcceptorId),
  22. Pid = spawn_link(?MODULE, init, [LSocket, Transport, Logger, ConnsSup]),
  23. {ok, Pid}.
  24. -spec init(inet:socket(), module(), module(), pid()) -> no_return().
  25. init(LSocket, Transport, Logger, ConnsSup) ->
  26. MonitorRef = monitor(process, ConnsSup),
  27. loop(LSocket, Transport, Logger, ConnsSup, MonitorRef).
  28. -spec loop(inet:socket(), module(), module(), pid(), reference()) -> no_return().
  29. loop(LSocket, Transport, Logger, ConnsSup, MonitorRef) ->
  30. _ = case Transport:accept(LSocket, infinity) of
  31. {ok, CSocket} ->
  32. case Transport:controlling_process(CSocket, ConnsSup) of
  33. ok ->
  34. %% This call will not return until process has been started
  35. %% AND we are below the maximum number of connections.
  36. ranch_conns_sup:start_protocol(ConnsSup, MonitorRef,
  37. CSocket);
  38. {error, _} ->
  39. Transport:close(CSocket)
  40. end;
  41. %% Reduce the accept rate if we run out of file descriptors.
  42. %% We can't accept anymore anyway, so we might as well wait
  43. %% a little for the situation to resolve itself.
  44. {error, emfile} ->
  45. ranch:log(warning,
  46. "Ranch acceptor reducing accept rate: out of file descriptors~n",
  47. [], Logger),
  48. receive after 100 -> ok end;
  49. %% Exit if the listening socket got closed.
  50. {error, closed} ->
  51. exit(closed);
  52. %% Continue otherwise.
  53. {error, _} ->
  54. ok
  55. end,
  56. flush(Logger),
  57. ?MODULE:loop(LSocket, Transport, Logger, ConnsSup, MonitorRef).
  58. flush(Logger) ->
  59. receive Msg ->
  60. ranch:log(warning,
  61. "Ranch acceptor received unexpected message: ~p~n",
  62. [Msg], Logger),
  63. flush(Logger)
  64. after 0 ->
  65. ok
  66. end.