Mercurial > prosody-modules
comparison mod_smacks/mod_smacks.lua @ 3620:fb1c8dee2ead
mod_smacks: piggyback ack request onto ack response if queue is not empty, use native stoppable timers if supported
author | tmolitor <thilo@eightysoft.de> |
---|---|
date | Sat, 15 Jun 2019 01:34:52 +0200 |
parents | 3d4eefdd950a |
children | c2c851722a8a |
comparison
equal
deleted
inserted
replaced
3619:74aa35aeb08a | 3620:fb1c8dee2ead |
---|---|
102 return true; -- allow session to be removed from full cache to make room for new one | 102 return true; -- allow session to be removed from full cache to make room for new one |
103 end); | 103 end); |
104 | 104 |
105 local function stoppable_timer(delay, callback) | 105 local function stoppable_timer(delay, callback) |
106 local stopped = false; | 106 local stopped = false; |
107 local timer = module:add_timer(delay, function (t) | |
108 if stopped then return; end | |
109 return callback(t); | |
110 end); | |
111 if timer.stop then return timer; end -- new prosody api includes stop() function | |
107 return { | 112 return { |
108 stop = function () stopped = true end; | 113 stop = function () stopped = true end; |
109 module:add_timer(delay, function (t) | 114 timer; |
110 if stopped then return; end | |
111 return callback(t); | |
112 end); | |
113 }; | 115 }; |
114 end | 116 end |
115 | 117 |
116 local function delayed_ack_function(session) | 118 local function delayed_ack_function(session) |
117 -- fire event only if configured to do so and our session is not hibernated or destroyed | 119 -- fire event only if configured to do so and our session is not hibernated or destroyed |
205 end | 207 end |
206 | 208 |
207 queue[#queue+1] = cached_stanza; | 209 queue[#queue+1] = cached_stanza; |
208 if session.hibernating then | 210 if session.hibernating then |
209 session.log("debug", "hibernating, stanza queued"); | 211 session.log("debug", "hibernating, stanza queued"); |
212 module:fire_event("smacks-hibernation-stanza-queued", {origin = session, queue = queue, stanza = cached_stanza}); | |
210 return nil; | 213 return nil; |
211 end | 214 end |
212 request_ack_if_needed(session, false, "outgoing_stanza_filter"); | 215 request_ack_if_needed(session, false, "outgoing_stanza_filter"); |
213 end | 216 end |
214 return stanza; | 217 return stanza; |
324 return; | 327 return; |
325 end | 328 end |
326 module:log("debug", "Received ack request, acking for %d", origin.handled_stanza_count); | 329 module:log("debug", "Received ack request, acking for %d", origin.handled_stanza_count); |
327 -- Reply with <a> | 330 -- Reply with <a> |
328 (origin.sends2s or origin.send)(st.stanza("a", { xmlns = xmlns_sm, h = string.format("%d", origin.handled_stanza_count) })); | 331 (origin.sends2s or origin.send)(st.stanza("a", { xmlns = xmlns_sm, h = string.format("%d", origin.handled_stanza_count) })); |
332 -- piggyback our own ack request | |
333 if #origin.outgoing_stanza_queue > 0 and origin.last_queue_count ~= #origin.outgoing_stanza_queue then | |
334 request_ack_if_needed(origin, true, "piggybacked by handle_r"); | |
335 end | |
329 return true; | 336 return true; |
330 end | 337 end |
331 module:hook_stanza(xmlns_sm2, "r", function (origin, stanza) return handle_r(origin, stanza, xmlns_sm2); end); | 338 module:hook_stanza(xmlns_sm2, "r", function (origin, stanza) return handle_r(origin, stanza, xmlns_sm2); end); |
332 module:hook_stanza(xmlns_sm3, "r", function (origin, stanza) return handle_r(origin, stanza, xmlns_sm3); end); | 339 module:hook_stanza(xmlns_sm3, "r", function (origin, stanza) return handle_r(origin, stanza, xmlns_sm3); end); |
333 | 340 |