Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/esockd.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
-define(ARG_CLOSED_MAX_LIMIT, closed_max_limit).
-define(ARG_CLOSED_OVERLOADED, closed_overloaded).
-define(ARG_CLOSED_RATE_LIMITED, closed_rate_limited).
%% The socket failed before the connection process was started,
%% most likely because the peer closed or reset it.
-define(ARG_CLOSED_EARLY, closed_early).
%% The peer address is denied by the access rules.
-define(ARG_CLOSED_FORBIDDEN, closed_forbidden).
-define(ARG_CLOSED_OTHER_REASONS, closed_other_reasons).

-define(ACCEPT_RESULT_GROUPS,
Expand All @@ -88,6 +93,8 @@
?ARG_CLOSED_MAX_LIMIT,
?ARG_CLOSED_OVERLOADED,
?ARG_CLOSED_RATE_LIMITED,
?ARG_CLOSED_EARLY,
?ARG_CLOSED_FORBIDDEN,
?ARG_CLOSED_OTHER_REASONS
]).

Expand Down
11 changes: 9 additions & 2 deletions src/esockd_acceptor_fsm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ handle_event(info, Message, State = {accepting, Ref, InState}, D) ->
case async_accept_result(Message, Ref, D) of
{ok, Sock} ->
handle_accepted(Sock, InState, D);
{error, closed} ->
%% The listen socket is closed, no client socket to count.
listener_socket_closed(D);
{error, Reason} ->
inc_stats(D, Reason),
handle_socket_error(Reason, InState, D);
Expand Down Expand Up @@ -238,8 +241,6 @@ maybe_log_start_error(Reason, D) ->
listener => format_sockname(D),
cause => Reason}).

handle_socket_error(closed, _State, D) ->
listener_socket_closed(D);
%% {error, econnaborted} -> accept
handle_socket_error(econnaborted, State, D) ->
{next_state, State, D, {next_event, internal, accept}};
Expand Down Expand Up @@ -285,6 +286,12 @@ counter(enfile) -> ?ARG_CLOSED_SYS_LIMIT;
counter(?ERROR_MAXLIMIT) -> ?ARG_CLOSED_MAX_LIMIT;
counter(overloaded) -> ?ARG_CLOSED_OVERLOADED;
counter(rate_limited) -> ?ARG_CLOSED_RATE_LIMITED;
counter(econnaborted) -> ?ARG_CLOSED_EARLY;
counter(econnreset) -> ?ARG_CLOSED_EARLY;
counter(enotconn) -> ?ARG_CLOSED_EARLY;
counter(einval) -> ?ARG_CLOSED_EARLY;
counter(closed) -> ?ARG_CLOSED_EARLY;
counter(forbidden) -> ?ARG_CLOSED_FORBIDDEN;
counter(_) -> ?ARG_CLOSED_OTHER_REASONS.

start_connection(ConnSup, Transport, Sock, UpgradeFuns) when is_pid(ConnSup) ->
Expand Down
4 changes: 3 additions & 1 deletion src/esockd_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ inc_stats({Protocol, ListenOn}, Metric, Num) when is_integer(Num) ->
dec_stats({Protocol, ListenOn}, Metric, Num) when is_integer(Num) ->
update_counter({{Protocol, ListenOn}, Metric}, -Num).

%% NOTE: The default row keeps the caller from crashing when the metric
%% was not initialized, e.g. a listener started before the metric was added.
update_counter(Key, Num) ->
ets:update_counter(?STATS_TAB, Key, {2, Num}).
ets:update_counter(?STATS_TAB, Key, {2, Num}, {Key, 0}).

-spec(del_stats({atom(), esockd:listen_on()}) -> ok).
del_stats({Protocol, ListenOn}) ->
Expand Down
67 changes: 67 additions & 0 deletions test/esockd_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,69 @@ t_tune_fun_ok(_) ->
?assertEqual(1, proplists:get_value(accepted, Cnts)),
ok = esockd:close(Name, LPort).

