Annotate

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
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
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local jid = require "util.jid";
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local st = require "util.stanza";
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local dt = require "util.datetime";
9912
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
12 local filters = require "util.filters";
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local queue_size = module:get_option_number("csi_queue_size", 256);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15
10724
7835b9f14cb8 mod_csi_simple: Allow configuring extra tags indicating importance
Kim Alvefur <zash@zash.se>
parents: 10414
diff changeset
16 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
17
10806
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
18 function is_important(stanza) --> boolean, reason: string
9632
fdefc43bffff mod_csi_simple: Consider non-stanza objects important
Kim Alvefur <zash@zash.se>
parents: 9631
diff changeset
19 if not st.is_stanza(stanza) then
10771
c562edcf51bf mod_csi_simple: Clarify what might not be stanzas here
Kim Alvefur <zash@zash.se>
parents: 10770
diff changeset
20 -- whitespace pings etc
9632
fdefc43bffff mod_csi_simple: Consider non-stanza objects important
Kim Alvefur <zash@zash.se>
parents: 9631
diff changeset
21 return true;
fdefc43bffff mod_csi_simple: Consider non-stanza objects important
Kim Alvefur <zash@zash.se>
parents: 9631
diff changeset
22 end
10770
b4cbe72966c9 mod_csi_simple: Consider nonzas important
Kim Alvefur <zash@zash.se>
parents: 10769
diff changeset
23 if stanza.attr.xmlns ~= nil then
b4cbe72966c9 mod_csi_simple: Consider nonzas important
Kim Alvefur <zash@zash.se>
parents: 10769
diff changeset
24 -- stream errors, stream management etc
b4cbe72966c9 mod_csi_simple: Consider nonzas important
Kim Alvefur <zash@zash.se>
parents: 10769
diff changeset
25 return true;
b4cbe72966c9 mod_csi_simple: Consider nonzas important
Kim Alvefur <zash@zash.se>
parents: 10769
diff changeset
26 end
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 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
28 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
29 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
30 if st_name == "presence" then
10801
2b97aac0ea3c mod_csi_simple: Don't consider presence errors as important
Kim Alvefur <zash@zash.se>
parents: 10772
diff changeset
31 if st_type == nil or st_type == "unavailable" or st_name == "error" then
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 return false;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 end
10801
2b97aac0ea3c mod_csi_simple: Don't consider presence errors as important
Kim Alvefur <zash@zash.se>
parents: 10772
diff changeset
34 -- TODO Some MUC awareness, e.g. check for the 'this relates to you' status code
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 return true;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 elseif st_name == "message" then
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_type == "headline" then
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 return false;
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 end
10801
2b97aac0ea3c mod_csi_simple: Don't consider presence errors as important
Kim Alvefur <zash@zash.se>
parents: 10772
diff changeset
40 if st_type == "error" then
2b97aac0ea3c mod_csi_simple: Don't consider presence errors as important
Kim Alvefur <zash@zash.se>
parents: 10772
diff changeset
41 return true;
2b97aac0ea3c mod_csi_simple: Don't consider presence errors as important
Kim Alvefur <zash@zash.se>
parents: 10772
diff changeset
42 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
43 if stanza:get_child("sent", "urn:xmpp:carbons:2") then
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
44 return true;
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
45 end
9769
150e9574c149 mod_csi_simple: Unpack Carbons-forwarded messages (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9768
diff changeset
46 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
47 if forwarded then
150e9574c149 mod_csi_simple: Unpack Carbons-forwarded messages (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9768
diff changeset
48 stanza = forwarded;
150e9574c149 mod_csi_simple: Unpack Carbons-forwarded messages (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9768
diff changeset
49 end
9767
57ceffb13963 mod_csi_simple: Tweak check for <body>
Kim Alvefur <zash@zash.se>
parents: 9651
diff changeset
50 if stanza:get_child("body") then
57ceffb13963 mod_csi_simple: Tweak check for <body>
Kim Alvefur <zash@zash.se>
parents: 9651
diff changeset
51 return true;
57ceffb13963 mod_csi_simple: Tweak check for <body>
Kim Alvefur <zash@zash.se>
parents: 9651
diff changeset
52 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
53 if stanza:get_child("subject") then
76cb409db537 mod_csi_simple: Consider messages with subject (eg MUC joins) (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9769
diff changeset
54 return true;
76cb409db537 mod_csi_simple: Consider messages with subject (eg MUC joins) (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9769
diff changeset
55 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
56 if stanza:get_child("encryption", "urn:xmpp:eme:0") then
bf92f37de137 mod_csi_simple: Consider messages encrypted payload as important (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9770
diff changeset
57 return true;
bf92f37de137 mod_csi_simple: Consider messages encrypted payload as important (fixes part of #1250)
Kim Alvefur <zash@zash.se>
parents: 9770
diff changeset
58 end
10733
89e0f5cb60a1 mod_csi_simple: Consider MUC invites important
Kim Alvefur <zash@zash.se>
parents: 10724
diff changeset
59 if stanza:get_child("x", "jabber:x:conference") or stanza:find("{http://jabber.org/protocol/muc#user}x/invite") then
89e0f5cb60a1 mod_csi_simple: Consider MUC invites important
Kim Alvefur <zash@zash.se>
parents: 10724
diff changeset
60 return true;
89e0f5cb60a1 mod_csi_simple: Consider MUC invites important
Kim Alvefur <zash@zash.se>
parents: 10724
diff changeset
61 end
10724
7835b9f14cb8 mod_csi_simple: Allow configuring extra tags indicating importance
Kim Alvefur <zash@zash.se>
parents: 10414
diff changeset
62 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
63 if stanza:find(important) then
7835b9f14cb8 mod_csi_simple: Allow configuring extra tags indicating importance
Kim Alvefur <zash@zash.se>
parents: 10414
diff changeset
64 return true;
7835b9f14cb8 mod_csi_simple: Allow configuring extra tags indicating importance
Kim Alvefur <zash@zash.se>
parents: 10414
diff changeset
65 end
7835b9f14cb8 mod_csi_simple: Allow configuring extra tags indicating importance
Kim Alvefur <zash@zash.se>
parents: 10414
diff changeset
66 end
9767
57ceffb13963 mod_csi_simple: Tweak check for <body>
Kim Alvefur <zash@zash.se>
parents: 9651
diff changeset
67 return false;
10772
31e702c5f475 mod_csi_simple: Explicitly mention iq stanzas
Kim Alvefur <zash@zash.se>
parents: 10771
diff changeset
68 elseif st_name == "iq" then
31e702c5f475 mod_csi_simple: Explicitly mention iq stanzas
Kim Alvefur <zash@zash.se>
parents: 10771
diff changeset
69 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
70 end
10806
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
71 end
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
72
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
73 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
74 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
75 event.reason = why;
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
76 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
77 end, -1);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78
10806
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
79 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
80 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
81 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
82 end
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
83 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
84 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
85 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
86 end
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
87
9911
ed011935c22d mod_csi_simple: Break out stanza timestamping into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 9909
diff changeset
88 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
89 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
90 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
91 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
92 end
ed011935c22d mod_csi_simple: Break out stanza timestamping into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 9909
diff changeset
93 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
94 end
ed011935c22d mod_csi_simple: Break out stanza timestamping into a function for future reuse
Kim Alvefur <zash@zash.se>
parents: 9909
diff changeset
95
9912
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
96 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
97 local ctr = session.csi_counter or 0;
10806
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
98 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
99 if flush then
24e2b571d29a mod_csi_simple: Refactor to allow logging reason for buffer flush
Kim Alvefur <zash@zash.se>
parents: 10801
diff changeset
100 session.log("debug", "Flushing buffer (%s; queue size is %d)", why or "important", 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
101 session.conn:resume_writes();
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
102 else
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
103 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
104 end
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
105 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
106 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
107 end
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
108
9913
7d78b24d8449 mod_csi_simple: Trigger buffer flush on seeing incoming data
Kim Alvefur <zash@zash.se>
parents: 9912
diff changeset
109 local function flush_buffer(data, session)
10302
4c53e58d2468 mod_csi_simple: Try not to flush buffer while already flushing it
Kim Alvefur <zash@zash.se>
parents: 10277
diff changeset
110 if session.csi_flushing then
4c53e58d2468 mod_csi_simple: Try not to flush buffer while already flushing it
Kim Alvefur <zash@zash.se>
parents: 10277
diff changeset
111 return data;
4c53e58d2468 mod_csi_simple: Try not to flush buffer while already flushing it
Kim Alvefur <zash@zash.se>
parents: 10277
diff changeset
112 end
4c53e58d2468 mod_csi_simple: Try not to flush buffer while already flushing it
Kim Alvefur <zash@zash.se>
parents: 10277
diff changeset
113 session.csi_flushing = true;
9923
e83dfcdeab59 mod_csi_simple: Include queue size in debug messages
Kim Alvefur <zash@zash.se>
parents: 9922
diff changeset
114 session.log("debug", "Client sent something, flushing buffer once (queue size is %d)", session.csi_counter);
9913
7d78b24d8449 mod_csi_simple: Trigger buffer flush on seeing incoming data
Kim Alvefur <zash@zash.se>
parents: 9912
diff changeset
115 session.conn:resume_writes();
7d78b24d8449 mod_csi_simple: Trigger buffer flush on seeing incoming data
Kim Alvefur <zash@zash.se>
parents: 9912
diff changeset
116 return data;
7d78b24d8449 mod_csi_simple: Trigger buffer flush on seeing incoming data
Kim Alvefur <zash@zash.se>
parents: 9912
diff changeset
117 end
7d78b24d8449 mod_csi_simple: Trigger buffer flush on seeing incoming data
Kim Alvefur <zash@zash.se>
parents: 9912
diff changeset
118
9918
6e9dcec259d0 mod_csi_simple: Separate out functions to enable/disable optimizations
Kim Alvefur <zash@zash.se>
parents: 9917
diff changeset
119 function enable_optimizations(session)
10277
45a58127a3e5 mod_csi_simple: Remove duplicated check for connection
Kim Alvefur <zash@zash.se>
parents: 10025
diff changeset
120 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
121 session.conn:pause_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
122 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
123 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
124 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
125 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
126 end
9918
6e9dcec259d0 mod_csi_simple: Separate out functions to enable/disable optimizations
Kim Alvefur <zash@zash.se>
parents: 9917
diff changeset
127 end
9589
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128
9922
06bf5ccd859f mod_csi_simple: Fix type in function name
Matthew Wild <mwild1@gmail.com>
parents: 9921
diff changeset
129 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
130 session.csi_flushing = nil;
c434bff22b14 mod_csi_simple: Always remove session filters when disabling CSI
Kim Alvefur <zash@zash.se>
parents: 10302
diff changeset
131 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
132 filters.remove_filter(session, "bytes/in", flush_buffer);
10277
45a58127a3e5 mod_csi_simple: Remove duplicated check for connection
Kim Alvefur <zash@zash.se>
parents: 10025
diff changeset
133 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
134 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
135 end
9918
6e9dcec259d0 mod_csi_simple: Separate out functions to enable/disable optimizations
Kim Alvefur <zash@zash.se>
parents: 9917
diff changeset
136 end
6e9dcec259d0 mod_csi_simple: Separate out functions to enable/disable optimizations
Kim Alvefur <zash@zash.se>
parents: 9917
diff changeset
137
6e9dcec259d0 mod_csi_simple: Separate out functions to enable/disable optimizations
Kim Alvefur <zash@zash.se>
parents: 9917
diff changeset
138 module:hook("csi-client-inactive", function (event)
6e9dcec259d0 mod_csi_simple: Separate out functions to enable/disable optimizations
Kim Alvefur <zash@zash.se>
parents: 9917
diff changeset
139 local session = event.origin;
6e9dcec259d0 mod_csi_simple: Separate out functions to enable/disable optimizations
Kim Alvefur <zash@zash.se>
parents: 9917
diff changeset
140 enable_optimizations(session);
6e9dcec259d0 mod_csi_simple: Separate out functions to enable/disable optimizations
Kim Alvefur <zash@zash.se>
parents: 9917
diff changeset
141 end);
6e9dcec259d0 mod_csi_simple: Separate out functions to enable/disable optimizations
Kim Alvefur <zash@zash.se>
parents: 9917
diff changeset
142
6e9dcec259d0 mod_csi_simple: Separate out functions to enable/disable optimizations
Kim Alvefur <zash@zash.se>
parents: 9917
diff changeset
143 module:hook("csi-client-active", function (event)
6e9dcec259d0 mod_csi_simple: Separate out functions to enable/disable optimizations
Kim Alvefur <zash@zash.se>
parents: 9917
diff changeset
144 local session = event.origin;
9922
06bf5ccd859f mod_csi_simple: Fix type in function name
Matthew Wild <mwild1@gmail.com>
parents: 9921
diff changeset
145 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
146 end);
aeb054ee88c5 mod_csi_simple: Import modified version of mod_csi_pump from prosody-modules
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147
10025
4498f601516d mod_csi_simple: Disable optimizations on disconnect (fixes #1358)
Kim Alvefur <zash@zash.se>
parents: 9923
diff changeset
148 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
149 local session = event.session;
4498f601516d mod_csi_simple: Disable optimizations on disconnect (fixes #1358)
Kim Alvefur <zash@zash.se>
parents: 9923
diff changeset
150 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
151 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
152
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
153 module:hook("c2s-ondrain", function (event)
601f9781a605 mod_csi_simple: Count buffered items and flush when it reaches configured limit
Kim Alvefur <zash@zash.se>
parents: 9911
diff changeset
154 local session = event.session;
10277
45a58127a3e5 mod_csi_simple: Remove duplicated check for connection
Kim Alvefur <zash@zash.se>
parents: 10025
diff changeset
155 if session.state == "inactive" and session.conn and session.conn.pause_writes then
9923
e83dfcdeab59 mod_csi_simple: Include queue size in debug messages
Kim Alvefur <zash@zash.se>
parents: 9922
diff changeset
156 session.conn:pause_writes();
e83dfcdeab59 mod_csi_simple: Include queue size in debug messages
Kim Alvefur <zash@zash.se>
parents: 9922
diff changeset
157 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
158 session.csi_counter = 0;
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 end);
9919
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
161
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
162 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
163 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
164 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
165 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
166 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
167 end
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
168 end
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
169 end
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
170 end
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
171
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
172 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
173 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
174 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
175 if session.state == "inactive" then
9922
06bf5ccd859f mod_csi_simple: Fix type in function name
Matthew Wild <mwild1@gmail.com>
parents: 9921
diff changeset
176 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
177 end
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
178 end
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
179 end
d602a495409b mod_csi_simple: Disable optimizations on unload and re-enable on load
Kim Alvefur <zash@zash.se>
parents: 9918
diff changeset
180 end