فهرست منبع

fix(ocpp): return correct current_connections number of listenrs http api

JianBo He 1 سال پیش
والد
کامیت
d7ebecddb4

+ 26 - 13
apps/emqx_gateway/src/emqx_gateway_api_listeners.erl

@@ -247,9 +247,10 @@ page_params(Qs) ->
 get_cluster_listeners_info(GwName) ->
     Listeners = emqx_gateway_conf:listeners(GwName),
     ListenOns = lists:map(
-        fun(#{id := Id} = Conf) ->
+        fun(#{id := Id, type := Type0} = Conf) ->
+            Type = binary_to_existing_atom(Type0),
             ListenOn = emqx_gateway_conf:get_bind(Conf),
-            {Id, ListenOn}
+            {Type, Id, ListenOn}
         end,
         Listeners
     ),
@@ -293,17 +294,11 @@ listeners_cluster_status(Listeners) ->
 do_listeners_cluster_status(Listeners) ->
     Node = node(),
     lists:foldl(
-        fun({Id, ListenOn}, Acc) ->
-            BinId = erlang:atom_to_binary(Id),
-            {ok, #{<<"max_connections">> := Max}} = emqx_gateway_conf:listener(BinId),
-            {Running, Curr} =
-                try esockd:get_current_connections({Id, ListenOn}) of
-                    Int -> {true, Int}
-                catch
-                    %% not started
-                    error:not_found ->
-                        {false, 0}
-                end,
+        fun({Type, Id, ListenOn}, Acc) ->
+            {Running, Curr} = current_listener_status(Type, Id, ListenOn),
+            {ok, #{<<"max_connections">> := Max}} = emqx_gateway_conf:listener(
+                erlang:atom_to_binary(Id)
+            ),
             Acc#{
                 Id => #{
                     node => Node,
@@ -319,6 +314,24 @@ do_listeners_cluster_status(Listeners) ->
         Listeners
     ).
 
+current_listener_status(Type, Id, _ListenOn) when Type =:= ws; Type =:= wss ->
+    Info = ranch:info(Id),
+    Conns = proplists:get_value(all_connections, Info, 0),
+    Running =
+        case proplists:get_value(status, Info) of
+            running -> true;
+            _ -> false
+        end,
+    {Running, Conns};
+current_listener_status(_Type, Id, ListenOn) ->
+    try esockd:get_current_connections({Id, ListenOn}) of
+        Int -> {true, Int}
+    catch
+        %% not started
+        error:not_found ->
+            {false, 0}
+    end.
+
 ensure_integer_or_infinity(infinity) ->
     infinity;
 ensure_integer_or_infinity(<<"infinity">>) ->

+ 50 - 0
apps/emqx_gateway_ocpp/test/emqx_ocpp_SUITE.erl

@@ -181,8 +181,55 @@ t_adjust_keepalive_timer(_Config) ->
     ?assertMatch(
         #{conninfo := #{keepalive := 300}}, emqx_gateway_cm:get_chan_info(ocpp, <<"client1">>)
     ),
+    %% close conns
+    close(ClientPid),
+    timer:sleep(1000),
+    %% assert:
+    ?assertEqual(undefined, emqx_gateway_cm:get_chan_info(ocpp, <<"client1">>)),
     ok.
 
+t_listeners_status(_Config) ->
+    {200, [Listener]} = request(get, "/gateways/ocpp/listeners"),
+    ?assertMatch(
+        #{
+            status := #{running := true, current_connections := 0}
+        },
+        Listener
+    ),
+    %% add a connection
+    {ok, ClientPid} = connect("127.0.0.1", 33033, <<"client1">>),
+    UniqueId = <<"3335862321">>,
+    BootNotification = #{
+        id => UniqueId,
+        type => ?OCPP_MSG_TYPE_ID_CALL,
+        action => <<"BootNotification">>,
+        payload => #{
+            <<"chargePointVendor">> => <<"vendor1">>,
+            <<"chargePointModel">> => <<"model1">>
+        }
+    },
+    ok = send_msg(ClientPid, BootNotification),
+    timer:sleep(1000),
+    %% assert: the current_connections is 1
+    {200, [Listener1]} = request(get, "/gateways/ocpp/listeners"),
+    ?assertMatch(
+        #{
+            status := #{running := true, current_connections := 1}
+        },
+        Listener1
+    ),
+    %% close conns
+    close(ClientPid),
+    timer:sleep(1000),
+    %% assert: the current_connections is 0
+    {200, [Listener2]} = request(get, "/gateways/ocpp/listeners"),
+    ?assertMatch(
+        #{
+            status := #{running := true, current_connections := 0}
+        },
+        Listener2
+    ).
+
 %%--------------------------------------------------------------------
 %% ocpp simple client
 
@@ -229,3 +276,6 @@ receive_msg(ConnPid) ->
     after 5000 ->
         {error, timeout}
     end.
+
+close(ConnPid) ->
+    gun:shutdown(ConnPid).

+ 0 - 1
changes/ce/fix-12892.md

@@ -1 +0,0 @@
-Avoid printing error logs when processing downstream messages

+ 3 - 0
changes/ee/fix-12892.md

@@ -0,0 +1,3 @@
+Fix a logical error in OCPP gateway's handling of downstream BootNotification.
+
+Fix the `gateways/ocpp/listeners` endpoint to return the correct current connection number.