%% A socket denied by the access rules is counted as `closed_forbidden'.
t_closed_forbidden(_) ->
LPort = 7006,
Name = ?FUNCTION_NAME,
{ok, _LSup} = esockd:open(Name, LPort,
[{access_rules, [{deny, "127.0.0.1/32"}]},
{connection_mfargs, echo_server}]),
try
{ok, Socket} = gen_tcp:connect("127.0.0.1", LPort, [{active, true}]),
receive
{tcp_closed, Socket} -> ok
after 1000 ->
ct:fail(close_timeout)
end,
Cnts = esockd_server:get_stats({Name, LPort}),
?assertEqual(0, proplists:get_value(accepted, Cnts)),
?assertEqual(1, proplists:get_value(closed_forbidden, Cnts)),
?assertEqual(0, proplists:get_value(closed_other_reasons, Cnts))
after
ok = esockd:close(Name, LPort)
end.

%% A socket reset by the peer before the connection process is started
%% is counted as `closed_early'.
t_closed_early(_) ->
LPort = 7007,
Name = ?FUNCTION_NAME,
TuneFun = {?MODULE, sock_tune_fun_wait, [self()]},
{ok, _LSup} = esockd:open(Name, LPort,
[{tune_fun, TuneFun},
{connection_mfargs, echo_server}]),
try
{ok, Socket} = gen_tcp:connect("127.0.0.1", LPort, [{active, false}]),
%% The acceptor holds the accepted socket in tune_fun.
Acceptor = receive {tune_fun_wait, Pid} -> Pid
after 1000 -> ct:fail(tune_fun_timeout)
end,
%% Reset the connection from the client side.
ok = inet:setopts(Socket, [{linger, {true, 0}}]),
ok = gen_tcp:close(Socket),
Acceptor ! tune_fun_continue,
ok = wait_for_stats({Name, LPort}, closed_early, 1, 1000),
Cnts = esockd_server:get_stats({Name, LPort}),
?assertEqual(0, proplists:get_value(accepted, Cnts)),
?assertEqual(0, proplists:get_value(closed_other_reasons, Cnts))
after
ok = esockd:close(Name, LPort)
end.

wait_for_stats(ListenerRef, Metric, Expected, Timeout) when Timeout > 0 ->
case proplists:get_value(Metric, esockd_server:get_stats(ListenerRef)) of
Expected ->
ok;
_ ->
timer:sleep(10),
wait_for_stats(ListenerRef, Metric, Expected, Timeout - 10)
end;
wait_for_stats(ListenerRef, Metric, Expected, _Timeout) ->
ct:fail(#{cause => stats_timeout,
metric => Metric,
expected => Expected,
stats => esockd_server:get_stats(ListenerRef)}).


t_listener_handle_port_exit_tcp(Config) ->
do_listener_handle_port_exit(Config, false).
Expand Down Expand Up @@ -953,6 +1016,10 @@ do_listener_handle_port_exit(Config, IsTls) ->
sock_tune_fun(Ret) ->
Ret.

sock_tune_fun_wait(TestPid) ->
TestPid ! {tune_fun_wait, self()},
receive tune_fun_continue -> ok end.

-spec get_acceptors(supervisor:supervisor()) -> [Acceptor::pid()].
get_acceptors(LSup) ->
Children = supervisor:which_children(LSup),
Expand Down
60 changes: 60 additions & 0 deletions test/esockd_acceptor_fsm_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@
-define(COUNTER_SYS_LIMIT, 4).
-define(COUNTER_MAX_LIMIT, 5).
-define(COUNTER_OTHER_REASONS, 6).
-define(COUNTER_EARLY, 7).
-define(COUNTER_FORBIDDEN, 8).
-define(COUNTER_LAST, 10).

counter_tag_to_index(accepted) -> ?COUNTER_ACCEPTED;
counter_tag_to_index(closed_sys_limit) -> ?COUNTER_SYS_LIMIT;
counter_tag_to_index(closed_max_limit) -> ?COUNTER_MAX_LIMIT;
counter_tag_to_index(closed_overloaded) -> ?COUNTER_OVERLOADED;
counter_tag_to_index(closed_rate_limited) -> ?COUNTER_RATE_LIMITED;
counter_tag_to_index(closed_early) -> ?COUNTER_EARLY;
counter_tag_to_index(closed_forbidden) -> ?COUNTER_FORBIDDEN;
counter_tag_to_index(closed_other_reasons) -> ?COUNTER_OTHER_REASONS.

