aboutsummaryrefslogtreecommitdiffstats
path: root/src/ranch_acceptor.erl
blob: d208a7fe4886a44bfd6b5c9c4b490a7e8977bfb3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
%% Copyright (c) 2011-2012, Loïc Hoguin <[email protected]>
%%
%% Permission to use, copy, modify, and/or distribute this software for any
%% purpose with or without fee is hereby granted, provided that the above
%% copyright notice and this permission notice appear in all copies.
%%
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

%% @private
-module(ranch_acceptor).

%% API.
-export([start_link/6]).

%% Internal.
-export([init/7]).
-export([loop/7]).

%% API.

-spec start_link(any(), inet:socket(), module(), module(), pid(), pid())
	-> {ok, pid()}.
start_link(Ref, LSocket, Transport, Protocol, ListenerPid, ConnsSup) ->
	{ok, MaxConns} = ranch_listener:get_max_connections(ListenerPid),
	{ok, Opts} = ranch_listener:get_protocol_options(ListenerPid),
	Pid = spawn_link(?MODULE, init,
		[LSocket, Transport, Protocol, MaxConns, Opts, ListenerPid, ConnsSup]),
	ok = ranch_server:add_acceptor(Ref, Pid),
	{ok, Pid}.

%% Internal.

-spec init(inet:socket(), module(), module(),
	non_neg_integer(), any(), pid(), pid()) -> no_return().
init(LSocket, Transport, Protocol, MaxConns, Opts, ListenerPid, ConnsSup) ->
	async_accept(LSocket, Transport),
	loop(LSocket, Transport, Protocol, MaxConns, Opts, ListenerPid, ConnsSup).

-spec loop(inet:socket(), module(), module(),
	non_neg_integer(), any(), pid(), pid()) -> no_return().
loop(LSocket, Transport, Protocol, MaxConns, Opts, ListenerPid, ConnsSup) ->
	receive
		%% We couldn't accept the socket but it's safe to continue.
		{accept, continue} ->
			?MODULE:init(LSocket, Transport, Protocol,
				MaxConns, Opts, ListenerPid, ConnsSup);
		%% Found my sockets!
		{accept, CSocket} ->
			{ok, ConnPid} = supervisor:start_child(ConnsSup,
				[ListenerPid, CSocket, Transport, Protocol, Opts]),
			Transport:controlling_process(CSocket, ConnPid),
			ConnPid ! {shoot, ListenerPid},
			NbConns = ranch_listener:add_connection(ListenerPid, ConnPid),
			maybe_wait(ListenerPid, MaxConns, NbConns),
			?MODULE:init(LSocket, Transport, Protocol,
				MaxConns, Opts, ListenerPid, ConnsSup);
		%% Upgrade the protocol options.
		{set_opts, Opts2} ->
			?MODULE:loop(LSocket, Transport, Protocol,
				MaxConns, Opts2, ListenerPid, ConnsSup)
	end.

-spec maybe_wait(pid(), non_neg_integer(), non_neg_integer()) -> ok.
maybe_wait(_, MaxConns, NbConns) when MaxConns > NbConns ->
	ok;
maybe_wait(ListenerPid, MaxConns, _) ->
	erlang:yield(),
	NbConns2 = ranch_server:count_connections(ListenerPid),
	maybe_wait(ListenerPid, MaxConns, NbConns2).

-spec async_accept(inet:socket(), module()) -> ok.
async_accept(LSocket, Transport) ->
	AcceptorPid = self(),
	_ = spawn_link(fun() ->
		case Transport:accept(LSocket, infinity) of
			{ok, CSocket} ->
				Transport:controlling_process(CSocket, AcceptorPid),
				AcceptorPid ! {accept, CSocket};
			%% We want to crash if the listening socket got closed.
			{error, Reason} when Reason =/= closed ->
				AcceptorPid ! {accept, continue}
		end
	end),
	ok.