Software /
code /
prosody
Annotate
plugins/mod_smacks.lua @ 12053:03e9587fbfd2
mod_smacks: Switch storage for tracking resumption tokens
All that was a complicated way to limit the number of resumable
sessions. Let's control resource usage some other way. This leaves the
essence of mapping resumption tokens to live sessions.
This keeps resumption state across reloads.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 02 Dec 2021 14:41:19 +0100 |
parent | 12052:d17b8fcf11c7 |
child | 12054:0116fa57f05c |
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; |
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
16 local math_max = math.max; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 local math_min = math.min; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 local os_time = os.time; |
11981
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
19 local t_remove = table.remove; |
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
20 |
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
21 local datetime = require "util.datetime"; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 local add_filter = require "util.filters".add_filter; |
11981
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
23 local jid = require "util.jid"; |
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
24 local st = require "util.stanza"; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 local timer = require "util.timer"; |
11981
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
26 local uuid_generate = require "util.uuid".generate; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 |
11981
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
28 local sessionmanager = require "core.sessionmanager"; |
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
29 local core_process_stanza = prosody.core_process_stanza; |
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
30 |
5d8264f464a2
mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents:
11980
diff
changeset
|
31 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
|
32 local xmlns_delay = "urn:xmpp:delay"; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 local xmlns_mam2 = "urn:xmpp:mam:2"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 local xmlns_sm2 = "urn:xmpp:sm:2"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 local xmlns_sm3 = "urn:xmpp:sm:3"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 local sm2_attr = { xmlns = xmlns_sm2 }; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 local sm3_attr = { xmlns = xmlns_sm3 }; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 |
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"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 |
11977
9f7a6f7d13de
mod_smacks: Factor out formatting of 'h' value
Kim Alvefur <zash@zash.se>
parents:
11976
diff
changeset
|
49 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
|
50 |
12051
c32ef09ab452
mod_smacks: Persist old counter values to storage
Kim Alvefur <zash@zash.se>
parents:
12050
diff
changeset
|
51 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
|
52 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
|
53 |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
54 local function ack_delayed(session, stanza) |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 -- 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
|
56 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
|
57 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
|
58 session.log("debug", "Firing event 'smacks-ack-delayed', queue = %d", |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 session.outgoing_stanza_queue and #session.outgoing_stanza_queue or 0); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 module:fire_event("smacks-ack-delayed", {origin = session, queue = session.outgoing_stanza_queue, stanza = stanza}); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 session.delayed_ack_timer = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 local function can_do_smacks(session, advertise_only) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 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
|
67 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 local session_type = session.type; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 if session.username then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 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
|
71 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
|
72 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 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
|
75 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 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
|
78 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 module:hook("stream-features", |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 function (event) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 if can_do_smacks(event.origin, true) then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 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
|
84 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
|
85 end |
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 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 module:hook("s2s-stream-features", |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 function (event) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 if can_do_smacks(event.origin, true) then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 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
|
92 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
|
93 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 |
11937
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
96 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
|
97 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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 if force then return force end |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 local queue = session.outgoing_stanza_queue; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 local expected_h = session.last_acknowledged_stanza + #queue; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 local max_unacked = max_unacked_stanzas; |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
106 if session.state == "inactive" then |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 max_unacked = max_inactive_unacked_stanzas; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 end |
11937
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
109 -- 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
|
110 -- 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
|
111 -- further requests until a higher h-value would be expected. |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
112 return #queue > max_unacked and expected_h ~= session.last_requested_h; |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
113 end |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
114 |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
115 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
|
116 local queue = session.outgoing_stanza_queue; |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
117 session.log("debug", "Sending <r> (inside timer, before send) from %s - #queue=%d", reason, #queue); |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
118 (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
|
119 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
|
120 session.awaiting_ack = true; |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
121 -- expected_h could be lower than this expression e.g. more stanzas added to the queue meanwhile) |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
122 session.last_requested_h = session.last_acknowledged_stanza + #queue; |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
123 session.log("debug", "Sending <r> (inside timer, after send) from %s - #queue=%d", reason, #queue); |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
124 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
|
125 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
|
126 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
|
127 end); |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
128 end |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
129 end |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
130 |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
131 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
|
132 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
|
133 request_ack(session, reason); |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
134 end |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
135 end |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
136 |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
137 local function request_ack_if_needed(session, force, reason, stanza) |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
138 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
|
139 timer.add_task(0, function () |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
140 request_ack_now_if_needed(session, force, reason, stanza); |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
141 end); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 -- Trigger "smacks-ack-delayed"-event if we added new (ackable) stanzas to the outgoing queue |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 -- and there isn't already a timer for this event running. |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 -- If we wouldn't do this, stanzas added to the queue after the first "smacks-ack-delayed"-event |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 -- would not trigger this event (again). |
11937
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
148 local queue = session.outgoing_stanza_queue; |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
149 local max_unacked = max_unacked_stanzas; |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
150 if session.state == "inactive" then |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
151 max_unacked = max_inactive_unacked_stanzas; |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
152 end |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
153 if #queue > max_unacked and session.awaiting_ack and session.delayed_ack_timer == nil then |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
154 session.log("debug", "Calling ack_delayed directly (still waiting for ack)"); |
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
155 ack_delayed(session, stanza); -- this is the only new stanza in the queue --> provide it to other modules |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
156 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 local function outgoing_stanza_filter(stanza, session) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 -- 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
|
161 -- supposed to be nil. |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
162 -- 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
|
163 -- 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
|
164 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
|
165 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
|
166 (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
|
167 and not stanza.name:find":"; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 |
12045
a6c821720cb6
mod_smacks: Back out 48f8fa6cea7b - back in a86ae74da96c again
Kim Alvefur <zash@zash.se>
parents:
12044
diff
changeset
|
169 if is_stanza then |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 local queue = session.outgoing_stanza_queue; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 local cached_stanza = st.clone(stanza); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 |
11983
27f2539b4f87
mod_smacks: Remove dead conditional
Kim Alvefur <zash@zash.se>
parents:
11982
diff
changeset
|
173 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
|
174 cached_stanza = cached_stanza:tag("delay", { |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 xmlns = xmlns_delay, |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 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
|
177 stamp = datetime.datetime() |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 }); |
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 queue[#queue+1] = cached_stanza; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 if session.hibernating then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
183 session.log("debug", "hibernating since %s, stanza queued", datetime.datetime(session.hibernating)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
184 module:fire_event("smacks-hibernation-stanza-queued", {origin = session, queue = queue, stanza = cached_stanza}); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
185 return nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 end |
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 return stanza; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
190 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
191 local function count_incoming_stanzas(stanza, session) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 if not stanza.attr.xmlns then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
193 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
|
194 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
|
195 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
196 return stanza; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
197 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 local function wrap_session_out(session, resume) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
200 if not resume then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
201 session.outgoing_stanza_queue = {}; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
202 session.last_acknowledged_stanza = 0; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
203 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
204 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
205 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
|
206 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
207 return session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
208 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
209 |
11975
fcea4d9e7502
mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents:
11952
diff
changeset
|
210 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
|
211 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
|
212 if session.resumption_token then |
12053
03e9587fbfd2
mod_smacks: Switch storage for tracking resumption tokens
Kim Alvefur <zash@zash.se>
parents:
12052
diff
changeset
|
213 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
|
214 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
|
215 session.resumption_token = nil; |
fcea4d9e7502
mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents:
11952
diff
changeset
|
216 end |
fcea4d9e7502
mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents:
11952
diff
changeset
|
217 -- 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
|
218 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
|
219 (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
|
220 xmlns = session.smacks; |
11977
9f7a6f7d13de
mod_smacks: Factor out formatting of 'h' value
Kim Alvefur <zash@zash.se>
parents:
11976
diff
changeset
|
221 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
|
222 })); |
fcea4d9e7502
mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents:
11952
diff
changeset
|
223 end |
fcea4d9e7502
mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents:
11952
diff
changeset
|
224 end); |
fcea4d9e7502
mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents:
11952
diff
changeset
|
225 |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
226 local function wrap_session_in(session, resume) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
227 if not resume then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
228 session.handled_stanza_count = 0; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
229 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
230 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
|
231 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
232 return session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
233 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
234 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
235 local function wrap_session(session, resume) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
236 wrap_session_out(session, resume); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
237 wrap_session_in(session, resume); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
238 return session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
239 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
240 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
241 function handle_enable(session, stanza, xmlns_sm) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
242 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
|
243 if not ok then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
244 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
|
245 (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
|
246 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
247 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
248 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
249 module:log("debug", "Enabling stream management"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
250 session.smacks = xmlns_sm; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
251 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
252 wrap_session(session, false); |
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 local resume_token; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
255 local resume = stanza.attr.resume; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
256 if resume == "true" or resume == "1" then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
257 resume_token = uuid_generate(); |
12053
03e9587fbfd2
mod_smacks: Switch storage for tracking resumption tokens
Kim Alvefur <zash@zash.se>
parents:
12052
diff
changeset
|
258 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
|
259 session.resumption_token = resume_token; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
260 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
261 (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
|
262 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
263 end |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
264 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
|
265 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
|
266 |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
267 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
|
268 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
|
269 -- 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
|
270 -- 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
|
271 timer.add_task(1e-6, function () |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
272 if can_do_smacks(session) then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
273 if stanza:get_child("sm", xmlns_sm3) then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
274 session.sends2s(st.stanza("enable", sm3_attr)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
275 session.smacks = xmlns_sm3; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
276 elseif stanza:get_child("sm", xmlns_sm2) then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
277 session.sends2s(st.stanza("enable", sm2_attr)); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
278 session.smacks = xmlns_sm2; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
279 else |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
280 return; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
281 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
282 wrap_session_out(session, false); |
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 end); |
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 |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
287 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
|
288 module:log("debug", "Enabling stream management"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
289 session.smacks = xmlns_sm; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
290 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
291 wrap_session_in(session, false); |
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 -- FIXME Resume? |
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 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
296 end |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
297 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
|
298 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
|
299 |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
300 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
|
301 if not origin.smacks then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
302 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
|
303 return; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
304 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
305 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
|
306 -- Reply with <a> |
11977
9f7a6f7d13de
mod_smacks: Factor out formatting of 'h' value
Kim Alvefur <zash@zash.se>
parents:
11976
diff
changeset
|
307 (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
|
308 -- 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
|
309 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
|
310 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
311 end |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
312 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
|
313 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
|
314 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
315 function handle_a(origin, stanza) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
316 if not origin.smacks then return; end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
317 origin.awaiting_ack = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
318 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
|
319 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
|
320 origin.awaiting_ack_timer = nil; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
321 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
322 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
|
323 timer.stop(origin.delayed_ack_timer) |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
324 origin.delayed_ack_timer = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
325 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
326 -- Remove handled stanzas from outgoing_stanza_queue |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
327 -- origin.log("debug", "ACK: h=%s, last=%s", stanza.attr.h or "", origin.last_acknowledged_stanza or ""); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
328 local h = tonumber(stanza.attr.h); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
329 if not h then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
330 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
|
331 return; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
332 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
333 local handled_stanza_count = h-origin.last_acknowledged_stanza; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
334 local queue = origin.outgoing_stanza_queue; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
335 if handled_stanza_count > #queue then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
336 origin.log("warn", "The client says it handled %d new stanzas, but we only sent %d :)", |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
337 handled_stanza_count, #queue); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
338 origin.log("debug", "Client h: %d, our h: %d", tonumber(stanza.attr.h), origin.last_acknowledged_stanza); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
339 for i=1,#queue do |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
340 origin.log("debug", "Q item %d: %s", i, tostring(queue[i])); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
341 end |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
342 origin:close{ condition = "undefined-condition"; text = "Client acknowledged more stanzas than sent by server"; }; |
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
343 return; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
344 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
345 |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
346 for _=1,math_min(handled_stanza_count,#queue) do |
12050
7b87a1747eb2
mod_smacks: Remove now unused variable [luacheck]
Kim Alvefur <zash@zash.se>
parents:
12049
diff
changeset
|
347 t_remove(origin.outgoing_stanza_queue, 1); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
348 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
349 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
350 origin.log("debug", "#queue = %d", #queue); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
351 origin.last_acknowledged_stanza = origin.last_acknowledged_stanza + handled_stanza_count; |
11937
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
352 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
|
353 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
354 end |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
355 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
|
356 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
|
357 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
358 local function handle_unacked_stanzas(session) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
359 local queue = session.outgoing_stanza_queue; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
360 if #queue > 0 then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
361 session.outgoing_stanza_queue = {}; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
362 for i=1,#queue do |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
363 if not module:fire_event("delivery/failure", { session = session, stanza = queue[i] }) then |
11997
1ea01660c79a
mod_smacks: Refactor, simplify, optimize
Kim Alvefur <zash@zash.se>
parents:
11996
diff
changeset
|
364 if queue[i].attr.type ~= "error" and queue[i].attr.from ~= session.full_jid then |
11996
3dd7886f9342
mod_smacks: Use function to construct error reply
Kim Alvefur <zash@zash.se>
parents:
11985
diff
changeset
|
365 local reply = st.error_reply(queue[i], "cancel", "recipient-unavailable"); |
11997
1ea01660c79a
mod_smacks: Refactor, simplify, optimize
Kim Alvefur <zash@zash.se>
parents:
11996
diff
changeset
|
366 core_process_stanza(session, reply); |
11934
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 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
371 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
372 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
373 -- 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
|
374 -- 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
|
375 -- 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
|
376 local function get_stanza_id(stanza, by_jid) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
377 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
|
378 if tag.attr.by == by_jid then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
379 return tag.attr.id; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
380 end |
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 return nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
383 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
384 module:hook("delivery/failure", function(event) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
385 local session, stanza = event.session, event.stanza; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
386 -- Only deal with authenticated (c2s) sessions |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
387 if session.username then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
388 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
|
389 ( 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
|
390 -- 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
|
391 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
|
392 if mam_result ~= nil then |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
393 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
|
394 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
395 -- 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
|
396 -- 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
|
397 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
|
398 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
|
399 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
|
400 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
|
401 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
402 -- store message in offline store, if this client does not use mam *and* was the last client online |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
403 local sessions = prosody.hosts[module.host].sessions[session.username] and |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
404 prosody.hosts[module.host].sessions[session.username].sessions or nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
405 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
|
406 local ok = module:fire_event("message/offline/handle", { origin = session, username = session.username, stanza = stanza }); |
12048 | 407 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
|
408 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
|
409 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
410 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
411 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
412 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
413 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
414 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
|
415 local session = event.session; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
416 if session.smacks then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
417 if not session.resumption_token then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
418 local queue = session.outgoing_stanza_queue; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
419 if #queue > 0 then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
420 session.log("debug", "Destroying session with %d unacked stanzas", #queue); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
421 handle_unacked_stanzas(session); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
422 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
423 else |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
424 session.log("debug", "mod_smacks hibernating session for up to %d seconds", resume_timeout); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
425 local hibernate_time = os_time(); -- Track the time we went into hibernation |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
426 session.hibernating = hibernate_time; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
427 local resumption_token = session.resumption_token; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
428 module:fire_event("smacks-hibernation-start", {origin = session, queue = session.outgoing_stanza_queue}); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
429 timer.add_task(resume_timeout, function () |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
430 session.log("debug", "mod_smacks hibernation timeout reached..."); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
431 -- We need to check the current resumption token for this resource |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
432 -- matches the smacks session this timer is for in case it changed |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
433 -- (for example, the client may have bound a new resource and |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
434 -- started a new smacks session, or not be using smacks) |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
435 local curr_session = prosody.full_sessions[session.full_jid]; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
436 if session.destroyed then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
437 session.log("debug", "The session has already been destroyed"); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
438 elseif curr_session and curr_session.resumption_token == resumption_token |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
439 -- Check the hibernate time still matches what we think it is, |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
440 -- otherwise the session resumed and re-hibernated. |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
441 and session.hibernating == hibernate_time then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
442 -- wait longer if the timeout isn't reached because push was enabled for this session |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
443 -- session.first_hibernated_push is the starting point for hibernation timeouts of those push enabled clients |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
444 -- wait for an additional resume_timeout seconds if no push occurred since hibernation at all |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
445 local current_time = os_time(); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
446 local timeout_start = math_max(session.hibernating, session.first_hibernated_push or session.hibernating); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
447 if session.push_identifier ~= nil and not session.first_hibernated_push then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
448 session.log("debug", "No push happened since hibernation started, hibernating session for up to %d extra seconds", resume_timeout); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
449 return resume_timeout; |
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 if session.push_identifier ~= nil and current_time-timeout_start < resume_timeout then |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
452 session.log("debug", "A push happened since hibernation started, hibernating session for up to %d extra seconds", |
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
453 resume_timeout - (current_time - timeout_start)); |
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
454 return resume_timeout-(current_time-timeout_start); -- time left to wait |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
455 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
456 session.log("debug", "Destroying session for hibernating too long"); |
12053
03e9587fbfd2
mod_smacks: Switch storage for tracking resumption tokens
Kim Alvefur <zash@zash.se>
parents:
12052
diff
changeset
|
457 session_registry[jid.join(session.username, session.host, session.resumption_token)] = nil; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
458 -- save only actual h value and username/host (for security) |
12051
c32ef09ab452
mod_smacks: Persist old counter values to storage
Kim Alvefur <zash@zash.se>
parents:
12050
diff
changeset
|
459 old_session_registry:set(session.username, session.resumption_token, { |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
460 h = session.handled_stanza_count, |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
461 }); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
462 session.resumption_token = nil; |
12038
4e54334febc1
mod_smacks: Include reason for destroying session on hibernation timeout
Kim Alvefur <zash@zash.se>
parents:
11997
diff
changeset
|
463 sessionmanager.destroy_session(session, "Hibernating too long"); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
464 else |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
465 session.log("debug", "Session resumed before hibernation timeout, all is well") |
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 end); |
11982
c7c0c40487e2
mod_smacks: Ensure hibernating session is not connected
Kim Alvefur <zash@zash.se>
parents:
11981
diff
changeset
|
468 if session.conn then |
c7c0c40487e2
mod_smacks: Ensure hibernating session is not connected
Kim Alvefur <zash@zash.se>
parents:
11981
diff
changeset
|
469 session.conn:close(); |
c7c0c40487e2
mod_smacks: Ensure hibernating session is not connected
Kim Alvefur <zash@zash.se>
parents:
11981
diff
changeset
|
470 end |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
471 return true; -- Postpone destruction for now |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
472 end |
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 end); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
475 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
476 local function handle_s2s_destroyed(event) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
477 local session = event.session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
478 local queue = session.outgoing_stanza_queue; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
479 if queue and #queue > 0 then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
480 session.log("warn", "Destroying session with %d unacked stanzas", #queue); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
481 if s2s_resend then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
482 for i = 1, #queue do |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
483 module:send(queue[i]); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
484 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
485 session.outgoing_stanza_queue = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
486 else |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
487 handle_unacked_stanzas(session); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
488 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
489 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
490 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
491 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
492 module:hook("s2sout-destroyed", handle_s2s_destroyed); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
493 module:hook("s2sin-destroyed", handle_s2s_destroyed); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
494 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
495 local function get_session_id(session) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
496 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
|
497 end |
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 function handle_resume(session, stanza, xmlns_sm) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
500 if session.full_jid then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
501 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
|
502 session.send(st.stanza("failed", { xmlns = xmlns_sm }) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
503 :tag("unexpected-request", { xmlns = xmlns_errors }) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
504 ); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
505 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
506 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
507 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
508 local id = stanza.attr.previd; |
12053
03e9587fbfd2
mod_smacks: Switch storage for tracking resumption tokens
Kim Alvefur <zash@zash.se>
parents:
12052
diff
changeset
|
509 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
|
510 if not original_session then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
511 session.log("debug", "Tried to resume non-existent session with id %s", id); |
12051
c32ef09ab452
mod_smacks: Persist old counter values to storage
Kim Alvefur <zash@zash.se>
parents:
12050
diff
changeset
|
512 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
|
513 if old_session then |
11977
9f7a6f7d13de
mod_smacks: Factor out formatting of 'h' value
Kim Alvefur <zash@zash.se>
parents:
11976
diff
changeset
|
514 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
|
515 :tag("item-not-found", { xmlns = xmlns_errors }) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
516 ); |
12052
d17b8fcf11c7
mod_smacks: Clear resumption token from persistent storage on resumption
Kim Alvefur <zash@zash.se>
parents:
12051
diff
changeset
|
517 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
|
518 else |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
519 session.send(st.stanza("failed", { xmlns = xmlns_sm }) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
520 :tag("item-not-found", { xmlns = xmlns_errors }) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
521 ); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
522 end; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
523 elseif session.username == original_session.username |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
524 and session.host == original_session.host then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
525 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
|
526 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
|
527 -- 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
|
528 if original_session.conn then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
529 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
|
530 local conn = original_session.conn; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
531 c2s_sessions[conn] = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
532 conn:close(); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
533 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
534 local migrated_session_log = session.log; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
535 original_session.ip = session.ip; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
536 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
|
537 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
|
538 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
|
539 original_session.rawsend.conn = original_session.conn; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
540 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
|
541 original_session.send.session = original_session; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
542 original_session.close = session.close; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
543 original_session.filter = session.filter; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
544 original_session.filter.session = original_session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
545 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
|
546 original_session.send.filter = original_session.filter; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
547 original_session.stream = session.stream; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
548 original_session.secure = session.secure; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
549 original_session.hibernating = nil; |
12046
372ec5cd0f51
mod_smacks: Increment a counter for each resumption
Kim Alvefur <zash@zash.se>
parents:
12045
diff
changeset
|
550 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
|
551 session.log = original_session.log; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
552 session.type = original_session.type; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
553 wrap_session(original_session, true); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
554 -- 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
|
555 original_session.stream:set_session(original_session); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
556 -- Similar for connlisteners |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
557 c2s_sessions[session.conn] = original_session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
558 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
559 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
|
560 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
|
561 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
562 -- Fake an <a> with the h of the <resume/> from the client |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
563 original_session:dispatch_stanza(st.stanza("a", { xmlns = xmlns_sm, |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
564 h = stanza.attr.h })); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
565 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
566 -- 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
|
567 -- ...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
|
568 -- 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
|
569 -- to the outgoing queue again |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
570 local queue = original_session.outgoing_stanza_queue; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
571 session.log("debug", "resending all unacked stanzas that are still queued after resume, #queue = %d", #queue); |
12047
81858fd6f198
mod_smacks: Flag both sessions to prevent duplicating queue on resumption
Kim Alvefur <zash@zash.se>
parents:
12046
diff
changeset
|
572 -- 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
|
573 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
|
574 original_session.resending_unacked = true; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
575 for i=1,#queue do |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
576 session.send(queue[i]); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
577 end |
12045
a6c821720cb6
mod_smacks: Back out 48f8fa6cea7b - back in a86ae74da96c again
Kim Alvefur <zash@zash.se>
parents:
12044
diff
changeset
|
578 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
|
579 original_session.resending_unacked = nil; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
580 session.log("debug", "all stanzas resent, now disabling send() in this migrated session, #queue = %d", #queue); |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
581 function session.send(stanza) -- luacheck: ignore 432 |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
582 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
|
583 return false; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
584 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
585 module:fire_event("smacks-hibernation-end", {origin = session, resumed = original_session, queue = queue}); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
586 request_ack_if_needed(original_session, true, "handle_resume", nil); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
587 else |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
588 module:log("warn", "Client %s@%s[%s] tried to resume stream for %s@%s[%s]", |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
589 session.username or "?", session.host or "?", session.type, |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
590 original_session.username or "?", original_session.host or "?", original_session.type); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
591 session.send(st.stanza("failed", { xmlns = xmlns_sm }) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
592 :tag("not-authorized", { xmlns = xmlns_errors })); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
593 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
594 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
595 end |
11935
4d0d10fabb82
mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents:
11934
diff
changeset
|
596 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
|
597 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
|
598 |
11937
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
599 -- 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
|
600 -- 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
|
601 local request_ack_events = { |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
602 ["csi-client-active"] = true; |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
603 ["csi-flushing"] = false; |
11938
6da703cb4c04
mod_smacks: Optimize scheduling of ack requests
Kim Alvefur <zash@zash.se>
parents:
11937
diff
changeset
|
604 ["c2s-pre-ondrain"] = false; |
6da703cb4c04
mod_smacks: Optimize scheduling of ack requests
Kim Alvefur <zash@zash.se>
parents:
11937
diff
changeset
|
605 ["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
|
606 }; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
607 |
11937
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
608 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
|
609 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
|
610 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
|
611 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
|
612 end); |
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
613 end |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
614 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
615 local function handle_read_timeout(event) |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
616 local session = event.session; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
617 if session.smacks then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
618 if session.awaiting_ack then |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
619 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
|
620 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
|
621 session.awaiting_ack_timer = nil; |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
622 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
623 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
|
624 timer.stop(session.delayed_ack_timer); |
11934
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
625 session.delayed_ack_timer = nil; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
626 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
627 return false; -- Kick the session |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
628 end |
11937
364c3f018e3a
mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents:
11936
diff
changeset
|
629 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
|
630 return true; |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
631 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
632 end |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
633 |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
634 module:hook("s2s-read-timeout", handle_read_timeout); |
65cdb1b21db3
mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
635 module:hook("c2s-read-timeout", handle_read_timeout); |