Comparison

plugins/mod_csi_simple.lua @ 10806:24e2b571d29a

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
author Kim Alvefur <zash@zash.se>
date Thu, 07 May 2020 21:55:29 +0200
parent 10801:2b97aac0ea3c
child 10807:b92afa0a4119
comparison
equal deleted inserted replaced
10805:d17392022cb2 10806:24e2b571d29a
13 13
14 local queue_size = module:get_option_number("csi_queue_size", 256); 14 local queue_size = module:get_option_number("csi_queue_size", 256);
15 15
16 local important_payloads = module:get_option_set("csi_important_payloads", { }); 16 local important_payloads = module:get_option_set("csi_important_payloads", { });
17 17
18 module:hook("csi-is-stanza-important", function (event) 18 function is_important(stanza) --> boolean, reason: string
19 local stanza = event.stanza;
20 if not st.is_stanza(stanza) then 19 if not st.is_stanza(stanza) then
21 -- whitespace pings etc 20 -- whitespace pings etc
22 return true; 21 return true;
23 end 22 end
24 if stanza.attr.xmlns ~= nil then 23 if stanza.attr.xmlns ~= nil then
67 end 66 end
68 return false; 67 return false;
69 elseif st_name == "iq" then 68 elseif st_name == "iq" then
70 return true; 69 return true;
71 end 70 end
71 end
72
73 module:hook("csi-is-stanza-important", function (event)
74 local important, why = is_important(event.stanza);
75 event.reason = why;
76 return important;
72 end, -1); 77 end, -1);
78
79 local function should_flush(stanza, session, ctr) --> boolean, reason: string
80 if ctr >= queue_size then
81 return true, "queue size limit reached";
82 end
83 local event = { stanza = stanza, session = session };
84 local ret = module:fire_event("csi-is-stanza-important", event)
85 return ret, event.reason;
86 end
73 87
74 local function with_timestamp(stanza, from) 88 local function with_timestamp(stanza, from)
75 if st.is_stanza(stanza) and stanza.attr.xmlns == nil and stanza.name ~= "iq" then 89 if st.is_stanza(stanza) and stanza.attr.xmlns == nil and stanza.name ~= "iq" then
76 stanza = st.clone(stanza); 90 stanza = st.clone(stanza);
77 stanza:add_direct_child(st.stanza("delay", {xmlns = "urn:xmpp:delay", from = from, stamp = dt.datetime()})); 91 stanza:add_direct_child(st.stanza("delay", {xmlns = "urn:xmpp:delay", from = from, stamp = dt.datetime()}));
79 return stanza; 93 return stanza;
80 end 94 end
81 95
82 local function manage_buffer(stanza, session) 96 local function manage_buffer(stanza, session)
83 local ctr = session.csi_counter or 0; 97 local ctr = session.csi_counter or 0;
84 if ctr >= queue_size then 98 local flush, why = should_flush(stanza, session, ctr);
85 session.log("debug", "Queue size limit hit, flushing buffer (queue size is %d)", session.csi_counter); 99 if flush then
86 session.conn:resume_writes(); 100 session.log("debug", "Flushing buffer (%s; queue size is %d)", why or "important", session.csi_counter);
87 elseif module:fire_event("csi-is-stanza-important", { stanza = stanza, session = session }) then
88 session.log("debug", "Important stanza, flushing buffer (queue size is %d)", session.csi_counter);
89 session.conn:resume_writes(); 101 session.conn:resume_writes();
90 else 102 else
91 stanza = with_timestamp(stanza, jid.join(session.username, session.host)) 103 stanza = with_timestamp(stanza, jid.join(session.username, session.host))
92 end 104 end
93 session.csi_counter = ctr + 1; 105 session.csi_counter = ctr + 1;