From 662d94a531178af005f06b0bfb4a8660b0fa023f Mon Sep 17 00:00:00 2001 From: Fred Hebert Date: Fri, 11 Jan 2013 10:31:00 -0500 Subject: Ignore tracking of requests when MaxConn = infinity There is no need to contact the server and track requests unless being asked to do so by the user. It's going to be faster and more efficient to not track anything when being told tracking doesn't matter. Whenever the max connections is set to infinity, the connections counting key is not created, or is deleted if it existed already. When using a numeric value, the connection count is created or maintained if it existed already. Moreover, trying to reduce a listener's counter while the max connection number is set to `infinity` will return 0 and avoid all counting operations as they are meaningless. --- src/ranch_listener.erl | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'src/ranch_listener.erl') diff --git a/src/ranch_listener.erl b/src/ranch_listener.erl index 81943d4..408fbcd 100644 --- a/src/ranch_listener.erl +++ b/src/ranch_listener.erl @@ -69,8 +69,14 @@ add_connection(ServerPid, ConnPid) -> %% connections. -spec remove_connection(pid()) -> non_neg_integer(). remove_connection(ServerPid) -> - ok = gen_server:cast(ServerPid, remove_connection), - ranch_server:remove_connection(ServerPid). + try + Count = ranch_server:remove_connection(ServerPid), + ok = gen_server:cast(ServerPid, remove_connection), + Count + catch + error:badarg -> % Max conns = infinity + 0 + end. %% @doc Return the listener's port. -spec get_port(pid()) -> {ok, inet:port_number()}. @@ -105,8 +111,12 @@ set_protocol_options(ServerPid, ProtoOpts) -> %% gen_server. %% @private +init([Ref, infinity, ProtoOpts]) -> + ok = ranch_server:insert_listener(Ref, self()), + {ok, #state{ref=Ref, max_conns=infinity, proto_opts=ProtoOpts}}; init([Ref, MaxConns, ProtoOpts]) -> ok = ranch_server:insert_listener(Ref, self()), + ranch_server:add_connections_counter(self()), {ok, #state{ref=Ref, max_conns=MaxConns, proto_opts=ProtoOpts}}. %% @private @@ -115,9 +125,19 @@ handle_call(get_port, _From, State=#state{port=Port}) -> handle_call(get_max_connections, _From, State=#state{max_conns=MaxConns}) -> {reply, {ok, MaxConns}, State}; handle_call({set_max_connections, MaxConnections}, _From, - State=#state{ref=Ref}) -> + State=#state{ref=Ref, max_conns=CurrMax, rm_diff=CurrDiff}) -> + RmDiff = case {MaxConnections, CurrMax} of + {infinity, _} -> % moving to infinity, delete connection key + ranch_server:remove_connections_counter(self()), + 0; + {_, infinity} -> % moving away from infinity, create connection key + ranch_server:add_connections_counter(self()), + CurrDiff; + {_, _} -> % stay current + CurrDiff + end, ranch_server:send_to_acceptors(Ref, {set_max_conns, MaxConnections}), - {reply, ok, State#state{max_conns=MaxConnections}}; + {reply, ok, State#state{max_conns=MaxConnections, rm_diff=RmDiff}}; handle_call(get_protocol_options, _From, State=#state{proto_opts=ProtoOpts}) -> {reply, {ok, ProtoOpts}, State}; handle_call({set_protocol_options, ProtoOpts}, _From, State=#state{ref=Ref}) -> @@ -132,6 +152,8 @@ handle_call(_, _From, State) -> handle_cast({add_connection, ConnPid}, State) -> _ = erlang:monitor(process, ConnPid), {noreply, State}; +handle_cast(remove_connection, State=#state{max_conns=infinity}) -> + {noreply, State}; handle_cast(remove_connection, State=#state{rm_diff=RmDiff}) -> {noreply, State#state{rm_diff=RmDiff + 1}}; handle_cast({set_port, Port}, State) -> -- cgit v1.2.3