Software /
code /
prosody
Annotate
plugins/mod_smacks.lua @ 12070:e662c172ea0f
mod_smacks: Remove references to connection on hibernation
Still having the connection on the session may cause unintentional
behavior, such as the session being treated as if connected, even tho
the connection has been closed.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 18 Dec 2021 12:50:53 +0100 |
parent | 12069:b9e08cbd032b |
child | 12072:ef0f174488af |
rev | line source |
---|---|
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- XEP-0198: Stream Management for Prosody IM |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 -- |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 -- Copyright (C) 2010-2015 Matthew Wild |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 -- Copyright (C) 2010 Waqas Hussain |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 -- Copyright (C) 2012-2021 Kim Alvefur |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 -- Copyright (C) 2012 Thijs Alkemade |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 -- Copyright (C) 2014 Florian Zeitz |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 -- Copyright (C) 2016-2020 Thilo Molitor |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 -- |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 -- This project is MIT/X11 licensed. Please see the |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 -- COPYING file in the source package for more information. |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 -- |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 |
11981
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
14 local tonumber = tonumber; |
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
15 local tostring = tostring; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 local os_time = os.time; |
11981
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
17 |
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
18 local datetime = require "util.datetime"; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 local add_filter = require "util.filters".add_filter; |
11981
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
20 local jid = require "util.jid"; |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
21 local smqueue = require "util.smqueue"; |
11981
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
22 local st = require "util.stanza"; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 local timer = require "util.timer"; |
11981
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
24 local uuid_generate = require "util.uuid".generate; |
12054
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
25 local watchdog = require "util.watchdog"; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 |
11981
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
27 local sessionmanager = require "core.sessionmanager"; |
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
28 local core_process_stanza = prosody.core_process_stanza; |
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
29 |
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
30 local xmlns_errors = "urn:ietf:params:xml:ns:xmpp-stanzas"; |
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
31 local xmlns_delay = "urn:xmpp:delay"; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 local xmlns_mam2 = "urn:xmpp:mam:2"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 local xmlns_sm2 = "urn:xmpp:sm:2"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 local xmlns_sm3 = "urn:xmpp:sm:3"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 local sm2_attr = { xmlns = xmlns_sm2 }; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 local sm3_attr = { xmlns = xmlns_sm3 }; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
39 local queue_size = module:get_option_number("smacks_max_queue_size", 500); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 local resume_timeout = module:get_option_number("smacks_hibernation_time", 600); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 local s2s_smacks = module:get_option_boolean("smacks_enabled_s2s", true); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 local s2s_resend = module:get_option_boolean("smacks_s2s_resend", false); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 local max_unacked_stanzas = module:get_option_number("smacks_max_unacked_stanzas", 0); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 local max_inactive_unacked_stanzas = module:get_option_number("smacks_max_inactive_unacked_stanzas", 256); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 local delayed_ack_timeout = module:get_option_number("smacks_max_ack_delay", 30); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 local c2s_sessions = module:shared("/*/c2s/sessions"); |
12063
d308f6901397
mod_smacks: Simplify access to local user sessions
Kim Alvefur <zash@zash.se>
parents:
12062
diff
changeset
|
48 local local_sessions = prosody.hosts[module.host].sessions; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 |
11977
9f7a6f7d13de
mod_smacks: Factor out formatting of 'h' value
Kim Alvefur <zash@zash.se>
parents:
11976
diff
changeset
|
50 local function format_h(h) if h then return string.format("%d", h) end end |
9f7a6f7d13de
mod_smacks: Factor out formatting of 'h' value
Kim Alvefur <zash@zash.se>
parents:
11976
diff
changeset
|
51 |
12051
c32ef09ab452
mod_smacks: Persist old counter values to storage
Kim Alvefur <zash@zash.se>
parents:
12050
diff
changeset
|
52 local old_session_registry = module:open_store("smacks_h", "map"); |
12053
03e9587fbfd2
mod_smacks: Switch storage for tracking resumption tokens
Kim Alvefur <zash@zash.se>
parents:
12052
diff
changeset
|
53 local session_registry = module:shared "/*/smacks/resumption-tokens"; -- > user@host/resumption-token --> resource |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
55 local ack_errors = require"util.error".init("mod_smacks", xmlns_sm3, { |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
56 head = { condition = "undefined-condition"; text = "Client acknowledged more stanzas than sent by server" }; |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
57 tail = { condition = "undefined-condition"; text = "Client acknowledged less stanzas than already acknowledged" }; |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
58 pop = { condition = "internal-server-error"; text = "Something went wrong with Stream Management" }; |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
59 overflow = { condition = "resource-constraint", text = "Too many unacked stanzas remaining, session can't be resumed" } |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
60 }); |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
61 |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
62 -- COMPAT note the use of compatibilty wrapper in events (queue:table()) |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
63 |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
64 local function ack_delayed(session, stanza) |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 -- fire event only if configured to do so and our session is not already hibernated or destroyed |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 if delayed_ack_timeout > 0 and session.awaiting_ack |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 and not session.hibernating and not session.destroyed then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 session.log("debug", "Firing event 'smacks-ack-delayed', queue = %d", |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
69 session.outgoing_stanza_queue and session.outgoing_stanza_queue:count_unacked() or 0); |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
70 module:fire_event("smacks-ack-delayed", {origin = session, queue = session.outgoing_stanza_queue:table(), stanza = stanza}); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 session.delayed_ack_timer = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 local function can_do_smacks(session, advertise_only) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 if session.smacks then return false, "unexpected-request", "Stream management is already enabled"; end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 local session_type = session.type; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 if session.username then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 if not(advertise_only) and not(session.resource) then -- Fail unless we're only advertising sm |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 return false, "unexpected-request", "Client must bind a resource before enabling stream management"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 elseif s2s_smacks and (session_type == "s2sin" or session_type == "s2sout") then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 return false, "service-unavailable", "Stream management is not available for this stream"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 module:hook("stream-features", |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 function (event) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 if can_do_smacks(event.origin, true) then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 event.features:tag("sm", sm2_attr):tag("optional"):up():up(); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 event.features:tag("sm", sm3_attr):tag("optional"):up():up(); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 module:hook("s2s-stream-features", |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 function (event) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 if can_do_smacks(event.origin, true) then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 event.features:tag("sm", sm2_attr):tag("optional"):up():up(); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 event.features:tag("sm", sm3_attr):tag("optional"):up():up(); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 |
11937
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
106 local function should_ack(session, force) |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
107 if not session then return end -- shouldn't be possible |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
108 if session.destroyed then return end -- gone |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
109 if not session.smacks then return end -- not using |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
110 if session.hibernating then return end -- can't ack when asleep |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
111 if session.awaiting_ack then return end -- already waiting |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
112 if force then return force end |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 local queue = session.outgoing_stanza_queue; |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
114 local expected_h = session.last_acknowledged_stanza + queue:count_unacked(); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 local max_unacked = max_unacked_stanzas; |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
116 if session.state == "inactive" then |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 max_unacked = max_inactive_unacked_stanzas; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 end |
11937
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
119 -- this check of last_requested_h prevents ack-loops if missbehaving clients report wrong |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
120 -- stanza counts. it is set when an <r> is really sent (e.g. inside timer), preventing any |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
121 -- further requests until a higher h-value would be expected. |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
122 return queue:count_unacked() > max_unacked and expected_h ~= session.last_requested_h; |
11937
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
123 end |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
124 |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
125 local function request_ack(session, reason) |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
126 local queue = session.outgoing_stanza_queue; |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
127 session.log("debug", "Sending <r> (inside timer, before send) from %s - #queue=%d", reason, queue:count_unacked()); |
11937
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
128 (session.sends2s or session.send)(st.stanza("r", { xmlns = session.smacks })) |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
129 if session.destroyed then return end -- sending something can trigger destruction |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
130 session.awaiting_ack = true; |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
131 -- expected_h could be lower than this expression e.g. more stanzas added to the queue meanwhile) |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
132 session.last_requested_h = session.last_acknowledged_stanza + queue:count_unacked(); |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
133 session.log("debug", "Sending <r> (inside timer, after send) from %s - #queue=%d", reason, queue:count_unacked()); |
11937
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
134 if not session.delayed_ack_timer then |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
135 session.delayed_ack_timer = timer.add_task(delayed_ack_timeout, function() |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
136 ack_delayed(session, nil); -- we don't know if this is the only new stanza in the queue |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
137 end); |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
138 end |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
139 end |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
140 |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
141 local function request_ack_now_if_needed(session, force, reason) |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
142 if should_ack(session, force) then |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
143 request_ack(session, reason); |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
144 end |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
145 end |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
146 |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 local function outgoing_stanza_filter(stanza, session) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 -- XXX: Normally you wouldn't have to check the xmlns for a stanza as it's |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 -- supposed to be nil. |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
150 -- However, when using mod_smacks with mod_websocket, then mod_websocket's |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
151 -- stanzas/out filter can get called before this one and adds the xmlns. |
12045
a6c821720cb6
mod_smacks: Back out 48f8fa6cea7b - back in a86ae74da96c again
Kim Alvefur <zash@zash.se>
parents:
12044
diff
changeset
|
152 if session.resending_unacked then return stanza end |
11979
705c4c07a860
mod_smacks: Use stanza type checking function for correctness
Kim Alvefur <zash@zash.se>
parents:
11978
diff
changeset
|
153 local is_stanza = st.is_stanza(stanza) and |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
154 (not stanza.attr.xmlns or stanza.attr.xmlns == 'jabber:client') |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 and not stanza.name:find":"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
156 |
12045
a6c821720cb6
mod_smacks: Back out 48f8fa6cea7b - back in a86ae74da96c again
Kim Alvefur <zash@zash.se>
parents:
12044
diff
changeset
|
157 if is_stanza then |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 local queue = session.outgoing_stanza_queue; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 local cached_stanza = st.clone(stanza); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 |
11983
27f2539b4f87
mod_smacks: Remove dead conditional
Kim Alvefur <zash@zash.se>
parents:
11982
diff
changeset
|
161 if cached_stanza.name ~= "iq" and cached_stanza:get_child("delay", xmlns_delay) == nil then |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
162 cached_stanza = cached_stanza:tag("delay", { |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
163 xmlns = xmlns_delay, |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 from = jid.bare(session.full_jid or session.host), |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 stamp = datetime.datetime() |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 }); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
169 queue:push(cached_stanza); |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
170 |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 if session.hibernating then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 session.log("debug", "hibernating since %s, stanza queued", datetime.datetime(session.hibernating)); |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
173 -- FIXME queue implementation changed, anything depending on it being an array will break |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
174 module:fire_event("smacks-hibernation-stanza-queued", {origin = session, queue = queue:table(), stanza = cached_stanza}); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 return nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 return stanza; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 local function count_incoming_stanzas(stanza, session) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 if not stanza.attr.xmlns then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
183 session.handled_stanza_count = session.handled_stanza_count + 1; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
184 session.log("debug", "Handled %d incoming stanzas", session.handled_stanza_count); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
185 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 return stanza; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 local function wrap_session_out(session, resume) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 if not resume then |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
191 session.outgoing_stanza_queue = smqueue.new(queue_size); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 session.last_acknowledged_stanza = 0; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
193 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 add_filter(session, "stanzas/out", outgoing_stanza_filter, -999); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 return session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 |
11975
fcea4d9e7502
mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents:
11952
diff
changeset
|
200 module:hook("pre-session-close", function(event) |
fcea4d9e7502
mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents:
11952
diff
changeset
|
201 local session = event.session; |
fcea4d9e7502
mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents:
11952
diff
changeset
|
202 if session.resumption_token then |
12069 | 203 session.log("debug", "Revoking resumption token"); |
12053
03e9587fbfd2
mod_smacks: Switch storage for tracking resumption tokens
Kim Alvefur <zash@zash.se>
parents:
12052
diff
changeset
|
204 session_registry[jid.join(session.username, session.host, session.resumption_token)] = nil; |
12051
c32ef09ab452
mod_smacks: Persist old counter values to storage
Kim Alvefur <zash@zash.se>
parents:
12050
diff
changeset
|
205 old_session_registry:set(session.username, session.resumption_token, nil); |
11975
fcea4d9e7502
mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents:
11952
diff
changeset
|
206 session.resumption_token = nil; |
12069 | 207 else |
208 session.log("debug", "Session not resumable"); | |
11975
fcea4d9e7502
mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents:
11952
diff
changeset
|
209 end |
12062
4972244fe87b
mod_smacks: Cancel hibernation when session is closed
Kim Alvefur <zash@zash.se>
parents:
12061
diff
changeset
|
210 if session.hibernating_watchdog then |
12069 | 211 session.log("debug", "Removing sleeping watchdog"); |
12062
4972244fe87b
mod_smacks: Cancel hibernation when session is closed
Kim Alvefur <zash@zash.se>
parents:
12061
diff
changeset
|
212 -- If the session is being replaced instead of resume, we don't want the |
4972244fe87b
mod_smacks: Cancel hibernation when session is closed
Kim Alvefur <zash@zash.se>
parents:
12061
diff
changeset
|
213 -- old session around to time out and cause trouble for the new session |
4972244fe87b
mod_smacks: Cancel hibernation when session is closed
Kim Alvefur <zash@zash.se>
parents:
12061
diff
changeset
|
214 session.hibernating_watchdog:cancel(); |
4972244fe87b
mod_smacks: Cancel hibernation when session is closed
Kim Alvefur <zash@zash.se>
parents:
12061
diff
changeset
|
215 session.hibernating_watchdog = nil; |
12069 | 216 else |
217 session.log("debug", "No watchdog set"); | |
12062
4972244fe87b
mod_smacks: Cancel hibernation when session is closed
Kim Alvefur <zash@zash.se>
parents:
12061
diff
changeset
|
218 end |
11975
fcea4d9e7502
mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents:
11952
diff
changeset
|
219 -- send out last ack as per revision 1.5.2 of XEP-0198 |
fcea4d9e7502
mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents:
11952
diff
changeset
|
220 if session.smacks and session.conn and session.handled_stanza_count then |
fcea4d9e7502
mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents:
11952
diff
changeset
|
221 (session.sends2s or session.send)(st.stanza("a", { |
fcea4d9e7502
mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents:
11952
diff
changeset
|
222 xmlns = session.smacks; |
11977
9f7a6f7d13de
mod_smacks: Factor out formatting of 'h' value
Kim Alvefur <zash@zash.se>
parents:
11976
diff
changeset
|
223 h = format_h(session.handled_stanza_count); |
11975
fcea4d9e7502
mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents:
11952
diff
changeset
|
224 })); |
fcea4d9e7502
mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents:
11952
diff
changeset
|
225 end |
fcea4d9e7502
mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents:
11952
diff
changeset
|
226 end); |
fcea4d9e7502
mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents:
11952
diff
changeset
|
227 |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
228 local function wrap_session_in(session, resume) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
229 if not resume then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
230 session.handled_stanza_count = 0; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
231 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
232 add_filter(session, "stanzas/in", count_incoming_stanzas, 999); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
233 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
234 return session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
235 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
236 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
237 local function wrap_session(session, resume) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
238 wrap_session_out(session, resume); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
239 wrap_session_in(session, resume); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
240 return session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
241 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
242 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
243 function handle_enable(session, stanza, xmlns_sm) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
244 local ok, err, err_text = can_do_smacks(session); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
245 if not ok then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
246 session.log("warn", "Failed to enable smacks: %s", err_text); -- TODO: XEP doesn't say we can send error text, should it? |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
247 (session.sends2s or session.send)(st.stanza("failed", { xmlns = xmlns_sm }):tag(err, { xmlns = xmlns_errors})); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
248 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
249 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
250 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
251 module:log("debug", "Enabling stream management"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
252 session.smacks = xmlns_sm; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
253 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
254 wrap_session(session, false); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
255 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
256 local resume_token; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
257 local resume = stanza.attr.resume; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
258 if resume == "true" or resume == "1" then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
259 resume_token = uuid_generate(); |
12053
03e9587fbfd2
mod_smacks: Switch storage for tracking resumption tokens
Kim Alvefur <zash@zash.se>
parents:
12052
diff
changeset
|
260 session_registry[jid.join(session.username, session.host, resume_token)] = session; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
261 session.resumption_token = resume_token; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
262 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
263 (session.sends2s or session.send)(st.stanza("enabled", { xmlns = xmlns_sm, id = resume_token, resume = resume, max = tostring(resume_timeout) })); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
264 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
265 end |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
266 module:hook_tag(xmlns_sm2, "enable", function (session, stanza) return handle_enable(session, stanza, xmlns_sm2); end, 100); |
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
267 module:hook_tag(xmlns_sm3, "enable", function (session, stanza) return handle_enable(session, stanza, xmlns_sm3); end, 100); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
268 |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
269 module:hook_tag("http://etherx.jabber.org/streams", "features", |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
270 function (session, stanza) |
11936
3f49c35607ca
mod_smacks: Fix timer lifetimes to follow session instead of module
Kim Alvefur <zash@zash.se>
parents:
11935
diff
changeset
|
271 -- Needs to be done after flushing sendq since those aren't stored as |
3f49c35607ca
mod_smacks: Fix timer lifetimes to follow session instead of module
Kim Alvefur <zash@zash.se>
parents:
11935
diff
changeset
|
272 -- stanzas and counting them is weird. |
3f49c35607ca
mod_smacks: Fix timer lifetimes to follow session instead of module
Kim Alvefur <zash@zash.se>
parents:
11935
diff
changeset
|
273 timer.add_task(1e-6, function () |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
274 if can_do_smacks(session) then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
275 if stanza:get_child("sm", xmlns_sm3) then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
276 session.sends2s(st.stanza("enable", sm3_attr)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
277 session.smacks = xmlns_sm3; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
278 elseif stanza:get_child("sm", xmlns_sm2) then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
279 session.sends2s(st.stanza("enable", sm2_attr)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
280 session.smacks = xmlns_sm2; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
281 else |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
282 return; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
283 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
284 wrap_session_out(session, false); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
285 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
286 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
287 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
288 |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
289 function handle_enabled(session, stanza, xmlns_sm) -- luacheck: ignore 212/stanza |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
290 module:log("debug", "Enabling stream management"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
291 session.smacks = xmlns_sm; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
292 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
293 wrap_session_in(session, false); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
294 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
295 -- FIXME Resume? |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
296 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
297 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
298 end |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
299 module:hook_tag(xmlns_sm2, "enabled", function (session, stanza) return handle_enabled(session, stanza, xmlns_sm2); end, 100); |
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
300 module:hook_tag(xmlns_sm3, "enabled", function (session, stanza) return handle_enabled(session, stanza, xmlns_sm3); end, 100); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
301 |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
302 function handle_r(origin, stanza, xmlns_sm) -- luacheck: ignore 212/stanza |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
303 if not origin.smacks then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
304 module:log("debug", "Received ack request from non-smack-enabled session"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
305 return; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
306 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
307 module:log("debug", "Received ack request, acking for %d", origin.handled_stanza_count); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
308 -- Reply with <a> |
11977
9f7a6f7d13de
mod_smacks: Factor out formatting of 'h' value
Kim Alvefur <zash@zash.se>
parents:
11976
diff
changeset
|
309 (origin.sends2s or origin.send)(st.stanza("a", { xmlns = xmlns_sm, h = format_h(origin.handled_stanza_count) })); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
310 -- piggyback our own ack request if needed (see request_ack_if_needed() for explanation of last_requested_h) |
11937
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
311 request_ack_now_if_needed(origin, false, "piggybacked by handle_r", nil); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
312 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
313 end |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
314 module:hook_tag(xmlns_sm2, "r", function (origin, stanza) return handle_r(origin, stanza, xmlns_sm2); end); |
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
315 module:hook_tag(xmlns_sm3, "r", function (origin, stanza) return handle_r(origin, stanza, xmlns_sm3); end); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
316 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
317 function handle_a(origin, stanza) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
318 if not origin.smacks then return; end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
319 origin.awaiting_ack = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
320 if origin.awaiting_ack_timer then |
11936
3f49c35607ca
mod_smacks: Fix timer lifetimes to follow session instead of module
Kim Alvefur <zash@zash.se>
parents:
11935
diff
changeset
|
321 timer.stop(origin.awaiting_ack_timer); |
3f49c35607ca
mod_smacks: Fix timer lifetimes to follow session instead of module
Kim Alvefur <zash@zash.se>
parents:
11935
diff
changeset
|
322 origin.awaiting_ack_timer = nil; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
323 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
324 if origin.delayed_ack_timer then |
11936
3f49c35607ca
mod_smacks: Fix timer lifetimes to follow session instead of module
Kim Alvefur <zash@zash.se>
parents:
11935
diff
changeset
|
325 timer.stop(origin.delayed_ack_timer) |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
326 origin.delayed_ack_timer = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
327 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
328 -- Remove handled stanzas from outgoing_stanza_queue |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
329 local h = tonumber(stanza.attr.h); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
330 if not h then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
331 origin:close{ condition = "invalid-xml"; text = "Missing or invalid 'h' attribute"; }; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
332 return; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
333 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
334 local queue = origin.outgoing_stanza_queue; |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
335 local handled_stanza_count = h-queue:count_acked(); |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
336 local acked, err = ack_errors.coerce(queue:ack(h)); -- luacheck: ignore 211/acked |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
337 if err then |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
338 origin.log("warn", "The client says it handled %d new stanzas, but we sent %d :)", |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
339 handled_stanza_count, queue:count_unacked()); |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
340 origin.log("debug", "Client h: %d, our h: %d", tonumber(stanza.attr.h), queue:count_acked()); |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
341 for i, item in queue._queue:items() do |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
342 origin.log("debug", "Q item %d: %s", i, item); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
343 end |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
344 origin:close(err); |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
345 return; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
346 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
347 |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
348 origin.log("debug", "#queue = %d", queue:count_unacked()); |
11937
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
349 request_ack_now_if_needed(origin, false, "handle_a", nil) |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
350 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
351 end |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
352 module:hook_tag(xmlns_sm2, "a", handle_a); |
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
353 module:hook_tag(xmlns_sm3, "a", handle_a); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
354 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
355 local function handle_unacked_stanzas(session) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
356 local queue = session.outgoing_stanza_queue; |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
357 if queue:count_unacked() > 0 then |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
358 session.smacks = false; -- Disable queueing |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
359 session.outgoing_stanza_queue = nil; |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
360 for stanza in queue._queue:consume() do |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
361 if not module:fire_event("delivery/failure", { session = session, stanza = stanza }) then |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
362 if stanza.attr.type ~= "error" and stanza.attr.to ~= session.full_jid then |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
363 local reply = st.error_reply(stanza, "cancel", "recipient-unavailable"); |
11997
1ea01660c79a
mod_smacks: Refactor, simplify, optimize
Kim Alvefur <zash@zash.se>
parents:
11996
diff
changeset
|
364 core_process_stanza(session, reply); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
365 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
366 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
367 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
368 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
369 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
370 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
371 -- don't send delivery errors for messages which will be delivered by mam later on |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
372 -- check if stanza was archived --> this will allow us to send back errors for stanzas not archived |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
373 -- because the user configured the server to do so ("no-archive"-setting for one special contact for example) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
374 local function get_stanza_id(stanza, by_jid) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
375 for tag in stanza:childtags("stanza-id", "urn:xmpp:sid:0") do |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
376 if tag.attr.by == by_jid then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
377 return tag.attr.id; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
378 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
379 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
380 return nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
381 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
382 module:hook("delivery/failure", function(event) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
383 local session, stanza = event.session, event.stanza; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
384 -- Only deal with authenticated (c2s) sessions |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
385 if session.username then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
386 if stanza.name == "message" and stanza.attr.xmlns == nil and |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
387 ( stanza.attr.type == "chat" or ( stanza.attr.type or "normal" ) == "normal" ) then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
388 -- don't store messages in offline store if they are mam results |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
389 local mam_result = stanza:get_child("result", xmlns_mam2); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
390 if mam_result ~= nil then |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
391 return true; -- stanza already "handled", don't send an error and don't add it to offline storage |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
392 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
393 -- do nothing here for normal messages and don't send out "message delivery errors", |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
394 -- because messages are already in MAM at this point (no need to frighten users) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
395 local stanza_id = get_stanza_id(stanza, jid.bare(session.full_jid)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
396 if session.mam_requested and stanza_id ~= nil then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
397 session.log("debug", "mod_smacks delivery/failure returning true for mam-handled stanza: mam-archive-id=%s", tostring(stanza_id)); |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
398 return true; -- stanza handled, don't send an error |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
399 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
400 -- store message in offline store, if this client does not use mam *and* was the last client online |
12063
d308f6901397
mod_smacks: Simplify access to local user sessions
Kim Alvefur <zash@zash.se>
parents:
12062
diff
changeset
|
401 local sessions = local_sessions[session.username] and local_sessions[session.username].sessions or nil; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
402 if sessions and next(sessions) == session.resource and next(sessions, session.resource) == nil then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
403 local ok = module:fire_event("message/offline/handle", { origin = session, username = session.username, stanza = stanza }); |
12048 | 404 session.log("debug", "mod_smacks delivery/failure returning %s for offline-handled stanza", tostring(ok)); |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
405 return ok; -- if stanza was handled, don't send an error |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
406 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
407 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
408 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
409 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
410 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
411 module:hook("pre-resource-unbind", function (event) |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
412 local session = event.session; |
12054
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
413 if not session.smacks then return end |
12060 | 414 if not session.resumption_token then |
415 local queue = session.outgoing_stanza_queue; | |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
416 if queue:count_unacked() > 0 then |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
417 session.log("debug", "Destroying session with %d unacked stanzas", queue:count_unacked()); |
12060 | 418 handle_unacked_stanzas(session); |
419 end | |
12054
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
420 return |
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
421 end |
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
422 |
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
423 session.hibernating = os_time(); |
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
424 session.hibernating_watchdog = watchdog.new(resume_timeout, function() |
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
425 session.log("debug", "mod_smacks hibernation timeout reached..."); |
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
426 if session.destroyed then |
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
427 session.log("debug", "The session has already been destroyed"); |
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
428 return |
12061
31a7e0ac6928
mod_smacks: Skip hibernation logic if session was closed or replaced
Kim Alvefur <zash@zash.se>
parents:
12060
diff
changeset
|
429 elseif not session.resumption_token then |
31a7e0ac6928
mod_smacks: Skip hibernation logic if session was closed or replaced
Kim Alvefur <zash@zash.se>
parents:
12060
diff
changeset
|
430 -- This should normally not happen, the watchdog should be canceled from session:close() |
31a7e0ac6928
mod_smacks: Skip hibernation logic if session was closed or replaced
Kim Alvefur <zash@zash.se>
parents:
12060
diff
changeset
|
431 session.log("debug", "The session has already been resumed or replaced"); |
31a7e0ac6928
mod_smacks: Skip hibernation logic if session was closed or replaced
Kim Alvefur <zash@zash.se>
parents:
12060
diff
changeset
|
432 return |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
433 end |
12054
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
434 |
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
435 session.log("debug", "Destroying session for hibernating too long"); |
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
436 session_registry[jid.join(session.username, session.host, session.resumption_token)] = nil; |
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
437 old_session_registry:set(session.username, session.resumption_token, { h = session.handled_stanza_count }); |
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
438 session.resumption_token = nil; |
12059
70a55fbe447c
mod_smacks: Stop stanzas from being queued on hibernation timeout
Kim Alvefur <zash@zash.se>
parents:
12056
diff
changeset
|
439 session.resending_unacked = true; -- stop outgoing_stanza_filter from re-queueing anything anymore |
12054
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
440 sessionmanager.destroy_session(session, "Hibernating too long"); |
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
441 end); |
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
442 if session.conn then |
12070
e662c172ea0f
mod_smacks: Remove references to connection on hibernation
Kim Alvefur <zash@zash.se>
parents:
12069
diff
changeset
|
443 local conn = session.conn; |
e662c172ea0f
mod_smacks: Remove references to connection on hibernation
Kim Alvefur <zash@zash.se>
parents:
12069
diff
changeset
|
444 c2s_sessions[conn] = nil; |
e662c172ea0f
mod_smacks: Remove references to connection on hibernation
Kim Alvefur <zash@zash.se>
parents:
12069
diff
changeset
|
445 session.conn = nil; |
e662c172ea0f
mod_smacks: Remove references to connection on hibernation
Kim Alvefur <zash@zash.se>
parents:
12069
diff
changeset
|
446 conn:close(); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
447 end |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
448 module:fire_event("smacks-hibernation-start", { origin = session; queue = session.outgoing_stanza_queue:table() }); |
12054
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
449 return true; -- Postpone destruction for now |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
450 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
451 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
452 local function handle_s2s_destroyed(event) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
453 local session = event.session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
454 local queue = session.outgoing_stanza_queue; |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
455 if queue and queue:count_unacked() > 0 then |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
456 session.log("warn", "Destroying session with %d unacked stanzas", queue:count_unacked()); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
457 if s2s_resend then |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
458 for stanza in queue:consume() do |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
459 module:send(stanza); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
460 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
461 session.outgoing_stanza_queue = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
462 else |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
463 handle_unacked_stanzas(session); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
464 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
465 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
466 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
467 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
468 module:hook("s2sout-destroyed", handle_s2s_destroyed); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
469 module:hook("s2sin-destroyed", handle_s2s_destroyed); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
470 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
471 local function get_session_id(session) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
472 return session.id or (tostring(session):match("[a-f0-9]+$")); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
473 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
474 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
475 function handle_resume(session, stanza, xmlns_sm) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
476 if session.full_jid then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
477 session.log("warn", "Tried to resume after resource binding"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
478 session.send(st.stanza("failed", { xmlns = xmlns_sm }) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
479 :tag("unexpected-request", { xmlns = xmlns_errors }) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
480 ); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
481 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
482 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
483 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
484 local id = stanza.attr.previd; |
12053
03e9587fbfd2
mod_smacks: Switch storage for tracking resumption tokens
Kim Alvefur <zash@zash.se>
parents:
12052
diff
changeset
|
485 local original_session = session_registry[jid.join(session.username, session.host, id)]; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
486 if not original_session then |
12051
c32ef09ab452
mod_smacks: Persist old counter values to storage
Kim Alvefur <zash@zash.se>
parents:
12050
diff
changeset
|
487 local old_session = old_session_registry:get(session.username, id); |
11978
628374809421
mod_smacks: Remove redundant fields
Kim Alvefur <zash@zash.se>
parents:
11977
diff
changeset
|
488 if old_session then |
12066
f07c8240a71d
mod_smacks: Split log messages for when an old session exists in storage
Kim Alvefur <zash@zash.se>
parents:
12065
diff
changeset
|
489 session.log("debug", "Tried to resume old expired session with id %s", id); |
11977
9f7a6f7d13de
mod_smacks: Factor out formatting of 'h' value
Kim Alvefur <zash@zash.se>
parents:
11976
diff
changeset
|
490 session.send(st.stanza("failed", { xmlns = xmlns_sm, h = format_h(old_session.h) }) |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
491 :tag("item-not-found", { xmlns = xmlns_errors }) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
492 ); |
12052
d17b8fcf11c7
mod_smacks: Clear resumption token from persistent storage on resumption
Kim Alvefur <zash@zash.se>
parents:
12051
diff
changeset
|
493 old_session_registry:set(session.username, id, nil); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
494 else |
12066
f07c8240a71d
mod_smacks: Split log messages for when an old session exists in storage
Kim Alvefur <zash@zash.se>
parents:
12065
diff
changeset
|
495 session.log("debug", "Tried to resume non-existent session with id %s", id); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
496 session.send(st.stanza("failed", { xmlns = xmlns_sm }) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
497 :tag("item-not-found", { xmlns = xmlns_errors }) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
498 ); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
499 end; |
12054
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
500 else |
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
501 if original_session.hibernating_watchdog then |
12069 | 502 original_session.log("debug", "Letting the watchdog go"); |
12054
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
503 original_session.hibernating_watchdog:cancel(); |
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
504 original_session.hibernating_watchdog = nil; |
12069 | 505 else |
506 original_session.log("error", "Hibernating session has no watchdog!") | |
12054
0116fa57f05c
mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents:
12053
diff
changeset
|
507 end |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
508 session.log("debug", "mod_smacks resuming existing session %s...", get_session_id(original_session)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
509 original_session.log("debug", "mod_smacks session resumed from %s...", get_session_id(session)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
510 -- TODO: All this should move to sessionmanager (e.g. session:replace(new_session)) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
511 if original_session.conn then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
512 original_session.log("debug", "mod_smacks closing an old connection for this session"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
513 local conn = original_session.conn; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
514 c2s_sessions[conn] = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
515 conn:close(); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
516 end |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
517 |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
518 local migrated_session_log = session.log; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
519 original_session.ip = session.ip; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
520 original_session.conn = session.conn; |
12044
9eb1a178293e
mod_smacks: Patch various self-references on the resumed session
Kim Alvefur <zash@zash.se>
parents:
12043
diff
changeset
|
521 original_session.rawsend = session.rawsend; |
9eb1a178293e
mod_smacks: Patch various self-references on the resumed session
Kim Alvefur <zash@zash.se>
parents:
12043
diff
changeset
|
522 original_session.rawsend.session = original_session; |
9eb1a178293e
mod_smacks: Patch various self-references on the resumed session
Kim Alvefur <zash@zash.se>
parents:
12043
diff
changeset
|
523 original_session.rawsend.conn = original_session.conn; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
524 original_session.send = session.send; |
12044
9eb1a178293e
mod_smacks: Patch various self-references on the resumed session
Kim Alvefur <zash@zash.se>
parents:
12043
diff
changeset
|
525 original_session.send.session = original_session; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
526 original_session.close = session.close; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
527 original_session.filter = session.filter; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
528 original_session.filter.session = original_session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
529 original_session.filters = session.filters; |
12044
9eb1a178293e
mod_smacks: Patch various self-references on the resumed session
Kim Alvefur <zash@zash.se>
parents:
12043
diff
changeset
|
530 original_session.send.filter = original_session.filter; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
531 original_session.stream = session.stream; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
532 original_session.secure = session.secure; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
533 original_session.hibernating = nil; |
12046
372ec5cd0f51
mod_smacks: Increment a counter for each resumption
Kim Alvefur <zash@zash.se>
parents:
12045
diff
changeset
|
534 original_session.resumption_counter = (original_session.resumption_counter or 0) + 1; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
535 session.log = original_session.log; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
536 session.type = original_session.type; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
537 wrap_session(original_session, true); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
538 -- Inform xmppstream of the new session (passed to its callbacks) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
539 original_session.stream:set_session(original_session); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
540 -- Similar for connlisteners |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
541 c2s_sessions[session.conn] = original_session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
542 |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
543 local queue = original_session.outgoing_stanza_queue; |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
544 local h = tonumber(stanza.attr.h); |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
545 |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
546 original_session.log("debug", "Pre-resumption #queue = %d", queue:count_unacked()) |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
547 local acked, err = ack_errors.coerce(queue:ack(h)); -- luacheck: ignore 211/acked |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
548 |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
549 if not err and not queue:resumable() then |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
550 err = ack_errors.new("overflow"); |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
551 end |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
552 |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
553 if err or not queue:resumable() then |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
554 original_session.send(st.stanza("failed", |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
555 { xmlns = xmlns_sm; h = format_h(original_session.handled_stanza_count); previd = id })); |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
556 original_session:close(err); |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
557 return false; |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
558 end |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
559 |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
560 original_session.send(st.stanza("resumed", { xmlns = xmlns_sm, |
11977
9f7a6f7d13de
mod_smacks: Factor out formatting of 'h' value
Kim Alvefur <zash@zash.se>
parents:
11976
diff
changeset
|
561 h = format_h(original_session.handled_stanza_count), previd = id })); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
562 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
563 -- Ok, we need to re-send any stanzas that the client didn't see |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
564 -- ...they are what is now left in the outgoing stanza queue |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
565 -- We have to use the send of "session" because we don't want to add our resent stanzas |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
566 -- to the outgoing queue again |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
567 |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
568 session.log("debug", "resending all unacked stanzas that are still queued after resume, #queue = %d", queue:count_unacked()); |
12047
81858fd6f198
mod_smacks: Flag both sessions to prevent duplicating queue on resumption
Kim Alvefur <zash@zash.se>
parents:
12046
diff
changeset
|
569 -- FIXME Which session is it that the queue filter sees? |
12045
a6c821720cb6
mod_smacks: Back out 48f8fa6cea7b - back in a86ae74da96c again
Kim Alvefur <zash@zash.se>
parents:
12044
diff
changeset
|
570 session.resending_unacked = true; |
12047
81858fd6f198
mod_smacks: Flag both sessions to prevent duplicating queue on resumption
Kim Alvefur <zash@zash.se>
parents:
12046
diff
changeset
|
571 original_session.resending_unacked = true; |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
572 for _, queued_stanza in queue:resume() do |
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
573 session.send(queued_stanza); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
574 end |
12045
a6c821720cb6
mod_smacks: Back out 48f8fa6cea7b - back in a86ae74da96c again
Kim Alvefur <zash@zash.se>
parents:
12044
diff
changeset
|
575 session.resending_unacked = nil; |
12047
81858fd6f198
mod_smacks: Flag both sessions to prevent duplicating queue on resumption
Kim Alvefur <zash@zash.se>
parents:
12046
diff
changeset
|
576 original_session.resending_unacked = nil; |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
577 session.log("debug", "all stanzas resent, now disabling send() in this migrated session, #queue = %d", queue:count_unacked()); |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
578 function session.send(stanza) -- luacheck: ignore 432 |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
579 migrated_session_log("error", "Tried to send stanza on old session migrated by smacks resume (maybe there is a bug?): %s", tostring(stanza)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
580 return false; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
581 end |
12056
e62025f949f9
mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents:
12054
diff
changeset
|
582 module:fire_event("smacks-hibernation-end", {origin = session, resumed = original_session, queue = queue:table()}); |
12067
97c377de8083
mod_smacks: Fix to not wait for acks from before a resumption
Kim Alvefur <zash@zash.se>
parents:
12066
diff
changeset
|
583 original_session.awaiting_ack = nil; -- Don't wait for acks from before the resumption |
12068
c3790ffdf467
mod_smacks: Remove useless delay in requesting ack on resumption
Kim Alvefur <zash@zash.se>
parents:
12067
diff
changeset
|
584 request_ack_now_if_needed(original_session, true, "handle_resume", nil); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
585 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
586 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
587 end |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
588 module:hook_tag(xmlns_sm2, "resume", function (session, stanza) return handle_resume(session, stanza, xmlns_sm2); end); |
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
589 module:hook_tag(xmlns_sm3, "resume", function (session, stanza) return handle_resume(session, stanza, xmlns_sm3); end); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
590 |
11937
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
591 -- Events when it's sensible to request an ack |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
592 -- Could experiment with forcing (ignoring max_unacked) <r>, but when and why? |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
593 local request_ack_events = { |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
594 ["csi-client-active"] = true; |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
595 ["csi-flushing"] = false; |
11938
6da703cb4c04
mod_smacks: Optimize scheduling of ack requests
Kim Alvefur <zash@zash.se>
parents:
11937
diff
changeset
|
596 ["c2s-pre-ondrain"] = false; |
6da703cb4c04
mod_smacks: Optimize scheduling of ack requests
Kim Alvefur <zash@zash.se>
parents:
11937
diff
changeset
|
597 ["s2s-pre-ondrain"] = false; |
11937
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
598 }; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
599 |
11937
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
600 for event_name, force in pairs(request_ack_events) do |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
601 module:hook(event_name, function(event) |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
602 local session = event.session or event.origin; |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
603 request_ack_now_if_needed(session, force, event_name); |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
604 end); |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
605 end |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
606 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
607 local function handle_read_timeout(event) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
608 local session = event.session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
609 if session.smacks then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
610 if session.awaiting_ack then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
611 if session.awaiting_ack_timer then |
11936
3f49c35607ca
mod_smacks: Fix timer lifetimes to follow session instead of module
Kim Alvefur <zash@zash.se>
parents:
11935
diff
changeset
|
612 timer.stop(session.awaiting_ack_timer); |
3f49c35607ca
mod_smacks: Fix timer lifetimes to follow session instead of module
Kim Alvefur <zash@zash.se>
parents:
11935
diff
changeset
|
613 session.awaiting_ack_timer = nil; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
614 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
615 if session.delayed_ack_timer then |
11936
3f49c35607ca
mod_smacks: Fix timer lifetimes to follow session instead of module
Kim Alvefur <zash@zash.se>
parents:
11935
diff
changeset
|
616 timer.stop(session.delayed_ack_timer); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
617 session.delayed_ack_timer = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
618 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
619 return false; -- Kick the session |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
620 end |
11937
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
621 request_ack_now_if_needed(session, true, "read timeout"); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
622 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
623 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
624 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
625 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
626 module:hook("s2s-read-timeout", handle_read_timeout); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
627 module:hook("c2s-read-timeout", handle_read_timeout); |
12064
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
628 |
12065
9102cbd2aec4
mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents:
12064
diff
changeset
|
629 module:hook_global("server-stopping", function(event) |
9102cbd2aec4
mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents:
12064
diff
changeset
|
630 local reason = event.reason; |
12064
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
631 -- Close smacks-enaled sessions ourselves instead of letting mod_c2s close |
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
632 -- it, which invalidates the smacks session. This allows preserving the |
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
633 -- counter value, so it can be communicated to the client when it tries to |
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
634 -- resume the lost session after a restart. |
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
635 for _, user in pairs(local_sessions) do |
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
636 for _, session in pairs(user.sessions) do |
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
637 if session.resumption_token then |
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
638 if old_session_registry:set(session.username, session.resumption_token, { h = session.handled_stanza_count }) then |
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
639 session.resumption_token = nil; |
12065
9102cbd2aec4
mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents:
12064
diff
changeset
|
640 |
9102cbd2aec4
mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents:
12064
diff
changeset
|
641 -- Deal with unacked stanzas |
9102cbd2aec4
mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents:
12064
diff
changeset
|
642 if session.outgoing_stanza_queue then |
9102cbd2aec4
mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents:
12064
diff
changeset
|
643 handle_unacked_stanzas(session); |
9102cbd2aec4
mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents:
12064
diff
changeset
|
644 end |
9102cbd2aec4
mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents:
12064
diff
changeset
|
645 |
12064
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
646 if session.conn then |
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
647 session.conn:close() |
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
648 session.conn = nil; |
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
649 -- Now when mod_c2s gets here, it will immediately destroy the |
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
650 -- session since it is unconnected. |
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
651 end |
12065
9102cbd2aec4
mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents:
12064
diff
changeset
|
652 |
9102cbd2aec4
mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents:
12064
diff
changeset
|
653 -- And make sure nobody tries to send anything |
9102cbd2aec4
mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents:
12064
diff
changeset
|
654 session:close{ condition = "system-shutdown", text = reason }; |
12064
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
655 end |
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
656 end |
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
657 end |
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
658 end |
d2380fd5e421
mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents:
12063
diff
changeset
|
659 end, -90); |