Просмотр исходного кода

fix(conn): better tolerate not-yet-known ClientID when emitting stats

Andrew Mayorov 1 год назад
Родитель
Сommit
f062f059e3
1 измененных файлов с 9 добавлено и 6 удалено
  1. 9 6
      apps/emqx/src/emqx_connection.erl

+ 9 - 6
apps/emqx/src/emqx_connection.erl

@@ -414,7 +414,6 @@ recvloop(
     Parent,
     Parent,
     State = #state{
     State = #state{
         hibernate_after = HibernateAfterMs,
         hibernate_after = HibernateAfterMs,
-        channel = Channel,
         zone = Zone
         zone = Zone
     }
     }
 ) ->
 ) ->
@@ -431,9 +430,7 @@ recvloop(
             true ->
             true ->
                 recvloop(Parent, State);
                 recvloop(Parent, State);
             false ->
             false ->
-                ClientId = emqx_channel:info(clientid, Channel),
-                undefined =/= ClientId andalso
-                    emqx_cm:set_chan_stats(ClientId, stats(State)),
+                _ = set_chan_stats(State),
                 hibernate(Parent, cancel_stats_timer(State))
                 hibernate(Parent, cancel_stats_timer(State))
         end
         end
     end.
     end.
@@ -727,9 +724,8 @@ handle_timeout(
         socket = Socket
         socket = Socket
     }
     }
 ) ->
 ) ->
+    _ = set_chan_stats(State),
     emqx_congestion:maybe_alarm_conn_congestion(Socket, Transport, Channel),
     emqx_congestion:maybe_alarm_conn_congestion(Socket, Transport, Channel),
-    ClientId = emqx_channel:info(clientid, Channel),
-    emqx_cm:set_chan_stats(ClientId, stats(State)),
     {ok, State#state{stats_timer = undefined}};
     {ok, State#state{stats_timer = undefined}};
 handle_timeout(
 handle_timeout(
     TRef,
     TRef,
@@ -747,6 +743,13 @@ handle_timeout(
 handle_timeout(TRef, Msg, State) ->
 handle_timeout(TRef, Msg, State) ->
     with_channel(handle_timeout, [TRef, Msg], State).
     with_channel(handle_timeout, [TRef, Msg], State).
 
 
+set_chan_stats(State = #state{channel = Channel}) ->
+    case emqx_channel:info(clientid, Channel) of
+        %% ClientID is not yet known, nothing to report.
+        undefined -> false;
+        ClientId -> emqx_cm:set_chan_stats(ClientId, stats(State))
+    end.
+
 %%--------------------------------------------------------------------
 %%--------------------------------------------------------------------
 %% Parse incoming data
 %% Parse incoming data
 -compile({inline, [when_bytes_in/3]}).
 -compile({inline, [when_bytes_in/3]}).