Browse Source

Removed connections trigger acceptors wake-up

Jose M Perez 5 years ago
parent
commit
1db8290685
2 changed files with 24 additions and 1 deletions
  1. 5 1
      src/ranch_conns_sup.erl
  2. 19 0
      test/acceptor_SUITE.erl

+ 5 - 1
src/ranch_conns_sup.erl

@@ -147,8 +147,12 @@ loop(State=#state{parent=Parent, ref=Ref, conn_type=ConnType,
 		%% Remove a connection from the count of connections.
 		{remove_connection, Ref, Pid} ->
 			case put(Pid, removed) of
-				active ->
+				active when Sleepers =:= [] ->
 					loop(State, CurConns - 1, NbChildren, Sleepers);
+				active ->
+					[To|Sleepers2] = Sleepers,
+					To ! self(),
+					loop(State, CurConns - 1, NbChildren, Sleepers2);
 				removed ->
 					loop(State, CurConns, NbChildren, Sleepers);
 				undefined ->

+ 19 - 0
test/acceptor_SUITE.erl

@@ -40,6 +40,7 @@ groups() ->
 		tcp_max_connections_and_beyond,
 		tcp_max_connections_infinity,
 		tcp_remove_connections,
+		tcp_remove_connections_acceptor_wakeup,
 		tcp_set_max_connections,
 		tcp_set_max_connections_clean,
 		tcp_getopts_capability,
@@ -974,6 +975,24 @@ tcp_remove_connections(_) ->
 	0 = ranch_server:count_connections(Name),
 	ok = ranch:stop_listener(Name).
 
+tcp_remove_connections_acceptor_wakeup(_) ->
+	doc("Ensure that removed connections wake up acceptors."),
+	Name = name(),
+	{ok, _} = ranch:start_listener(Name,
+		ranch_tcp, #{max_connections => 1, num_acceptors => 1},
+		remove_conn_and_wait_protocol, [{remove, true, infinity}]),
+	Port = ranch:get_port(Name),
+	ConnectOptions = [binary, {active, false}],
+	Localhost = "localhost",
+	{ok, Socket1} = gen_tcp:connect(Localhost, Port, ConnectOptions),
+	{ok, Socket2} = gen_tcp:connect(Localhost, Port, ConnectOptions),
+	{ok, Socket3} = gen_tcp:connect(Localhost, Port, ConnectOptions),
+	ok = gen_tcp:send(Socket3, <<"bye">>),
+	true = maps:get(all_connections, ranch:info(Name)) >= 2,
+	ok = gen_tcp:send(Socket1, <<"bye">>),
+	ok = gen_tcp:send(Socket2, <<"bye">>),
+	ok = ranch:stop_listener(Name).
+
 tcp_set_max_connections(_) ->
 	doc("Ensure that changing the max_connections option to a larger value allows for more connections."),
 	Name = name(),