Explorar o código

fix(gateway): fix 500 crash for '/gateways' endpoint

It introduced by https://github.com/emqx/emqx/pull/10961
JianBo He %!s(int64=2) %!d(string=hai) anos
pai
achega
57a3b6fdda

+ 1 - 8
apps/emqx_gateway/src/emqx_gateway_api_listeners.erl

@@ -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.

+ 7 - 2
apps/emqx_gateway/src/emqx_gateway_http.erl

@@ -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) ->

+ 11 - 1
apps/emqx_gateway/src/emqx_gateway_utils.erl

@@ -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.

+ 7 - 0
apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl

@@ -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.