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_server.erl | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'src/ranch_server.erl') diff --git a/src/ranch_server.erl b/src/ranch_server.erl index a17e103..c6d7c19 100644 --- a/src/ranch_server.erl +++ b/src/ranch_server.erl @@ -27,6 +27,8 @@ -export([add_connection/1]). -export([count_connections/1]). -export([remove_connection/1]). +-export([add_connections_counter/1]). +-export([remove_connections_counter/1]). %% gen_server. -export([init/1]). @@ -95,7 +97,12 @@ add_connection(ListenerPid) -> %% @doc Count the number of connections in the connection pool. -spec count_connections(pid()) -> non_neg_integer(). count_connections(ListenerPid) -> - ets:update_counter(?TAB, {connections, ListenerPid}, 0). + try + ets:update_counter(?TAB, {connections, ListenerPid}, 0) + catch + error:badarg -> % Max conns = infinity + 0 + end. %% @doc Remove a connection from the connection pool. %% @@ -104,6 +111,21 @@ count_connections(ListenerPid) -> remove_connection(ListenerPid) -> ets:update_counter(?TAB, {connections, ListenerPid}, -1). + +%% @doc Add a connections counter to the connection pool +%% +%% Should only be used by ranch listeners when settings regarding the max +%% number of connections change. +add_connections_counter(Pid) -> + true = ets:insert_new(?TAB, {{connections, Pid}, 0}). + +%% @doc remove a connections counter from the connection pool +%% +%% Should only be used by ranch listeners when settings regarding the max +%% number of connections change. +remove_connections_counter(Pid) -> + true = ets:delete(?TAB, {connections, Pid}). + %% gen_server. %% @private @@ -117,7 +139,6 @@ handle_call(_Request, _From, State) -> %% @private handle_cast({insert_listener, Ref, Pid}, State=#state{monitors=Monitors}) -> true = ets:insert_new(?TAB, {{acceptors, Ref}, []}), - true = ets:insert_new(?TAB, {{connections, Pid}, 0}), MonitorRef = erlang:monitor(process, Pid), {noreply, State#state{ monitors=[{{MonitorRef, Pid}, {listener, Ref}}|Monitors]}}; @@ -157,7 +178,7 @@ code_change(_OldVsn, State, _Extra) -> remove_process(Key = {listener, Ref}, MonitorRef, Pid, Monitors) -> true = ets:delete(?TAB, Key), true = ets:delete(?TAB, {acceptors, Ref}), - true = ets:delete(?TAB, {connections, Pid}), + remove_connections_counter(Pid), lists:keydelete({MonitorRef, Pid}, 1, Monitors); remove_process(Key = {acceptors, _}, MonitorRef, Pid, Monitors) -> try -- cgit v1.2.3