chore: don't lost previous's statval

This commit is contained in:
某文 2023-05-18 09:44:01 +08:00
parent e26ce5816e
commit d4d25d2660
2 changed files with 12 additions and 5 deletions

View File

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

View File

@ -21,7 +21,8 @@
init/2, init/2,
info/1, info/1,
info/2, info/2,
check/2 check/2,
update/2
]). ]).
-elvis([{elvis_style, no_if_expression, disable}]). -elvis([{elvis_style, no_if_expression, disable}]).
@ -52,7 +53,7 @@ init(Interval) -> init(0, Interval).
%% typically this is a few minutes. %% typically this is a few minutes.
%% The maximum value is (65535s) 18 hours 12 minutes and 15 seconds. %% The maximum value is (65535s) 18 hours 12 minutes and 15 seconds.
%% @doc Init keepalive. %% @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 -> init(StatVal, Interval) when Interval > 0 andalso Interval =< ?MAX_INTERVAL ->
#keepalive{interval = Interval, statval = StatVal}; #keepalive{interval = Interval, statval = StatVal};
init(_, 0) -> init(_, 0) ->
@ -84,3 +85,10 @@ info(interval, undefined) ->
{ok, keepalive()} | {error, timeout}. {ok, keepalive()} | {error, timeout}.
check(Val, #keepalive{statval = Val}) -> {error, timeout}; check(Val, #keepalive{statval = Val}) -> {error, timeout};
check(Val, KeepAlive) -> {ok, KeepAlive#keepalive{statval = Val}}. 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).