From f5c4fb5860130ad4cbfe47a43c165d558fa4bee7 Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Tue, 28 Nov 2023 09:58:03 -0300 Subject: [PATCH] fix(ds_session): take conservative estimate of `last_alive_at` when bumping Addresses https://github.com/emqx/emqx/pull/12024#discussion_r1407432154 --- apps/emqx/src/emqx_persistent_session_ds.erl | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/apps/emqx/src/emqx_persistent_session_ds.erl b/apps/emqx/src/emqx_persistent_session_ds.erl index 97825e728..f66d9d451 100644 --- a/apps/emqx/src/emqx_persistent_session_ds.erl +++ b/apps/emqx/src/emqx_persistent_session_ds.erl @@ -399,8 +399,13 @@ handle_timeout(_ClientInfo, get_streams, Session) -> ensure_timer(get_streams), {ok, [], Session}; handle_timeout(_ClientInfo, bump_last_alive_at, Session0) -> - NowMS = now_ms(), - Session = session_set_last_alive_at_trans(Session0, NowMS), + %% Note: we take a pessimistic approach here and assume that the client will be alive + %% until the next bump timeout. With this, we avoid garbage collecting this session + %% too early in case the session/connection/node crashes earlier without having time + %% to commit the time. + BumpInterval = emqx_config:get([session_persistence, last_alive_update_interval]), + EstimatedLastAliveAt = now_ms() + BumpInterval, + Session = session_set_last_alive_at_trans(Session0, EstimatedLastAliveAt), ensure_timer(bump_last_alive_at), {ok, [], Session}.