feat(emqx_cm): add check to see if count is ever negative

This commit is contained in:
Thales Macedo Garitezi 2021-11-05 14:01:15 -03:00
parent 1fa7e70081
commit 5cbdfa61b8
No known key found for this signature in database
GPG Key ID: DD279F8152A9B6DD
2 changed files with 22 additions and 1 deletions

View File

@ -534,4 +534,11 @@ decrement_connected_client_count() ->
get_connected_client_count() ->
CRef = persistent_term:get({?MODULE, ?CONN_CLIENT_CTR}),
max(0, counters:get(CRef, ?CONN_CLIENT_CTR_IDX)).
%% check if inconsistent; if so, reset to 0
case counters:get(CRef, ?CONN_CLIENT_CTR_IDX) of
N when N < 0 ->
counters:put(CRef, ?CONN_CLIENT_CTR_IDX, 0),
0;
N ->
N
end.

View File

@ -422,6 +422,20 @@ t_connected_client_stats({'end', _Config}) ->
{ok, _} = supervisor:restart_child(emqx_kernel_sup, emqx_stats),
ok.
%% alwayseven if
t_connect_client_never_negative({init, Config}) ->
Config;
t_connect_client_never_negative(Config) when is_list(Config) ->
?assertEqual(0, emqx_cm:get_connected_client_count()),
%% would go to -1
emqx_cm:decrement_connected_client_count(),
?assertEqual(0, emqx_cm:get_connected_client_count()),
%% would be 0, if really went to -1
emqx_cm:increment_connected_client_count(),
?assertEqual(1, emqx_cm:get_connected_client_count()),
ok;
t_connect_client_never_negative({'end', _Config}) ->
ok.
wait_for_events(Action, Kinds) ->
wait_for_events(Action, Kinds, 100).