aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMaria-12648430 <[email protected]>2020-09-04 14:32:41 +0200
committerLoïc Hoguin <[email protected]>2020-09-10 15:01:49 +0200
commit03a8256e989091d7d153b8632c25fdc64c88ada1 (patch)
tree9493887c7cbd1f4fb3335a966e9367eb1527d3d4 /src
parenta57210e4e06358304b8d49ae955ab6466e0a289b (diff)
downloadranch-03a8256e989091d7d153b8632c25fdc64c88ada1.tar.gz
ranch-03a8256e989091d7d153b8632c25fdc64c88ada1.tar.bz2
ranch-03a8256e989091d7d153b8632c25fdc64c88ada1.zip
Metric counters for connection accepts and terminates
Diffstat (limited to 'src')
-rw-r--r--src/ranch.appup4
-rw-r--r--src/ranch.erl20
-rw-r--r--src/ranch_conns_sup.erl54
-rw-r--r--src/ranch_conns_sup_sup.erl2
-rw-r--r--src/ranch_server.erl18
5 files changed, 89 insertions, 9 deletions
diff --git a/src/ranch.appup b/src/ranch.appup
index 8630070..8357065 100644
--- a/src/ranch.appup
+++ b/src/ranch.appup
@@ -16,15 +16,15 @@
{load_module, ranch_acceptor},
{update, ranch_acceptors_sup, supervisor},
{load_module, ranch_app},
+ {update, ranch_server, {advanced, []}},
+ {update, ranch_conns_sup_sup, supervisor},
%% See comments at the top of the file about ranch_conns_sup.
{update, ranch_conns_sup, {advanced, []}},
- {update, ranch_conns_sup_sup, supervisor},
{load_module, ranch_crc32c},
{update, ranch_embedded_sup, supervisor},
{update, ranch_listener_sup, supervisor},
{load_module, ranch_protocol},
{load_module, ranch_proxy_header},
- {update, ranch_server, {advanced, []}},
{update, ranch_server_proxy, {advanced, []}},
{load_module, ranch_ssl},
{update, ranch_sup, supervisor},
diff --git a/src/ranch.erl b/src/ranch.erl
index 4adb1d8..a83402d 100644
--- a/src/ranch.erl
+++ b/src/ranch.erl
@@ -420,7 +420,8 @@ listener_info(Ref, Pid) ->
transport => Transport,
transport_options => TransOpts,
protocol => Protocol,
- protocol_options => ProtoOpts
+ protocol_options => ProtoOpts,
+ metrics => metrics(Ref)
}.
-spec procs(ref(), acceptors | connections) -> [pid()].
@@ -447,6 +448,23 @@ procs1(ListenerSup, connections) ->
),
lists:flatten(Conns).
+-spec metrics(ref()) -> #{}.
+metrics(Ref) ->
+ Counters = ranch_server:get_stats_counters(Ref),
+ CounterInfo = counters:info(Counters),
+ NumCounters = maps:get(size, CounterInfo),
+ NumConnsSups = NumCounters div 2,
+ lists:foldl(
+ fun (Id, Acc) ->
+ Acc#{
+ {conns_sup, Id, accept} => counters:get(Counters, 2*Id-1),
+ {conns_sup, Id, terminate} => counters:get(Counters, 2*Id)
+ }
+ end,
+ #{},
+ lists:seq(1, NumConnsSups)
+ ).
+
-spec wait_for_connections
(ref(), '>' | '>=' | '==' | '=<', non_neg_integer()) -> ok;
(ref(), '<', pos_integer()) -> ok.
diff --git a/src/ranch_conns_sup.erl b/src/ranch_conns_sup.erl
index ca2682d..5242446 100644
--- a/src/ranch_conns_sup.erl
+++ b/src/ranch_conns_sup.erl
@@ -34,6 +34,7 @@
-record(state, {
parent = undefined :: pid(),
ref :: ranch:ref(),
+ id :: pos_integer(),
conn_type :: conn_type(),
shutdown :: shutdown(),
transport = undefined :: module(),
@@ -41,6 +42,7 @@
opts :: any(),
handshake_timeout :: timeout(),
max_conns = undefined :: ranch:max_conns(),
+ stats_counters_ref :: counters:counters_ref(),
logger = undefined :: module()
}).
@@ -108,21 +110,25 @@ init(Parent, Ref, Id, Transport, TransOpts, Protocol, Logger) ->
Shutdown = maps:get(shutdown, TransOpts, 5000),
HandshakeTimeout = maps:get(handshake_timeout, TransOpts, 5000),
ProtoOpts = ranch_server:get_protocol_options(Ref),
+ StatsCounters = ranch_server:get_stats_counters(Ref),
ok = proc_lib:init_ack(Parent, {ok, self()}),
- loop(#state{parent=Parent, ref=Ref, conn_type=ConnType,
+ loop(#state{parent=Parent, ref=Ref, id=Id, conn_type=ConnType,
shutdown=Shutdown, transport=Transport, protocol=Protocol,
- opts=ProtoOpts, handshake_timeout=HandshakeTimeout,
+ opts=ProtoOpts, stats_counters_ref=StatsCounters,
+ handshake_timeout=HandshakeTimeout,
max_conns=MaxConns, logger=Logger}, 0, 0, []).
-loop(State=#state{parent=Parent, ref=Ref, conn_type=ConnType,
- transport=Transport, protocol=Protocol, opts=Opts,
+loop(State=#state{parent=Parent, ref=Ref, id=Id, conn_type=ConnType,
+ transport=Transport, protocol=Protocol, opts=Opts, stats_counters_ref=StatsCounters,
max_conns=MaxConns, logger=Logger}, CurConns, NbChildren, Sleepers) ->
receive
{?MODULE, start_protocol, To, Socket} ->
try Protocol:start_link(Ref, Transport, Opts) of
{ok, Pid} ->
+ inc_accept(StatsCounters, Id, 1),
handshake(State, CurConns, NbChildren, Sleepers, To, Socket, Pid, Pid);
{ok, SupPid, ProtocolPid} when ConnType =:= supervisor ->
+ inc_accept(StatsCounters, Id, 1),
handshake(State, CurConns, NbChildren, Sleepers, To, Socket, SupPid, ProtocolPid);
Ret ->
To ! self(),
@@ -180,9 +186,11 @@ loop(State=#state{parent=Parent, ref=Ref, conn_type=ConnType,
{'EXIT', Pid, Reason} when Sleepers =:= [] ->
case erase(Pid) of
active ->
+ inc_terminate(StatsCounters, Id, 1),
report_error(Logger, Ref, Protocol, Pid, Reason),
loop(State, CurConns - 1, NbChildren - 1, Sleepers);
removed ->
+ inc_terminate(StatsCounters, Id, 1),
report_error(Logger, Ref, Protocol, Pid, Reason),
loop(State, CurConns, NbChildren - 1, Sleepers);
undefined ->
@@ -192,14 +200,17 @@ loop(State=#state{parent=Parent, ref=Ref, conn_type=ConnType,
{'EXIT', Pid, Reason} ->
case erase(Pid) of
active when CurConns > MaxConns ->
+ inc_terminate(StatsCounters, Id, 1),
report_error(Logger, Ref, Protocol, Pid, Reason),
loop(State, CurConns - 1, NbChildren - 1, Sleepers);
active ->
+ inc_terminate(StatsCounters, Id, 1),
report_error(Logger, Ref, Protocol, Pid, Reason),
[To|Sleepers2] = Sleepers,
To ! self(),
loop(State, CurConns - 1, NbChildren - 1, Sleepers2);
removed ->
+ inc_terminate(StatsCounters, Id, 1),
report_error(Logger, Ref, Protocol, Pid, Reason),
loop(State, CurConns, NbChildren - 1, Sleepers);
undefined ->
@@ -270,12 +281,15 @@ set_transport_options(State=#state{max_conns=MaxConns0}, CurConns, NbChildren, S
CurConns, NbChildren, Sleepers1).
-spec terminate(#state{}, any(), non_neg_integer()) -> no_return().
-terminate(#state{shutdown=brutal_kill}, Reason, _) ->
+terminate(#state{shutdown=brutal_kill, id=Id,
+ stats_counters_ref=StatsCounters}, Reason, NbChildren) ->
kill_children(get_keys(active)),
kill_children(get_keys(removed)),
+ inc_terminate(StatsCounters, Id, NbChildren),
exit(Reason);
%% Attempt to gracefully shutdown all children.
-terminate(#state{shutdown=Shutdown}, Reason, NbChildren) ->
+terminate(#state{shutdown=Shutdown, id=Id,
+ stats_counters_ref=StatsCounters}, Reason, NbChildren) ->
shutdown_children(get_keys(active)),
shutdown_children(get_keys(removed)),
_ = if
@@ -285,8 +299,17 @@ terminate(#state{shutdown=Shutdown}, Reason, NbChildren) ->
erlang:send_after(Shutdown, self(), kill)
end,
wait_children(NbChildren),
+ inc_terminate(StatsCounters, Id, NbChildren),
exit(Reason).
+inc_accept(StatsCounters, Id, N) ->
+ %% Accepts are counted in the odd indexes.
+ counters:add(StatsCounters, 2*Id-1, N).
+
+inc_terminate(StatsCounters, Id, N) ->
+ %% Terminates are counted in the even indexes.
+ counters:add(StatsCounters, 2*Id, N).
+
%% Kill all children and then exit. We unlink first to avoid
%% getting a message for each child getting killed.
kill_children(Pids) ->
@@ -334,6 +357,25 @@ system_terminate(Reason, _, _, {State, _, NbChildren, _}) ->
terminate(State, Reason, NbChildren).
-spec system_code_change(any(), _, _, _) -> {ok, any()}.
+system_code_change({#state{parent=Parent, ref=Ref, conn_type=ConnType,
+ shutdown=Shutdown, transport=Transport, protocol=Protocol,
+ opts=Opts, handshake_timeout=HandshakeTimeout,
+ max_conns=MaxConns, logger=Logger}, CurConns, NbChildren,
+ Sleepers}, _, {down, _}, _) ->
+ {ok, {{state, Parent, Ref, ConnType, Shutdown, Transport, Protocol,
+ Opts, HandshakeTimeout, MaxConns, Logger}, CurConns, NbChildren,
+ Sleepers}};
+system_code_change({{state, Parent, Ref, ConnType, Shutdown, Transport, Protocol,
+ Opts, HandshakeTimeout, MaxConns, Logger}, CurConns, NbChildren,
+ Sleepers}, _, _, _) ->
+ Self = self(),
+ [Id] = [Id || {Id, Pid} <- ranch_server:get_connections_sups(Ref), Pid=:=Self],
+ StatsCounters = ranch_server:get_stats_counters(Ref),
+ {ok, {#state{parent=Parent, ref=Ref, id=Id, conn_type=ConnType, shutdown=Shutdown,
+ transport=Transport, protocol=Protocol, opts=Opts,
+ handshake_timeout=HandshakeTimeout, max_conns=MaxConns,
+ stats_counters_ref=StatsCounters,
+ logger=Logger}, CurConns, NbChildren, Sleepers}};
system_code_change(Misc, _, _, _) ->
{ok, Misc}.
diff --git a/src/ranch_conns_sup_sup.erl b/src/ranch_conns_sup_sup.erl
index 11e62cd..ce6c349 100644
--- a/src/ranch_conns_sup_sup.erl
+++ b/src/ranch_conns_sup_sup.erl
@@ -32,6 +32,8 @@ init({Ref, Transport, Protocol, Logger}) ->
TransOpts = ranch_server:get_transport_options(Ref),
NumAcceptors = maps:get(num_acceptors, TransOpts, 10),
NumConnsSups = maps:get(num_conns_sups, TransOpts, NumAcceptors),
+ StatsCounters = counters:new(2*NumConnsSups, []),
+ ok = ranch_server:set_stats_counters(Ref, StatsCounters),
ChildSpecs = [#{
id => {ranch_conns_sup, N},
start => {ranch_conns_sup, start_link, [Ref, N, Transport, TransOpts, Protocol, Logger]},
diff --git a/src/ranch_server.erl b/src/ranch_server.erl
index bff7c80..8ef22f3 100644
--- a/src/ranch_server.erl
+++ b/src/ranch_server.erl
@@ -32,6 +32,8 @@
-export([get_addr/1]).
-export([set_max_connections/2]).
-export([get_max_connections/1]).
+-export([set_stats_counters/2]).
+-export([get_stats_counters/1]).
-export([set_transport_options/2]).
-export([get_transport_options/1]).
-export([set_protocol_options/2]).
@@ -79,6 +81,7 @@ cleanup_listener_opts(Ref) ->
%% expected a crash (because the listener was stopped).
%% Deleting it explictly here removes any possible confusion.
_ = ets:match_delete(?TAB, {{conns_sup, Ref, '_'}, '_'}),
+ _ = ets:delete(?TAB, {stats_counters, Ref}),
%% Ditto for the listener supervisor.
_ = ets:delete(?TAB, {listener_sup, Ref}),
ok.
@@ -86,6 +89,7 @@ cleanup_listener_opts(Ref) ->
-spec cleanup_connections_sups(ranch:ref()) -> ok.
cleanup_connections_sups(Ref) ->
_ = ets:match_delete(?TAB, {{conns_sup, Ref, '_'}, '_'}),
+ _ = ets:delete(?TAB, {stats_counters, Ref}),
ok.
-spec set_connections_sup(ranch:ref(), non_neg_integer(), pid()) -> ok.
@@ -139,6 +143,14 @@ set_max_connections(Ref, MaxConnections) ->
get_max_connections(Ref) ->
ets:lookup_element(?TAB, {max_conns, Ref}, 2).
+-spec set_stats_counters(ranch:ref(), counters:counters_ref()) -> ok.
+set_stats_counters(Ref, Counters) ->
+ gen_server:call(?MODULE, {set_stats_counters, Ref, Counters}).
+
+-spec get_stats_counters(ranch:ref()) -> counters:counters_ref().
+get_stats_counters(Ref) ->
+ ets:lookup_element(?TAB, {stats_counters, Ref}, 2).
+
-spec set_transport_options(ranch:ref(), any()) -> ok.
set_transport_options(Ref, TransOpts) ->
gen_server:call(?MODULE, {set_trans_opts, Ref, TransOpts}).
@@ -198,6 +210,9 @@ handle_call({set_max_conns, Ref, MaxConns}, _, State) ->
ets:insert(?TAB, {{max_conns, Ref}, MaxConns}),
_ = [ConnsSup ! {set_max_conns, MaxConns} || {_, ConnsSup} <- get_connections_sups(Ref)],
{reply, ok, State};
+handle_call({set_stats_counters, Ref, Counters}, _, State) ->
+ ets:insert(?TAB, {{stats_counters, Ref}, Counters}),
+ {reply, ok, State};
handle_call({set_trans_opts, Ref, Opts}, _, State) ->
ets:insert(?TAB, {{trans_opts, Ref}, Opts}),
{reply, ok, State};
@@ -237,6 +252,9 @@ terminate(_Reason, _State) ->
ok.
-spec code_change(term() | {down, term()}, #state{}, term()) -> {ok, term()}.
+code_change({down, _}, State, _Extra) ->
+ true = ets:match_delete(?TAB, {{stats_counters, '_'}, '_'}),
+ {ok, State};
code_change(_OldVsn, State, _Extra) ->
{ok, State}.