plugins.pubsub2room: Throttle messages to the room when a batch of notifications arrives at once

Sat, 12 Mar 2011 10:36:06 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 12 Mar 2011 10:36:06 +0000
changeset 71
9ab5919aa416
parent 70
22670e551879
child 72
a1c73a366ee0

plugins.pubsub2room: Throttle messages to the room when a batch of notifications arrives at once

plugins/pubsub2room.lua file | annotate | diff | comparison | revisions
--- a/plugins/pubsub2room.lua	Sat Mar 12 10:35:40 2011 +0000
+++ b/plugins/pubsub2room.lua	Sat Mar 12 10:36:06 2011 +0000
@@ -34,6 +34,8 @@
 	return stanza and setmetatable({ stanza = stanza, data = data }, extractor_mt) or nil;
 end
 
+local last_message_time = 0;
+
 function riddim.plugins.pubsub2room(bot)
 	local bare_jid = require "util.jid".bare;
 	bot:add_plugin("pubsub");
@@ -47,7 +49,17 @@
 
 		if not conf or not entry or not room then return end
 		local message = conf.template:gsub("%${([^}]+)}", entry);
-		room:send_message(message);
+		
+		-- Throttle to 1 message/second so we don't flood the room
+		if os.time() - last_message_time > 0 then
+			room:send_message(message);
+			last_message_time = os.time();
+		else
+			last_message_time = last_message_time + 1;
+			verse.add_task(last_message_time - os.time(), function ()
+				room:send_message(message);
+			end);
+		end
 	end);
 
 	-- FIXME When to unsubscribe?

mercurial