zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

ranch_acceptor.erl (2283B)


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