Changeset

2764:1872a9129c2f

mod_mam: Use a FIFO queue for scheduling archive expiry
author Kim Alvefur <zash@zash.se>
date Tue, 12 Sep 2017 15:46:49 +0200
parents 2763:a6d19aea9b73
children 2765:85cf9a8b4020
files mod_mam/mod_mam.lua
diffstat 1 files changed, 11 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/mod_mam/mod_mam.lua	Mon Sep 04 14:49:37 2017 +0200
+++ b/mod_mam/mod_mam.lua	Tue Sep 12 15:46:49 2017 +0200
@@ -60,6 +60,13 @@
 
 local cleanup;
 
+local function schedule_cleanup(username)
+	if cleanup and not cleanup[username] then
+		table.insert(cleanup, username);
+		cleanup[username] = true;
+	end
+end
+
 -- Handle prefs.
 local function handle_prefs(event)
 	local origin, stanza = event.origin, event.stanza;
@@ -113,7 +120,7 @@
 	local query = stanza.tags[1];
 	local qid = query.attr.queryid;
 
-	if cleanup then cleanup[origin.username] = true; end
+	schedule_cleanup(origin.username);
 
 	-- Search query parameters
 	local qwith, qstart, qend;
@@ -316,7 +323,7 @@
 			local id = ok;
 			clone_for_other_handlers:tag("stanza-id", { xmlns = xmlns_st_id, by = store_user.."@"..host, id = id }):up();
 			event.stanza = clone_for_other_handlers;
-			if cleanup then cleanup[store_user] = true; end
+			schedule_cleanup(store_user);
 			module:fire_event("archive-message-added", { origin = origin, stanza = stanza, for_user = store_user, id = id });
 		end
 	else
@@ -370,13 +377,13 @@
 	pcall(function ()
 		-- If this works, then we schedule cleanup for all known users on startup
 		for user in um.users(module.host) do
-			cleanup[user] = true;
+			schedule_cleanup(user);
 		end
 	end);
 
 	-- At odd intervals, delete old messages for one user
 	module:add_timer(math.random(10, 60), function()
-		local user = next(cleanup);
+		local user = table.remove(cleanup, 1);
 		if user then
 			module:log("debug", "Removing old messages for user %q", user);
 			local ok, err = archive:delete(user, { ["end"] = os.time() - cleanup_after; })