diff options
author | Loïc Hoguin <[email protected]> | 2015-08-20 12:21:24 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2015-08-20 12:21:24 +0200 |
commit | 52797b0610b1caf0c05e46d7b2e94d09d1ebd393 (patch) | |
tree | 62cb89d1f9142f24989c9a21d52f80f665be520c /src | |
parent | 315582f0bd835205b65a99a5b4a82a278a0a2940 (diff) | |
download | ranch-52797b0610b1caf0c05e46d7b2e94d09d1ebd393.tar.gz ranch-52797b0610b1caf0c05e46d7b2e94d09d1ebd393.tar.bz2 ranch-52797b0610b1caf0c05e46d7b2e94d09d1ebd393.zip |
Supervised and connection process can now be different
Diffstat (limited to 'src')
-rw-r--r-- | src/ranch_conns_sup.erl | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/src/ranch_conns_sup.erl b/src/ranch_conns_sup.erl index c2e8a5c..f3a6a0c 100644 --- a/src/ranch_conns_sup.erl +++ b/src/ranch_conns_sup.erl @@ -108,30 +108,14 @@ init(Parent, Ref, ConnType, Shutdown, Transport, AckTimeout, Protocol) -> loop(State=#state{parent=Parent, ref=Ref, conn_type=ConnType, transport=Transport, protocol=Protocol, opts=Opts, - ack_timeout=AckTimeout, max_conns=MaxConns}, - CurConns, NbChildren, Sleepers) -> + max_conns=MaxConns}, CurConns, NbChildren, Sleepers) -> receive {?MODULE, start_protocol, To, Socket} -> case Protocol:start_link(Ref, Socket, Transport, Opts) of {ok, Pid} -> - case Transport:controlling_process(Socket, Pid) of - ok -> - Pid ! {shoot, Ref, Transport, Socket, AckTimeout}, - put(Pid, true), - CurConns2 = CurConns + 1, - if CurConns2 < MaxConns -> - To ! self(), - loop(State, CurConns2, NbChildren + 1, - Sleepers); - true -> - loop(State, CurConns2, NbChildren + 1, - [To|Sleepers]) - end; - {error, _} -> - Transport:close(Socket), - exit(Pid, kill), - loop(State, CurConns, NbChildren, Sleepers) - end; + shoot(State, CurConns, NbChildren, Sleepers, To, Socket, Pid, Pid); + {ok, SupPid, ProtocolPid} when ConnType =:= supervisor -> + shoot(State, CurConns, NbChildren, Sleepers, To, Socket, SupPid, ProtocolPid); Ret -> To ! self(), error_logger:error_msg( @@ -200,6 +184,27 @@ loop(State=#state{parent=Parent, ref=Ref, conn_type=ConnType, [Ref, Msg]) end. +shoot(State=#state{ref=Ref, transport=Transport, ack_timeout=AckTimeout, max_conns=MaxConns}, + CurConns, NbChildren, Sleepers, To, Socket, SupPid, ProtocolPid) -> + case Transport:controlling_process(Socket, ProtocolPid) of + ok -> + ProtocolPid ! {shoot, Ref, Transport, Socket, AckTimeout}, + put(SupPid, true), + CurConns2 = CurConns + 1, + if CurConns2 < MaxConns -> + To ! self(), + loop(State, CurConns2, NbChildren + 1, Sleepers); + true -> + loop(State, CurConns2, NbChildren + 1, [To|Sleepers]) + end; + {error, _} -> + Transport:close(Socket), + %% Only kill the supervised pid, because the connection's pid, + %% when different, is supposed to be sitting under it and linked. + exit(SupPid, kill), + loop(State, CurConns, NbChildren, Sleepers) + end. + -spec terminate(#state{}, any(), non_neg_integer()) -> no_return(). %% Kill all children and then exit. We unlink first to avoid %% getting a message for each child getting killed. |