annotate plugins/mod_csi_simple.lua @ 14016:1df1782ccdfe

prosodyctl shell: Improve process exit codes This fixes a bug introduced in 3326c8a29a86 where prosodyctl shell would always exit with non-zero error codes, even on success. Now it will exit with 0 for success, or 1 if any errors were printed. It also updates the other exit codes in the shell command to help distinguish between different error cases. These are based on the sysexits.h codes, although it is not recommended to rely on that interface.
author Matthew Wild <mwild1@gmail.com>
date Fri, 19 Dec 2025 15:32:26 +0000
parents 0ff11f2e87cd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10724
7835b9f14cb8 mod_csi_simple: Allow configuring extra tags indicating importance
Kim Alvefur <zash@zash.se>
parents: 10414
diff changeset
1 -- Copyright (C) 2016-2020 Kim Alvefur
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 --
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 -- This project is MIT/X11 licensed. Please see the
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 -- COPYING file in the source package for more information.
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 --
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 module:depends"csi"
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8
12977
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12552
diff changeset
9 local jid = require "prosody.util.jid";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12552
diff changeset
10 local st = require "prosody.util.stanza";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12552
diff changeset
11 local dt = require "prosody.util.datetime";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12552
diff changeset
12 local filters = require "prosody.util.filters";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12552
diff changeset
13 local timer = require "prosody.util.timer";
13917
598359d9f580 mod_csi_simple: Consider own PEP event broadcasts as high priority
Kim Alvefur <zash@zash.se>
parents: 13213
diff changeset
14 local jid_bare = require "prosody.util.jid".bare;
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15
13213
50324f66ca2a plugins: Use integer config API with interval specification where sensible
Kim Alvefur <zash@zash.se>
parents: 13209
diff changeset
16 local queue_size = module:get_option_integer("csi_queue_size", 256, 1);
13209
c8d949cf6b09 plugins: Switch to :get_option_period() for time range options
Kim Alvefur <zash@zash.se>
parents: 13092
diff changeset
17 local resume_delay = module:get_option_period("csi_resume_inactive_delay", 5);
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18
10724
7835b9f14cb8 mod_csi_simple: Allow configuring extra tags indicating importance
Kim Alvefur <zash@zash.se>
parents: 10414
diff changeset
19 local important_payloads = module:get_option_set("csi_important_payloads", { });
7835b9f14cb8 mod_csi_simple: Allow configuring extra tags indicating importance
Kim Alvefur <zash@zash.se>
parents: 10414
diff changeset
20
10806
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
21 function is_important(stanza) --> boolean, reason: string
10832
7395f6e68dba mod_csi_simple: Report whitespace keepalives
Kim Alvefur <zash@zash.se>
parents: 10831
diff changeset
22 if stanza == " " then
7395f6e68dba mod_csi_simple: Report whitespace keepalives
Kim Alvefur <zash@zash.se>
parents: 10831
diff changeset
23 return true, "whitespace keepalive";
7395f6e68dba mod_csi_simple: Report whitespace keepalives
Kim Alvefur <zash@zash.se>
parents: 10831
diff changeset
24 elseif type(stanza) == "string" then
10831
7dd7cdb43181 mod_csi_simple: Identify raw string data in logging and stats
Kim Alvefur <zash@zash.se>
parents: 10830
diff changeset
25 return true, "raw data";
7dd7cdb43181 mod_csi_simple: Identify raw string data in logging and stats
Kim Alvefur <zash@zash.se>
parents: 10830
diff changeset
26 elseif not st.is_stanza(stanza) then
10833
ac691f305ea7 mod_csi_simple: Report whatever's not a stirng and not a stanza
Kim Alvefur <zash@zash.se>
parents: 10832
diff changeset
27 -- This should probably never happen
ac691f305ea7 mod_csi_simple: Report whatever's not a stirng and not a stanza
Kim Alvefur <zash@zash.se>
parents: 10832
diff changeset
28 return true, type(stanza);
9632
fdefc43bffff mod_csi_simple: Consider non-stanza objects important
Kim Alvefur <zash@zash.se>
parents: 9631
diff changeset
29 end
10770
b4cbe72966c9 mod_csi_simple: Consider nonzas important
Kim Alvefur <zash@zash.se>
parents: 10769
diff changeset
30 if stanza.attr.xmlns ~= nil then
b4cbe72966c9 mod_csi_simple: Consider nonzas important
Kim Alvefur <zash@zash.se>
parents: 10769
diff changeset
31 -- stream errors, stream management etc
10807
b92afa0a4119 mod_csi_simple: Add short reasons to report
Kim Alvefur <zash@zash.se>
parents: 10806
diff changeset
32 return true, "nonza";
10770
b4cbe72966c9 mod_csi_simple: Consider nonzas important
Kim Alvefur <zash@zash.se>
parents: 10769
diff changeset
33 end
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 local st_name = stanza.name;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 if not st_name then return false; end
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 local st_type = stanza.attr.type;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 if st_name == "presence" then
10824
c8430ee33967 mod_csi_simple: Fix treating presence errors as presence updates
Kim Alvefur <zash@zash.se>
parents: 10823
diff changeset
38 if st_type == nil or st_type == "unavailable" or st_type == "error" then
10807
b92afa0a4119 mod_csi_simple: Add short reasons to report
Kim Alvefur <zash@zash.se>
parents: 10806
diff changeset
39 return false, "presence update";
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 end
10801
2b97aac0ea3c mod_csi_simple: Don't consider presence errors as important
Kim Alvefur <zash@zash.se>
parents: 10772
diff changeset
41 -- TODO Some MUC awareness, e.g. check for the 'this relates to you' status code
10807
b92afa0a4119 mod_csi_simple: Add short reasons to report
Kim Alvefur <zash@zash.se>
parents: 10806
diff changeset
42 return true, "subscription request";
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 elseif st_name == "message" then
13917
598359d9f580 mod_csi_simple: Consider own PEP event broadcasts as high priority
Kim Alvefur <zash@zash.se>
parents: 13213
diff changeset
44 if st_type == "error" then
598359d9f580 mod_csi_simple: Consider own PEP event broadcasts as high priority
Kim Alvefur <zash@zash.se>
parents: 13213
diff changeset
45 return true, "delivery failure";
598359d9f580 mod_csi_simple: Consider own PEP event broadcasts as high priority
Kim Alvefur <zash@zash.se>
parents: 13213
diff changeset
46 end
598359d9f580 mod_csi_simple: Consider own PEP event broadcasts as high priority
Kim Alvefur <zash@zash.se>
parents: 13213
diff changeset
47 if stanza:get_child("event", "http://jabber.org/protocol/pubsub#event") then
598359d9f580 mod_csi_simple: Consider own PEP event broadcasts as high priority
Kim Alvefur <zash@zash.se>
parents: 13213
diff changeset
48 if stanza.attr.from == nil or stanza.attr.from == jid_bare(stanza.attr.to) then
598359d9f580 mod_csi_simple: Consider own PEP event broadcasts as high priority
Kim Alvefur <zash@zash.se>
parents: 13213
diff changeset
49 return true, "self-pep-event";
598359d9f580 mod_csi_simple: Consider own PEP event broadcasts as high priority
Kim Alvefur <zash@zash.se>
parents: 13213
diff changeset
50 end
598359d9f580 mod_csi_simple: Consider own PEP event broadcasts as high priority
Kim Alvefur <zash@zash.se>
parents: 13213
diff changeset
51 end
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 if st_type == "headline" then
10807
b92afa0a4119 mod_csi_simple: Add short reasons to report
Kim Alvefur <zash@zash.se>
parents: 10806
diff changeset
53 -- Headline messages are ephemeral by definition
b92afa0a4119 mod_csi_simple: Add short reasons to report
Kim Alvefur <zash@zash.se>
parents: 10806
diff changeset
54 return false, "headline";
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 end
9768
ab12fd48e124 mod_csi_simple: Consider messages forwarded from another of the users clients as important (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9767
diff changeset
56 if stanza:get_child("sent", "urn:xmpp:carbons:2") then
10807
b92afa0a4119 mod_csi_simple: Add short reasons to report
Kim Alvefur <zash@zash.se>
parents: 10806
diff changeset
57 return true, "carbon";
9768
ab12fd48e124 mod_csi_simple: Consider messages forwarded from another of the users clients as important (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9767
diff changeset
58 end
9769
150e9574c149 mod_csi_simple: Unpack Carbons-forwarded messages (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9768
diff changeset
59 local forwarded = stanza:find("{urn:xmpp:carbons:2}received/{urn:xmpp:forward:0}/{jabber:client}message");
150e9574c149 mod_csi_simple: Unpack Carbons-forwarded messages (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9768
diff changeset
60 if forwarded then
150e9574c149 mod_csi_simple: Unpack Carbons-forwarded messages (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9768
diff changeset
61 stanza = forwarded;
150e9574c149 mod_csi_simple: Unpack Carbons-forwarded messages (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9768
diff changeset
62 end
9767
57ceffb13963 mod_csi_simple: Tweak check for <body>
Kim Alvefur <zash@zash.se>
parents: 9651
diff changeset
63 if stanza:get_child("body") then
10807
b92afa0a4119 mod_csi_simple: Add short reasons to report
Kim Alvefur <zash@zash.se>
parents: 10806
diff changeset
64 return true, "body";
9767
57ceffb13963 mod_csi_simple: Tweak check for <body>
Kim Alvefur <zash@zash.se>
parents: 9651
diff changeset
65 end
9770
76cb409db537 mod_csi_simple: Consider messages with subject (eg MUC joins) (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9769
diff changeset
66 if stanza:get_child("subject") then
10807
b92afa0a4119 mod_csi_simple: Add short reasons to report
Kim Alvefur <zash@zash.se>
parents: 10806
diff changeset
67 -- Last step of a MUC join
b92afa0a4119 mod_csi_simple: Add short reasons to report
Kim Alvefur <zash@zash.se>
parents: 10806
diff changeset
68 return true, "subject";
9770
76cb409db537 mod_csi_simple: Consider messages with subject (eg MUC joins) (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9769
diff changeset
69 end
9771
bf92f37de137 mod_csi_simple: Consider messages encrypted payload as important (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9770
diff changeset
70 if stanza:get_child("encryption", "urn:xmpp:eme:0") then
10807
b92afa0a4119 mod_csi_simple: Add short reasons to report
Kim Alvefur <zash@zash.se>
parents: 10806
diff changeset
71 -- Since we can't know what an encrypted message contains, we assume it's important
b92afa0a4119 mod_csi_simple: Add short reasons to report
Kim Alvefur <zash@zash.se>
parents: 10806
diff changeset
72 -- XXX Experimental XEP
b92afa0a4119 mod_csi_simple: Add short reasons to report
Kim Alvefur <zash@zash.se>
parents: 10806
diff changeset
73 return true, "encrypted";
9771
bf92f37de137 mod_csi_simple: Consider messages encrypted payload as important (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9770
diff changeset
74 end
10733
89e0f5cb60a1 mod_csi_simple: Consider MUC invites important
Kim Alvefur <zash@zash.se>
parents: 10724
diff changeset
75 if stanza:get_child("x", "jabber:x:conference") or stanza:find("{http://jabber.org/protocol/muc#user}x/invite") then
10807
b92afa0a4119 mod_csi_simple: Add short reasons to report
Kim Alvefur <zash@zash.se>
parents: 10806
diff changeset
76 return true, "invite";
10733
89e0f5cb60a1 mod_csi_simple: Consider MUC invites important
Kim Alvefur <zash@zash.se>
parents: 10724
diff changeset
77 end
12234
1c47162dd965 plugins: Update for namespace bump in XEP-0353 v0.4.0
Kim Alvefur <zash@zash.se>
parents: 11928
diff changeset
78 if stanza:get_child(nil, "urn:xmpp:jingle-message:0") or stanza:get_child(nil, "urn:xmpp:jingle-message:1") then
11260
08b397c21805 mod_csi_simple,mod_carbons,mod_mam: Update comment about XEP-0353
Kim Alvefur <zash@zash.se>
parents: 10833
diff changeset
79 -- XXX Experimental XEP
10822
af42448a8e98 mod_csi_simple: Fix unintentional order of rules from merge
Kim Alvefur <zash@zash.se>
parents: 10817
diff changeset
80 return true, "jingle call";
af42448a8e98 mod_csi_simple: Fix unintentional order of rules from merge
Kim Alvefur <zash@zash.se>
parents: 10817
diff changeset
81 end
10724
7835b9f14cb8 mod_csi_simple: Allow configuring extra tags indicating importance
Kim Alvefur <zash@zash.se>
parents: 10414
diff changeset
82 for important in important_payloads do
7835b9f14cb8 mod_csi_simple: Allow configuring extra tags indicating importance
Kim Alvefur <zash@zash.se>
parents: 10414
diff changeset
83 if stanza:find(important) then
7835b9f14cb8 mod_csi_simple: Allow configuring extra tags indicating importance
Kim Alvefur <zash@zash.se>
parents: 10414
diff changeset
84 return true;
7835b9f14cb8 mod_csi_simple: Allow configuring extra tags indicating importance
Kim Alvefur <zash@zash.se>
parents: 10414
diff changeset
85 end
9771
bf92f37de137 mod_csi_simple: Consider messages encrypted payload as important (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9770
diff changeset
86 end
9767
57ceffb13963 mod_csi_simple: Tweak check for <body>
Kim Alvefur <zash@zash.se>
parents: 9651
diff changeset
87 return false;
10772
31e702c5f475 mod_csi_simple: Explicitly mention iq stanzas
Kim Alvefur <zash@zash.se>
parents: 10771
diff changeset
88 elseif st_name == "iq" then
31e702c5f475 mod_csi_simple: Explicitly mention iq stanzas
Kim Alvefur <zash@zash.se>
parents: 10771
diff changeset
89 return true;
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 end
10806
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
91 end
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
92
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
93 module:hook("csi-is-stanza-important", function (event)
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
94 local important, why = is_important(event.stanza);
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
95 event.reason = why;
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
96 return important;
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 end, -1);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98
10806
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
99 local function should_flush(stanza, session, ctr) --> boolean, reason: string
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
100 if ctr >= queue_size then
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
101 return true, "queue size limit reached";
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
102 end
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
103 local event = { stanza = stanza, session = session };
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
104 local ret = module:fire_event("csi-is-stanza-important", event)
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
105 return ret, event.reason;
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
106 end
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
107
9911
ed011935c22d mod_csi_simple: Break out stanza timestamping into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 9909
diff changeset
108 local function with_timestamp(stanza, from)
ed011935c22d mod_csi_simple: Break out stanza timestamping into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 9909
diff changeset
109 if st.is_stanza(stanza) and stanza.attr.xmlns == nil and stanza.name ~= "iq" then
ed011935c22d mod_csi_simple: Break out stanza timestamping into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 9909
diff changeset
110 stanza = st.clone(stanza);
ed011935c22d mod_csi_simple: Break out stanza timestamping into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 9909
diff changeset
111 stanza:add_direct_child(st.stanza("delay", {xmlns = "urn:xmpp:delay", from = from, stamp = dt.datetime()}));
ed011935c22d mod_csi_simple: Break out stanza timestamping into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 9909
diff changeset
112 end
ed011935c22d mod_csi_simple: Break out stanza timestamping into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 9909
diff changeset
113 return stanza;
ed011935c22d mod_csi_simple: Break out stanza timestamping into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 9909
diff changeset
114 end
ed011935c22d mod_csi_simple: Break out stanza timestamping into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 9909
diff changeset
115
11834
f54d9abc4e14 mod_csi_simple: Provide custom set of timing buckets
Kim Alvefur <zash@zash.se>
parents: 11573
diff changeset
116 local measure_buffer_hold = module:measure("buffer_hold", "times",
f54d9abc4e14 mod_csi_simple: Provide custom set of timing buckets
Kim Alvefur <zash@zash.se>
parents: 11573
diff changeset
117 { buckets = { 0.1; 1; 5; 10; 15; 30; 60; 120; 180; 300; 600; 900 } });
10829
67a09706e56e mod_csi_simple: Record stats of how long buffers are held
Kim Alvefur <zash@zash.se>
parents: 10828
diff changeset
118
11573
cb5748f94840 mod_csi_simple: convert to use new metric interface for flush reasons
Jonas Schäfer <jonas@wielicki.name>
parents: 11425
diff changeset
119 local flush_reasons = module:metric(
cb5748f94840 mod_csi_simple: convert to use new metric interface for flush reasons
Jonas Schäfer <jonas@wielicki.name>
parents: 11425
diff changeset
120 "counter", "flushes", "",
cb5748f94840 mod_csi_simple: convert to use new metric interface for flush reasons
Jonas Schäfer <jonas@wielicki.name>
parents: 11425
diff changeset
121 "CSI queue flushes",
cb5748f94840 mod_csi_simple: convert to use new metric interface for flush reasons
Jonas Schäfer <jonas@wielicki.name>
parents: 11425
diff changeset
122 { "reason" }
cb5748f94840 mod_csi_simple: convert to use new metric interface for flush reasons
Jonas Schäfer <jonas@wielicki.name>
parents: 11425
diff changeset
123 );
10830
8889d5037aca mod_csi_simple: Collect stats on flush reasons
Kim Alvefur <zash@zash.se>
parents: 10829
diff changeset
124
13991
0ff11f2e87cd mod_csi_simple: Collect stats on why stanzas are held
Kim Alvefur <zash@zash.se>
parents: 13917
diff changeset
125 local hold_reasons = module:metric(
0ff11f2e87cd mod_csi_simple: Collect stats on why stanzas are held
Kim Alvefur <zash@zash.se>
parents: 13917
diff changeset
126 "counter", "hold", "",
0ff11f2e87cd mod_csi_simple: Collect stats on why stanzas are held
Kim Alvefur <zash@zash.se>
parents: 13917
diff changeset
127 "CSI queue held",
0ff11f2e87cd mod_csi_simple: Collect stats on why stanzas are held
Kim Alvefur <zash@zash.se>
parents: 13917
diff changeset
128 { "reason" }
0ff11f2e87cd mod_csi_simple: Collect stats on why stanzas are held
Kim Alvefur <zash@zash.se>
parents: 13917
diff changeset
129 );
0ff11f2e87cd mod_csi_simple: Collect stats on why stanzas are held
Kim Alvefur <zash@zash.se>
parents: 13917
diff changeset
130
12552
b4bc5a715e65 mod_csi_simple: Collect stats on number of stanzas per flush
Kim Alvefur <zash@zash.se>
parents: 12234
diff changeset
131 local flush_sizes = module:metric("histogram", "flush_stanza_count", "", "Number of stanzas flushed at once", {},
b4bc5a715e65 mod_csi_simple: Collect stats on number of stanzas per flush
Kim Alvefur <zash@zash.se>
parents: 12234
diff changeset
132 { buckets = { 0, 1, 2, 4, 8, 16, 32, 64, 128, 256 } }):with_labels();
b4bc5a715e65 mod_csi_simple: Collect stats on number of stanzas per flush
Kim Alvefur <zash@zash.se>
parents: 12234
diff changeset
133
9912
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
134 local function manage_buffer(stanza, session)
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
135 local ctr = session.csi_counter or 0;
11914
8f7946ce7d66 mod_csi_simple: Only act in inactive mode to prevent infinite recursion
Kim Alvefur <zash@zash.se>
parents: 11913
diff changeset
136 if session.state ~= "inactive" then
8f7946ce7d66 mod_csi_simple: Only act in inactive mode to prevent infinite recursion
Kim Alvefur <zash@zash.se>
parents: 11913
diff changeset
137 session.csi_counter = ctr + 1;
8f7946ce7d66 mod_csi_simple: Only act in inactive mode to prevent infinite recursion
Kim Alvefur <zash@zash.se>
parents: 11913
diff changeset
138 return stanza;
8f7946ce7d66 mod_csi_simple: Only act in inactive mode to prevent infinite recursion
Kim Alvefur <zash@zash.se>
parents: 11913
diff changeset
139 end
10806
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
140 local flush, why = should_flush(stanza, session, ctr);
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
141 if flush then
10829
67a09706e56e mod_csi_simple: Record stats of how long buffers are held
Kim Alvefur <zash@zash.se>
parents: 10828
diff changeset
142 if session.csi_measure_buffer_hold then
67a09706e56e mod_csi_simple: Record stats of how long buffers are held
Kim Alvefur <zash@zash.se>
parents: 10828
diff changeset
143 session.csi_measure_buffer_hold();
67a09706e56e mod_csi_simple: Record stats of how long buffers are held
Kim Alvefur <zash@zash.se>
parents: 10828
diff changeset
144 session.csi_measure_buffer_hold = nil;
67a09706e56e mod_csi_simple: Record stats of how long buffers are held
Kim Alvefur <zash@zash.se>
parents: 10828
diff changeset
145 end
11573
cb5748f94840 mod_csi_simple: convert to use new metric interface for flush reasons
Jonas Schäfer <jonas@wielicki.name>
parents: 11425
diff changeset
146 flush_reasons:with_labels(why or "important"):add(1);
12552
b4bc5a715e65 mod_csi_simple: Collect stats on number of stanzas per flush
Kim Alvefur <zash@zash.se>
parents: 12234
diff changeset
147 flush_sizes:sample(ctr);
10806
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
148 session.log("debug", "Flushing buffer (%s; queue size is %d)", why or "important", session.csi_counter);
11379
5c820553ef82 mod_csi_simple: Set session state to 'flushing' while doing so
Kim Alvefur <zash@zash.se>
parents: 11260
diff changeset
149 session.state = "flushing";
11380
9a1758c5aaa4 mod_csi_simple: Fire event when flushing queue
Kim Alvefur <zash@zash.se>
parents: 11379
diff changeset
150 module:fire_event("csi-flushing", { session = session });
11913
75d69e4c54a2 mod_csi_simple: Unlock writes after event, to allow things to be queued
Kim Alvefur <zash@zash.se>
parents: 11834
diff changeset
151 session.conn:resume_writes();
9912
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
152 else
13991
0ff11f2e87cd mod_csi_simple: Collect stats on why stanzas are held
Kim Alvefur <zash@zash.se>
parents: 13917
diff changeset
153 hold_reasons:with_labels(why or "unimportant"):add(1);
10808
0d365c0ee9fe mod_csi_simple: Log reasons for not flushing
Kim Alvefur <zash@zash.se>
parents: 10807
diff changeset
154 session.log("debug", "Holding buffer (%s; queue size is %d)", why or "unimportant", session.csi_counter);
9912
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
155 stanza = with_timestamp(stanza, jid.join(session.username, session.host))
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
156 end
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
157 session.csi_counter = ctr + 1;
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
158 return stanza;
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
159 end
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
160
9913
7d78b24d8449 mod_csi_simple: Trigger buffer flush on seeing incoming data
Kim Alvefur <zash@zash.se>
parents: 9912
diff changeset
161 local function flush_buffer(data, session)
11919
fae5441fc6cf mod_csi_simple: Skip flushing of empty buffer
Kim Alvefur <zash@zash.se>
parents: 11918
diff changeset
162 local ctr = session.csi_counter or 0;
11928
16cf863b36c0 mod_csi_simple: Skip initiating flush in all but inactive state
Kim Alvefur <zash@zash.se>
parents: 11927
diff changeset
163 if ctr == 0 or session.state ~= "inactive" then return data end
10828
c12ed21f877e mod_csi_simple: Change debug message of client-triggered flush for coherence
Kim Alvefur <zash@zash.se>
parents: 10827
diff changeset
164 session.log("debug", "Flushing buffer (%s; queue size is %d)", "client activity", session.csi_counter);
11918
2dc3bc5e137a mod_csi_simple: Fire event when flushing due to client activity
Kim Alvefur <zash@zash.se>
parents: 11916
diff changeset
165 session.state = "flushing";
2dc3bc5e137a mod_csi_simple: Fire event when flushing due to client activity
Kim Alvefur <zash@zash.se>
parents: 11916
diff changeset
166 module:fire_event("csi-flushing", { session = session });
12552
b4bc5a715e65 mod_csi_simple: Collect stats on number of stanzas per flush
Kim Alvefur <zash@zash.se>
parents: 12234
diff changeset
167 flush_sizes:sample(ctr);
11573
cb5748f94840 mod_csi_simple: convert to use new metric interface for flush reasons
Jonas Schäfer <jonas@wielicki.name>
parents: 11425
diff changeset
168 flush_reasons:with_labels("client activity"):add(1);
10829
67a09706e56e mod_csi_simple: Record stats of how long buffers are held
Kim Alvefur <zash@zash.se>
parents: 10828
diff changeset
169 if session.csi_measure_buffer_hold then
67a09706e56e mod_csi_simple: Record stats of how long buffers are held
Kim Alvefur <zash@zash.se>
parents: 10828
diff changeset
170 session.csi_measure_buffer_hold();
67a09706e56e mod_csi_simple: Record stats of how long buffers are held
Kim Alvefur <zash@zash.se>
parents: 10828
diff changeset
171 session.csi_measure_buffer_hold = nil;
67a09706e56e mod_csi_simple: Record stats of how long buffers are held
Kim Alvefur <zash@zash.se>
parents: 10828
diff changeset
172 end
9913
7d78b24d8449 mod_csi_simple: Trigger buffer flush on seeing incoming data
Kim Alvefur <zash@zash.se>
parents: 9912
diff changeset
173 session.conn:resume_writes();
7d78b24d8449 mod_csi_simple: Trigger buffer flush on seeing incoming data
Kim Alvefur <zash@zash.se>
parents: 9912
diff changeset
174 return data;
7d78b24d8449 mod_csi_simple: Trigger buffer flush on seeing incoming data
Kim Alvefur <zash@zash.se>
parents: 9912
diff changeset
175 end
7d78b24d8449 mod_csi_simple: Trigger buffer flush on seeing incoming data
Kim Alvefur <zash@zash.se>
parents: 9912
diff changeset
176
9918
6e9dcec259d0 mod_csi_simple: Separate out functions to enable/disable optimizations
Kim Alvefur <zash@zash.se>
parents: 9917
diff changeset
177 function enable_optimizations(session)
10277
45a58127a3e5 mod_csi_simple: Remove duplicated check for connection
Kim Alvefur <zash@zash.se>
parents: 10025
diff changeset
178 if session.conn and session.conn.pause_writes then
9909
3229be01a08a mod_csi_simple: Use write locks in net.server if available
Kim Alvefur <zash@zash.se>
parents: 9771
diff changeset
179 session.conn:pause_writes();
10829
67a09706e56e mod_csi_simple: Record stats of how long buffers are held
Kim Alvefur <zash@zash.se>
parents: 10828
diff changeset
180 session.csi_measure_buffer_hold = measure_buffer_hold();
10827
d8e83d94a99a mod_csi_simple: Reset queue counter to zero when enabling
Kim Alvefur <zash@zash.se>
parents: 10826
diff changeset
181 session.csi_counter = 0;
13091
214a679823e8 mod_csi_simple: Disable revert-to-inactive timer when going to active mode
Kim Alvefur <zash@zash.se>
parents: 13090
diff changeset
182 if session.csi_resume then
214a679823e8 mod_csi_simple: Disable revert-to-inactive timer when going to active mode
Kim Alvefur <zash@zash.se>
parents: 13090
diff changeset
183 timer.stop(session.csi_resume);
214a679823e8 mod_csi_simple: Disable revert-to-inactive timer when going to active mode
Kim Alvefur <zash@zash.se>
parents: 13090
diff changeset
184 session.csi_resume = nil;
214a679823e8 mod_csi_simple: Disable revert-to-inactive timer when going to active mode
Kim Alvefur <zash@zash.se>
parents: 13090
diff changeset
185 end
9912
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
186 filters.add_filter(session, "stanzas/out", manage_buffer);
9913
7d78b24d8449 mod_csi_simple: Trigger buffer flush on seeing incoming data
Kim Alvefur <zash@zash.se>
parents: 9912
diff changeset
187 filters.add_filter(session, "bytes/in", flush_buffer);
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 else
9917
45b5528b128a mod_csi_simple: Remove old "pump" queue/buffer method, handled in net.server now
Kim Alvefur <zash@zash.se>
parents: 9914
diff changeset
189 session.log("warn", "Session connection does not support write pausing");
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 end
9918
6e9dcec259d0 mod_csi_simple: Separate out functions to enable/disable optimizations
Kim Alvefur <zash@zash.se>
parents: 9917
diff changeset
191 end
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192
9922
06bf5ccd859f mod_csi_simple: Fix type in function name
Matthew Wild <mwild1@gmail.com>
parents: 9921
diff changeset
193 function disable_optimizations(session)
10303
c434bff22b14 mod_csi_simple: Always remove session filters when disabling CSI
Kim Alvefur <zash@zash.se>
parents: 10302
diff changeset
194 filters.remove_filter(session, "stanzas/out", manage_buffer);
c434bff22b14 mod_csi_simple: Always remove session filters when disabling CSI
Kim Alvefur <zash@zash.se>
parents: 10302
diff changeset
195 filters.remove_filter(session, "bytes/in", flush_buffer);
10826
4f7226d5ee30 mod_csi_simple: Forget queue counter when disabling optimizations
Kim Alvefur <zash@zash.se>
parents: 10825
diff changeset
196 session.csi_counter = nil;
13090
3cea237f9d1d mod_csi_simple: Clear delayed active mode timer on disable
Kim Alvefur <zash@zash.se>
parents: 12234
diff changeset
197 if session.csi_resume then
3cea237f9d1d mod_csi_simple: Clear delayed active mode timer on disable
Kim Alvefur <zash@zash.se>
parents: 12234
diff changeset
198 timer.stop(session.csi_resume);
3cea237f9d1d mod_csi_simple: Clear delayed active mode timer on disable
Kim Alvefur <zash@zash.se>
parents: 12234
diff changeset
199 session.csi_resume = nil;
3cea237f9d1d mod_csi_simple: Clear delayed active mode timer on disable
Kim Alvefur <zash@zash.se>
parents: 12234
diff changeset
200 end
10829
67a09706e56e mod_csi_simple: Record stats of how long buffers are held
Kim Alvefur <zash@zash.se>
parents: 10828
diff changeset
201 if session.csi_measure_buffer_hold then
67a09706e56e mod_csi_simple: Record stats of how long buffers are held
Kim Alvefur <zash@zash.se>
parents: 10828
diff changeset
202 session.csi_measure_buffer_hold();
67a09706e56e mod_csi_simple: Record stats of how long buffers are held
Kim Alvefur <zash@zash.se>
parents: 10828
diff changeset
203 session.csi_measure_buffer_hold = nil;
67a09706e56e mod_csi_simple: Record stats of how long buffers are held
Kim Alvefur <zash@zash.se>
parents: 10828
diff changeset
204 end
10277
45a58127a3e5 mod_csi_simple: Remove duplicated check for connection
Kim Alvefur <zash@zash.se>
parents: 10025
diff changeset
205 if session.conn and session.conn.resume_writes then
9909
3229be01a08a mod_csi_simple: Use write locks in net.server if available
Kim Alvefur <zash@zash.se>
parents: 9771
diff changeset
206 session.conn:resume_writes();
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 end
9918
6e9dcec259d0 mod_csi_simple: Separate out functions to enable/disable optimizations
Kim Alvefur <zash@zash.se>
parents: 9917
diff changeset
208 end
6e9dcec259d0 mod_csi_simple: Separate out functions to enable/disable optimizations
Kim Alvefur <zash@zash.se>
parents: 9917
diff changeset
209
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 module:hook("csi-client-inactive", function (event)
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 local session = event.origin;
9918
6e9dcec259d0 mod_csi_simple: Separate out functions to enable/disable optimizations
Kim Alvefur <zash@zash.se>
parents: 9917
diff changeset
212 enable_optimizations(session);
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 end);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 module:hook("csi-client-active", function (event)
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 local session = event.origin;
9922
06bf5ccd859f mod_csi_simple: Fix type in function name
Matthew Wild <mwild1@gmail.com>
parents: 9921
diff changeset
217 disable_optimizations(session);
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 end);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219
10025
4498f601516d mod_csi_simple: Disable optimizations on disconnect (fixes #1358)
Kim Alvefur <zash@zash.se>
parents: 9923
diff changeset
220 module:hook("pre-resource-unbind", function (event)
4498f601516d mod_csi_simple: Disable optimizations on disconnect (fixes #1358)
Kim Alvefur <zash@zash.se>
parents: 9923
diff changeset
221 local session = event.session;
4498f601516d mod_csi_simple: Disable optimizations on disconnect (fixes #1358)
Kim Alvefur <zash@zash.se>
parents: 9923
diff changeset
222 disable_optimizations(session);
10414
51ebfdeccad7 mod_csi_simple: Make sure to disable optimizations before mod_smacks (thanks pep.)
Kim Alvefur <zash@zash.se>
parents: 10303
diff changeset
223 end, 1);
9912
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
224
11926
99444bf26a3d mod_csi_simple: Allow some straggler traffic after flushing buffer
Kim Alvefur <zash@zash.se>
parents: 11919
diff changeset
225 local function resume_optimizations(_, _, session)
99444bf26a3d mod_csi_simple: Allow some straggler traffic after flushing buffer
Kim Alvefur <zash@zash.se>
parents: 11919
diff changeset
226 if (session.state == "flushing" or session.state == "inactive") and session.conn and session.conn.pause_writes then
11379
5c820553ef82 mod_csi_simple: Set session state to 'flushing' while doing so
Kim Alvefur <zash@zash.se>
parents: 11260
diff changeset
227 session.state = "inactive";
9923
e83dfcdeab59 mod_csi_simple: Include queue size in debug messages
Kim Alvefur <zash@zash.se>
parents: 9922
diff changeset
228 session.conn:pause_writes();
10829
67a09706e56e mod_csi_simple: Record stats of how long buffers are held
Kim Alvefur <zash@zash.se>
parents: 10828
diff changeset
229 session.csi_measure_buffer_hold = measure_buffer_hold();
9923
e83dfcdeab59 mod_csi_simple: Include queue size in debug messages
Kim Alvefur <zash@zash.se>
parents: 9922
diff changeset
230 session.log("debug", "Buffer flushed, resuming inactive mode (queue size was %d)", session.csi_counter);
9912
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
231 session.csi_counter = 0;
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 end
11926
99444bf26a3d mod_csi_simple: Allow some straggler traffic after flushing buffer
Kim Alvefur <zash@zash.se>
parents: 11919
diff changeset
233 session.csi_resume = nil;
99444bf26a3d mod_csi_simple: Allow some straggler traffic after flushing buffer
Kim Alvefur <zash@zash.se>
parents: 11919
diff changeset
234 end
99444bf26a3d mod_csi_simple: Allow some straggler traffic after flushing buffer
Kim Alvefur <zash@zash.se>
parents: 11919
diff changeset
235
99444bf26a3d mod_csi_simple: Allow some straggler traffic after flushing buffer
Kim Alvefur <zash@zash.se>
parents: 11919
diff changeset
236 module:hook("c2s-ondrain", function (event)
99444bf26a3d mod_csi_simple: Allow some straggler traffic after flushing buffer
Kim Alvefur <zash@zash.se>
parents: 11919
diff changeset
237 local session = event.session;
99444bf26a3d mod_csi_simple: Allow some straggler traffic after flushing buffer
Kim Alvefur <zash@zash.se>
parents: 11919
diff changeset
238 if (session.state == "flushing" or session.state == "inactive") and session.conn and session.conn.pause_writes then
99444bf26a3d mod_csi_simple: Allow some straggler traffic after flushing buffer
Kim Alvefur <zash@zash.se>
parents: 11919
diff changeset
239 -- After flushing, remain in pseudo-flushing state for a moment to allow
99444bf26a3d mod_csi_simple: Allow some straggler traffic after flushing buffer
Kim Alvefur <zash@zash.se>
parents: 11919
diff changeset
240 -- some followup traffic, iq replies, smacks acks to be sent without having
99444bf26a3d mod_csi_simple: Allow some straggler traffic after flushing buffer
Kim Alvefur <zash@zash.se>
parents: 11919
diff changeset
241 -- to go back and forth between inactive and flush mode.
99444bf26a3d mod_csi_simple: Allow some straggler traffic after flushing buffer
Kim Alvefur <zash@zash.se>
parents: 11919
diff changeset
242 if not session.csi_resume then
99444bf26a3d mod_csi_simple: Allow some straggler traffic after flushing buffer
Kim Alvefur <zash@zash.se>
parents: 11919
diff changeset
243 session.csi_resume = timer.add_task(resume_delay, resume_optimizations, session);
99444bf26a3d mod_csi_simple: Allow some straggler traffic after flushing buffer
Kim Alvefur <zash@zash.se>
parents: 11919
diff changeset
244 end
99444bf26a3d mod_csi_simple: Allow some straggler traffic after flushing buffer
Kim Alvefur <zash@zash.se>
parents: 11919
diff changeset
245 -- Should further noise in this short grace period push back the delay?
99444bf26a3d mod_csi_simple: Allow some straggler traffic after flushing buffer
Kim Alvefur <zash@zash.se>
parents: 11919
diff changeset
246 -- Probably not great if the session can be kept in pseudo-active mode
99444bf26a3d mod_csi_simple: Allow some straggler traffic after flushing buffer
Kim Alvefur <zash@zash.se>
parents: 11919
diff changeset
247 -- indefinitely.
99444bf26a3d mod_csi_simple: Allow some straggler traffic after flushing buffer
Kim Alvefur <zash@zash.se>
parents: 11919
diff changeset
248 end
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
249 end);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
250
9919
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
251 function module.load()
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
252 for _, user_session in pairs(prosody.hosts[module.host].sessions) do
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
253 for _, session in pairs(user_session.sessions) do
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
254 if session.state == "inactive" then
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
255 enable_optimizations(session);
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
256 end
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
257 end
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
258 end
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
259 end
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
260
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
261 function module.unload()
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
262 for _, user_session in pairs(prosody.hosts[module.host].sessions) do
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
263 for _, session in pairs(user_session.sessions) do
11916
5dae9262f81f mod_csi_simple: Detach cleanly from sessions if unloaded while flushing
Kim Alvefur <zash@zash.se>
parents: 11914
diff changeset
264 if session.state and session.state ~= "active" then
9922
06bf5ccd859f mod_csi_simple: Fix type in function name
Matthew Wild <mwild1@gmail.com>
parents: 9921
diff changeset
265 disable_optimizations(session);
9919
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
266 end
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
267 end
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
268 end
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
269 end
11401
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
270
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
271 function module.command(arg)
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
272 if arg[1] ~= "test" then
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
273 print("Usage: "..module.name.." test < test-stream.xml")
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
274 print("");
11425
fc7706fe115d mod_csi_simple: s/algoritm/algorithm/ [codespell]
Kim Alvefur <zash@zash.se>
parents: 11401
diff changeset
275 print("Provide a series of stanzas to test against importance algorithm");
11401
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
276 return 1;
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
277 end
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
278 -- luacheck: ignore 212/self
12977
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12552
diff changeset
279 local xmppstream = require "prosody.util.xmppstream";
11401
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
280 local input_session = { notopen = true }
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
281 local stream_callbacks = { stream_ns = "jabber:client", default_ns = "jabber:client" };
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
282 function stream_callbacks:handlestanza(stanza)
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
283 local important, because = is_important(stanza);
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
284 print("--");
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
285 print(stanza:indent(nil, " "));
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
286 -- :pretty_print() maybe?
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
287 if important then
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
288 print((because or "unspecified reason").. " -> important");
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
289 else
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
290 print((because or "unspecified reason").. " -> unimportant");
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
291 end
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
292 end
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
293 local input_stream = xmppstream.new(input_session, stream_callbacks);
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
294 input_stream:reset();
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
295 input_stream:feed(st.stanza("stream", { xmlns = "jabber:client" }):top_tag());
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
296 input_session.notopen = nil;
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
297
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
298 for line in io.lines() do
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
299 input_stream:feed(line);
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
300 end
228bd43fbc3d mod_csi_simple: Add command to test importance algorithm on stream of stanzas
Kim Alvefur <zash@zash.se>
parents: 11380
diff changeset
301 end