Mercurial > prosody-hg
comparison plugins/mod_csi_simple.lua @ 11926:99444bf26a3d
mod_csi_simple: Allow some straggler traffic after flushing buffer
Statistics from my server shows a high rate of very short buffer hold
times, most of which are the result of replies to pings or other iq
traffic, or mod_smacks acks and ack requests just after a flush was
completed.
This grace period should eliminate noise and quick flipping between
flushing and inactive mode.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sat, 20 Nov 2021 19:23:08 +0100 |
| parents | fae5441fc6cf |
| children | 4d63d8ef1cf9 |
comparison
equal
deleted
inserted
replaced
| 11925:3e0d03a74285 | 11926:99444bf26a3d |
|---|---|
| 8 | 8 |
| 9 local jid = require "util.jid"; | 9 local jid = require "util.jid"; |
| 10 local st = require "util.stanza"; | 10 local st = require "util.stanza"; |
| 11 local dt = require "util.datetime"; | 11 local dt = require "util.datetime"; |
| 12 local filters = require "util.filters"; | 12 local filters = require "util.filters"; |
| 13 local timer = require "util.timer"; | |
| 13 | 14 |
| 14 local queue_size = module:get_option_number("csi_queue_size", 256); | 15 local queue_size = module:get_option_number("csi_queue_size", 256); |
| 16 local resume_delay = module:get_option_number("csi_resume_inactive_delay", 5); | |
| 15 | 17 |
| 16 local important_payloads = module:get_option_set("csi_important_payloads", { }); | 18 local important_payloads = module:get_option_set("csi_important_payloads", { }); |
| 17 | 19 |
| 18 function is_important(stanza) --> boolean, reason: string | 20 function is_important(stanza) --> boolean, reason: string |
| 19 if stanza == " " then | 21 if stanza == " " then |
| 192 module:hook("pre-resource-unbind", function (event) | 194 module:hook("pre-resource-unbind", function (event) |
| 193 local session = event.session; | 195 local session = event.session; |
| 194 disable_optimizations(session); | 196 disable_optimizations(session); |
| 195 end, 1); | 197 end, 1); |
| 196 | 198 |
| 197 module:hook("c2s-ondrain", function (event) | 199 local function resume_optimizations(_, _, session) |
| 198 local session = event.session; | 200 if (session.state == "flushing" or session.state == "inactive") and session.conn and session.conn.pause_writes then |
| 199 if (session.state == "flushing" or session.state == "inactive") and session.conn and session.conn.pause_writes then | |
| 200 session.state = "inactive"; | 201 session.state = "inactive"; |
| 201 session.conn:pause_writes(); | 202 session.conn:pause_writes(); |
| 202 session.csi_measure_buffer_hold = measure_buffer_hold(); | 203 session.csi_measure_buffer_hold = measure_buffer_hold(); |
| 203 session.log("debug", "Buffer flushed, resuming inactive mode (queue size was %d)", session.csi_counter); | 204 session.log("debug", "Buffer flushed, resuming inactive mode (queue size was %d)", session.csi_counter); |
| 204 session.csi_counter = 0; | 205 session.csi_counter = 0; |
| 206 end | |
| 207 session.csi_resume = nil; | |
| 208 end | |
| 209 | |
| 210 module:hook("c2s-ondrain", function (event) | |
| 211 local session = event.session; | |
| 212 if (session.state == "flushing" or session.state == "inactive") and session.conn and session.conn.pause_writes then | |
| 213 -- After flushing, remain in pseudo-flushing state for a moment to allow | |
| 214 -- some followup traffic, iq replies, smacks acks to be sent without having | |
| 215 -- to go back and forth between inactive and flush mode. | |
| 216 if not session.csi_resume then | |
| 217 session.csi_resume = timer.add_task(resume_delay, resume_optimizations, session); | |
| 218 end | |
| 219 -- Should further noise in this short grace period push back the delay? | |
| 220 -- Probably not great if the session can be kept in pseudo-active mode | |
| 221 -- indefinitely. | |
| 205 end | 222 end |
| 206 end); | 223 end); |
| 207 | 224 |
| 208 function module.load() | 225 function module.load() |
| 209 for _, user_session in pairs(prosody.hosts[module.host].sessions) do | 226 for _, user_session in pairs(prosody.hosts[module.host].sessions) do |
