Improving the pruning mechanism.

This commit is contained in:
John Livingston 2024-04-18 20:36:06 +02:00
parent ce2d8ed123
commit cfc5e98d90
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
2 changed files with 20 additions and 8 deletions

View File

@ -8,9 +8,11 @@ module:depends"http";
local module_host = module:get_host(); -- this module is not global local module_host = module:get_host(); -- this module is not global
local bare_sessions = prosody.bare_sessions; local bare_sessions = prosody.bare_sessions;
local vcards = module:open_store("vcard"); local vcards = module:open_store("vcard");
local prune_counts = {};
local prune_thresold = 4; -- arbitrary value
function check_auth(routes) function check_auth(routes)
local function check_request_auth(event) local function check_request_auth(event)
local apikey = module:get_option_string("peertubelivechat_manage_users_apikey", "") 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 for user in usermanager.users(module_host) do
-- has the user still open sessions? -- 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); module:log("debug", "User %s on host %s has still active sessions, ignoring.", user, module_host);
prune_counts[jid] = 0; -- reset
else else
-- FIXME: there is a little chance that we delete a user that is currently connecting... -- 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
end end

View File

@ -629,8 +629,8 @@ class ExternalAuthOIDC {
public startPruneTimer (options: RegisterServerOptions): void { public startPruneTimer (options: RegisterServerOptions): void {
this.stopPruneTimer() // just in case... this.stopPruneTimer() // just in case...
// every 4 hour (every minutes in debug mode) // every hour (every minutes in debug mode)
const pruneInterval = debugNumericParameter(options, 'externalAccountPruneInterval', 60 * 1000, 4 * 60 * 60 * 1000) 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.`) 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 // eslint-disable-next-line @typescript-eslint/no-misused-promises