From 44847d620cf0b7f690682e4906dfcbea8cd3ef03 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Thu, 7 May 2020 21:55:29 +0200 Subject: mod_csi_simple: Refactor to allow logging reason for buffer flush Same style as mod_mam and mod_carbons allows easy comparison. BC: Log format changes --- plugins/mod_csi_simple.lua | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'plugins') diff --git a/plugins/mod_csi_simple.lua b/plugins/mod_csi_simple.lua index 05650957..7b8b3741 100644 --- a/plugins/mod_csi_simple.lua +++ b/plugins/mod_csi_simple.lua @@ -15,8 +15,7 @@ local queue_size = module:get_option_number("csi_queue_size", 256); local important_payloads = module:get_option_set("csi_important_payloads", { }); -module:hook("csi-is-stanza-important", function (event) - local stanza = event.stanza; +function is_important(stanza) --> boolean, reason: string if not st.is_stanza(stanza) then -- whitespace pings etc return true; @@ -69,8 +68,23 @@ module:hook("csi-is-stanza-important", function (event) elseif st_name == "iq" then return true; end +end + +module:hook("csi-is-stanza-important", function (event) + local important, why = is_important(event.stanza); + event.reason = why; + return important; end, -1); +local function should_flush(stanza, session, ctr) --> boolean, reason: string + if ctr >= queue_size then + return true, "queue size limit reached"; + end + local event = { stanza = stanza, session = session }; + local ret = module:fire_event("csi-is-stanza-important", event) + return ret, event.reason; +end + local function with_timestamp(stanza, from) if st.is_stanza(stanza) and stanza.attr.xmlns == nil and stanza.name ~= "iq" then stanza = st.clone(stanza); @@ -81,11 +95,9 @@ end local function manage_buffer(stanza, session) local ctr = session.csi_counter or 0; - if ctr >= queue_size then - session.log("debug", "Queue size limit hit, flushing buffer (queue size is %d)", session.csi_counter); - session.conn:resume_writes(); - elseif module:fire_event("csi-is-stanza-important", { stanza = stanza, session = session }) then - session.log("debug", "Important stanza, flushing buffer (queue size is %d)", session.csi_counter); + local flush, why = should_flush(stanza, session, ctr); + if flush then + session.log("debug", "Flushing buffer (%s; queue size is %d)", why or "important", session.csi_counter); session.conn:resume_writes(); else stanza = with_timestamp(stanza, jid.join(session.username, session.host)) -- cgit v1.2.3