all() ->
Expand Down Expand Up @@ -193,8 +197,55 @@ t_einval(Config) ->
Opts = #{tune_fun => {fun(_, _) -> {error, einval} end, []}},
Server = start(Port, no_rate_limit(), Opts, Config),
{ok, Sock1} = connect(Port),
try
ok = wait_for_counter(Config, ?COUNTER_EARLY, 1, 2000),
?assertEqual(0, get_counter(Config, ?COUNTER_OTHER_REASONS)),
disconnect(Sock1)
after
stop(Server)
end.

%% Failed to tune the socket opts for a reason that is not a socket error
t_tune_fun_other_error(Config) ->
Port = ?PORT,
Opts = #{tune_fun => {fun(_, _) -> {error, unknown} end, []}},
Server = start(Port, no_rate_limit(), Opts, Config),
{ok, Sock1} = connect(Port),
try
ok = wait_for_counter(Config, ?COUNTER_OTHER_REASONS, 1, 2000),
?assertEqual(0, get_counter(Config, ?COUNTER_EARLY)),
disconnect(Sock1)
after
stop(Server)
end.

%% The socket is closed before the connection process is started
t_closed_early(Config) ->
lists:foreach(
fun(Reason) ->
Port = ?PORT,
Opts = #{start_connection_result => {error, Reason}},
Server = start(Port, no_rate_limit(), Opts, Config),
{ok, Sock1} = connect(Port),
try
ok = wait_for_counter(Config, ?COUNTER_EARLY, 1, 2000),
?assertEqual(0, get_counter(Config, ?COUNTER_OTHER_REASONS)),
disconnect(Sock1)
after
stop(Server),
reset_counter(Config, ?COUNTER_EARLY)
end
end, [econnreset, enotconn, einval, closed]).

%% The peer address is denied by the access rules
t_forbidden(Config) ->
Port = ?PORT,
Opts = #{start_connection_result => {error, forbidden}},
Server = start(Port, no_rate_limit(), Opts, Config),
{ok, Sock1} = connect(Port),
try
ok = wait_for_counter(Config, ?COUNTER_FORBIDDEN, 1, 2000),
?assertEqual(0, get_counter(Config, ?COUNTER_OTHER_REASONS)),
disconnect(Sock1)
after
stop(Server)
Expand Down Expand Up @@ -258,6 +309,9 @@ t_close_listener_socket_cause_acceptor_stop(Config) ->
1000 ->
error("Acceptor process did not terminate in time")
end,
%% A closed listen socket is not a closed client socket.
?assertEqual(0, get_counter(Config, ?COUNTER_EARLY)),
?assertEqual(0, get_counter(Config, ?COUNTER_OTHER_REASONS)),
receive
{listen_socket_closed, Acceptor} ->
ok
Expand Down Expand Up @@ -326,6 +380,12 @@ consume(_Token, #{name := no_rate_limit} = Limiter) ->

now_ts() -> erlang:system_time(millisecond).

get_counter(Config, Index) ->
counters:get(proplists:get_value(counters, Config), Index).

reset_counter(Config, Index) ->
counters:put(proplists:get_value(counters, Config), Index, 0).

wait_for_counter(Config, Index, Count, Timeout) ->
Counters = proplists:get_value(counters, Config),
Now = now_ts(),
Expand Down
10 changes: 10 additions & 0 deletions test/esockd_server_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ t_inc_dec_stats(_) ->
[] = esockd_server:get_stats(Name),
ok = esockd_server:stop().

%% Updating a metric that was not initialized creates it.
t_inc_uninitialized_stats(_) ->
{ok, _} = esockd_server:start_link(),
Name = {echo, 3000},
esockd_server:init_stats(Name, [accepting]),
esockd_server:inc_stats(Name, closed_early, 1),
?assertEqual([{accepting, 0}, {closed_early, 1}],
lists:sort(esockd_server:get_stats(Name))),
ok = esockd_server:stop().

t_stats_fun(_) ->
{ok, _} = esockd_server:start_link(),
StatsFun = esockd_server:stats_fun({echo, 3000}, accepting),
Expand Down