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

chore: don't lost previous's statval

某文 2 лет назад
Родитель
Сommit
d4d25d2660
2 измененных файлов с 12 добавлено и 5 удалено
  1. 2 3
      apps/emqx/src/emqx_channel.erl
  2. 10 2
      apps/emqx/src/emqx_keepalive.erl

+ 2 - 3
apps/emqx/src/emqx_channel.erl

@@ -1199,13 +1199,12 @@ handle_call(list_authz_cache, Channel) ->
 handle_call(
     {keepalive, Interval},
     Channel = #channel{
-        keepalive = _KeepAlive,
+        keepalive = KeepAlive,
         conninfo = ConnInfo
     }
 ) ->
     ClientId = info(clientid, Channel),
-    RecvCnt = emqx_pd:get_counter(recv_pkt),
-    NKeepalive = emqx_keepalive:init(RecvCnt, Interval * 1000),
+    NKeepalive = emqx_keepalive:update(timer:seconds(Interval), KeepAlive),
     NConnInfo = maps:put(keepalive, Interval, ConnInfo),
     NChannel = Channel#channel{keepalive = NKeepalive, conninfo = NConnInfo},
     SockInfo = maps:get(sockinfo, emqx_cm:get_chan_info(ClientId), #{}),

+ 10 - 2
apps/emqx/src/emqx_keepalive.erl

@@ -21,7 +21,8 @@
     init/2,
     info/1,
     info/2,
-    check/2
+    check/2,
+    update/2
 ]).
 
 -elvis([{elvis_style, no_if_expression, disable}]).
@@ -52,7 +53,7 @@ init(Interval) -> init(0, Interval).
 %% typically this is a few minutes.
 %% The maximum value is (65535s) 18 hours 12 minutes and 15 seconds.
 %% @doc Init keepalive.
--spec init(StatVal :: non_neg_integer(), Interval :: non_neg_integer()) -> keepalive().
+-spec init(StatVal :: non_neg_integer(), Interval :: non_neg_integer()) -> keepalive() | undefined.
 init(StatVal, Interval) when Interval > 0 andalso Interval =< ?MAX_INTERVAL ->
     #keepalive{interval = Interval, statval = StatVal};
 init(_, 0) ->
@@ -84,3 +85,10 @@ info(interval, undefined) ->
     {ok, keepalive()} | {error, timeout}.
 check(Val, #keepalive{statval = Val}) -> {error, timeout};
 check(Val, KeepAlive) -> {ok, KeepAlive#keepalive{statval = Val}}.
+
+%% @doc Update keepalive.
+%% The statval of the previous keepalive will be used,
+%% and normal checks will begin from the next cycle.
+-spec update(non_neg_integer(), keepalive() | undefined) -> keepalive() | undefined.
+update(Interval, undefined) -> init(0, Interval);
+update(Interval, #keepalive{statval = StatVal}) -> init(StatVal, Interval).