comparison mod_mam/mod_mam.lua @ 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 f2096c03428b
children 2af42a3af131
comparison
equal deleted inserted replaced
2763:a6d19aea9b73 2764:1872a9129c2f
57 end 57 end
58 58
59 local use_total = true; 59 local use_total = true;
60 60
61 local cleanup; 61 local cleanup;
62
63 local function schedule_cleanup(username)
64 if cleanup and not cleanup[username] then
65 table.insert(cleanup, username);
66 cleanup[username] = true;
67 end
68 end
62 69
63 -- Handle prefs. 70 -- Handle prefs.
64 local function handle_prefs(event) 71 local function handle_prefs(event)
65 local origin, stanza = event.origin, event.stanza; 72 local origin, stanza = event.origin, event.stanza;
66 local xmlns_mam = stanza.tags[1].attr.xmlns; 73 local xmlns_mam = stanza.tags[1].attr.xmlns;
111 local origin, stanza = event.origin, event.stanza; 118 local origin, stanza = event.origin, event.stanza;
112 local xmlns_mam = stanza.tags[1].attr.xmlns; 119 local xmlns_mam = stanza.tags[1].attr.xmlns;
113 local query = stanza.tags[1]; 120 local query = stanza.tags[1];
114 local qid = query.attr.queryid; 121 local qid = query.attr.queryid;
115 122
116 if cleanup then cleanup[origin.username] = true; end 123 schedule_cleanup(origin.username);
117 124
118 -- Search query parameters 125 -- Search query parameters
119 local qwith, qstart, qend; 126 local qwith, qstart, qend;
120 local form = query:get_child("x", "jabber:x:data"); 127 local form = query:get_child("x", "jabber:x:data");
121 if form then 128 if form then
314 if ok then 321 if ok then
315 local clone_for_other_handlers = st.clone(stanza); 322 local clone_for_other_handlers = st.clone(stanza);
316 local id = ok; 323 local id = ok;
317 clone_for_other_handlers:tag("stanza-id", { xmlns = xmlns_st_id, by = store_user.."@"..host, id = id }):up(); 324 clone_for_other_handlers:tag("stanza-id", { xmlns = xmlns_st_id, by = store_user.."@"..host, id = id }):up();
318 event.stanza = clone_for_other_handlers; 325 event.stanza = clone_for_other_handlers;
319 if cleanup then cleanup[store_user] = true; end 326 schedule_cleanup(store_user);
320 module:fire_event("archive-message-added", { origin = origin, stanza = stanza, for_user = store_user, id = id }); 327 module:fire_event("archive-message-added", { origin = origin, stanza = stanza, for_user = store_user, id = id });
321 end 328 end
322 else 329 else
323 log("debug", "Not archiving stanza: %s (prefs)", stanza:top_tag()); 330 log("debug", "Not archiving stanza: %s (prefs)", stanza:top_tag());
324 end 331 end
368 -- Iterating over users is not supported by all authentication modules 375 -- Iterating over users is not supported by all authentication modules
369 -- Catch and ignore error if not supported 376 -- Catch and ignore error if not supported
370 pcall(function () 377 pcall(function ()
371 -- If this works, then we schedule cleanup for all known users on startup 378 -- If this works, then we schedule cleanup for all known users on startup
372 for user in um.users(module.host) do 379 for user in um.users(module.host) do
373 cleanup[user] = true; 380 schedule_cleanup(user);
374 end 381 end
375 end); 382 end);
376 383
377 -- At odd intervals, delete old messages for one user 384 -- At odd intervals, delete old messages for one user
378 module:add_timer(math.random(10, 60), function() 385 module:add_timer(math.random(10, 60), function()
379 local user = next(cleanup); 386 local user = table.remove(cleanup, 1);
380 if user then 387 if user then
381 module:log("debug", "Removing old messages for user %q", user); 388 module:log("debug", "Removing old messages for user %q", user);
382 local ok, err = archive:delete(user, { ["end"] = os.time() - cleanup_after; }) 389 local ok, err = archive:delete(user, { ["end"] = os.time() - cleanup_after; })
383 if not ok then 390 if not ok then
384 module:log("warn", "Could not expire archives for user %s: %s", user, err); 391 module:log("warn", "Could not expire archives for user %s: %s", user, err);