Merge pull request #11013 from HJianBo/fix-max-conns-in-gateway-lists-api

fix(gateway): fix 500 crash for '/gateways' endpoint
This commit is contained in:
zhongwencool 2023-06-11 22:29:24 +08:00 committed by GitHub
commit 89ddd54c68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 11 deletions

View File

@ -334,19 +334,12 @@ aggregate_listener_status(
CurrAcc,
RunningAcc
) ->
NMaxAcc = plus_max_connections(MaxAcc, Max),
NMaxAcc = emqx_gateway_utils:plus_max_connections(MaxAcc, Max),
NRunning = aggregate_running(Running, RunningAcc),
aggregate_listener_status(T, NMaxAcc, Current + CurrAcc, NRunning);
aggregate_listener_status([], MaxAcc, CurrAcc, RunningAcc) ->
{MaxAcc, CurrAcc, RunningAcc}.
plus_max_connections(_, infinity) ->
infinity;
plus_max_connections(infinity, _) ->
infinity;
plus_max_connections(A, B) when is_integer(A) andalso is_integer(B) ->
A + B.
aggregate_running(R, R) -> R;
aggregate_running(R, undefined) -> R;
aggregate_running(_, _) -> inconsistent.

View File

@ -161,7 +161,10 @@ max_connections_count(Config) ->
Listeners = emqx_gateway_utils:normalize_config(Config),
lists:foldl(
fun({_, _, _, SocketOpts, _}, Acc) ->
Acc + proplists:get_value(max_connections, SocketOpts, 0)
emqx_gateway_utils:plus_max_connections(
Acc,
proplists:get_value(max_connections, SocketOpts, 0)
)
end,
0,
Listeners
@ -588,10 +591,12 @@ sum_cluster_connections(List) ->
%%--------------------------------------------------------------------
%% Internal funcs
sum_cluster_connections(
[#{max_connections := Max, current_connections := Current} | T], MaxAcc, CurrAcc
) ->
sum_cluster_connections(T, MaxAcc + Max, Current + CurrAcc);
NMaxAcc = emqx_gateway_utils:plus_max_connections(MaxAcc, Max),
sum_cluster_connections(T, NMaxAcc, Current + CurrAcc);
sum_cluster_connections([_ | T], MaxAcc, CurrAcc) ->
sum_cluster_connections(T, MaxAcc, CurrAcc);
sum_cluster_connections([], MaxAcc, CurrAcc) ->

View File

@ -45,7 +45,8 @@
is_running/2,
global_chain/1,
listener_chain/3,
find_gateway_definitions/0
find_gateway_definitions/0,
plus_max_connections/2
]).
-export([stringfy/1]).
@ -607,3 +608,12 @@ ignore_lib_apps(Apps) ->
wx
],
[AppName || {AppName, _, _} <- Apps, not lists:member(AppName, LibApps)].
-spec plus_max_connections(non_neg_integer() | infinity, non_neg_integer() | infinity) ->
pos_integer() | infinity.
plus_max_connections(_, infinity) ->
infinity;
plus_max_connections(infinity, _) ->
infinity;
plus_max_connections(A, B) when is_integer(A) andalso is_integer(B) ->
A + B.

View File

@ -439,6 +439,13 @@ t_listeners_max_conns(_) ->
{200, [Listeners]} = request(get, "/gateways/stomp/listeners"),
?assertMatch(#{max_connections := <<"infinity">>}, Listeners),
{200, Gateways} = request(get, "/gateways"),
[StompGwOverview] = lists:filter(
fun(Gw) -> maps:get(name, Gw) =:= <<"stomp">> end,
Gateways
),
?assertMatch(#{max_connections := <<"infinity">>}, StompGwOverview),
{204, _} = request(delete, "/gateways/stomp/listeners/stomp:tcp:def"),
{404, _} = request(get, "/gateways/stomp/listeners/stomp:tcp:def"),
ok.