From cfc5e98d901b28b9981a284c73731ff715f88fea Mon Sep 17 00:00:00 2001 From: John Livingston Date: Thu, 18 Apr 2024 20:36:06 +0200 Subject: [PATCH] Improving the pruning mechanism. --- ...mod_http_peertubelivechat_manage_users.lua | 24 ++++++++++++++----- server/lib/external-auth/oidc.ts | 4 ++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/prosody-modules/mod_http_peertubelivechat_manage_users/mod_http_peertubelivechat_manage_users.lua b/prosody-modules/mod_http_peertubelivechat_manage_users/mod_http_peertubelivechat_manage_users.lua index b323fb08..817cf26b 100644 --- a/prosody-modules/mod_http_peertubelivechat_manage_users/mod_http_peertubelivechat_manage_users.lua +++ b/prosody-modules/mod_http_peertubelivechat_manage_users/mod_http_peertubelivechat_manage_users.lua @@ -8,9 +8,11 @@ module:depends"http"; local module_host = module:get_host(); -- this module is not global local bare_sessions = prosody.bare_sessions; - local vcards = module:open_store("vcard"); +local prune_counts = {}; +local prune_thresold = 4; -- arbitrary value + function check_auth(routes) local function check_request_auth(event) local apikey = module:get_option_string("peertubelivechat_manage_users_apikey", "") @@ -124,15 +126,25 @@ local function prune_users(event) -- delete all users that are not connected! for user in usermanager.users(module_host) do -- has the user still open sessions? - if (bare_sessions[user..'@'..module_host] ~= nil) then + local jid = user..'@'..module_host; + if (bare_sessions[jid] ~= nil) then module:log("debug", "User %s on host %s has still active sessions, ignoring.", user, module_host); + prune_counts[jid] = 0; -- reset else -- FIXME: there is a little chance that we delete a user that is currently connecting... - -- As this prune should not be called too often, we can consider it is not an issue for now. + -- to avoid doing this, we track how often we got here, and only delete after X tries. + if (not prune_counts[jid]) then + prune_counts[jid] = 0; + end + prune_counts[jid] = prune_counts[jid] + 1; + if (prune_counts[jid] < prune_thresold) then -- X is arbitrary... in production will mean X hours + module:log("debug", "User %s on host %s prune count is only %i, ignoring.", user, module_host, prune_counts[jid]); + else + module:log("debug", "Deleting user %s on host %s", user, module_host); + update_vcard(user, nil); + usermanager.delete_user(user, module_host); + end - module:log("debug", "Deleting user %s on host %s", user, module_host); - update_vcard(user, nil); - usermanager.delete_user(user, module_host); end end diff --git a/server/lib/external-auth/oidc.ts b/server/lib/external-auth/oidc.ts index d5433ae3..46dd25b0 100644 --- a/server/lib/external-auth/oidc.ts +++ b/server/lib/external-auth/oidc.ts @@ -629,8 +629,8 @@ class ExternalAuthOIDC { public startPruneTimer (options: RegisterServerOptions): void { this.stopPruneTimer() // just in case... - // every 4 hour (every minutes in debug mode) - const pruneInterval = debugNumericParameter(options, 'externalAccountPruneInterval', 60 * 1000, 4 * 60 * 60 * 1000) + // every hour (every minutes in debug mode) + const pruneInterval = debugNumericParameter(options, 'externalAccountPruneInterval', 60 * 1000, 60 * 60 * 1000) this.logger.info(`Creating a timer for external account pruning, every ${Math.round(pruneInterval / 1000)}s.`) // eslint-disable-next-line @typescript-eslint/no-misused-promises