Annotate

plugins/mod_smacks.lua @ 12235:c98527220564

doap: Make note of mod_mam storing XEP-0184 receipts
author Kim Alvefur <zash@zash.se>
date Thu, 18 Mar 2021 00:03:16 +0100
parent 12137:4e61aaa4e9b2
child 12272:fe0f5c47fda3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- XEP-0198: Stream Management for Prosody IM
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 --
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 -- Copyright (C) 2010-2015 Matthew Wild
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 -- Copyright (C) 2010 Waqas Hussain
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 -- Copyright (C) 2012-2021 Kim Alvefur
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 -- Copyright (C) 2012 Thijs Alkemade
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 -- Copyright (C) 2014 Florian Zeitz
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 -- Copyright (C) 2016-2020 Thilo Molitor
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 --
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 -- This project is MIT/X11 licensed. Please see the
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 -- COPYING file in the source package for more information.
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 --
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13
11981
5d8264f464a2 mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents: 11980
diff changeset
14 local tonumber = tonumber;
5d8264f464a2 mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents: 11980
diff changeset
15 local tostring = tostring;
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 local os_time = os.time;
11981
5d8264f464a2 mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents: 11980
diff changeset
17
12125
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
18 -- These metrics together allow to calculate an instantaneous
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
19 -- "unacked stanzas" metric in the graphing frontend, without us having to
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
20 -- iterate over all the queues.
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
21 local tx_queued_stanzas = module:measure("tx_queued_stanzas", "counter");
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
22 local tx_dropped_stanzas = module:metric(
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
23 "histogram",
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
24 "tx_dropped_stanzas", "", "number of stanzas in a queue which got dropped",
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
25 {},
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
26 {buckets = {0, 1, 2, 4, 8, 16, 32}}
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
27 ):with_labels();
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
28 local tx_acked_stanzas = module:metric(
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
29 "histogram",
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
30 "tx_acked_stanzas", "", "number of items acked per ack received",
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
31 {},
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
32 {buckets = {0, 1, 2, 4, 8, 16, 32}}
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
33 ):with_labels();
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
34
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
35 -- number of session resumptions attempts where the session had expired
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
36 local resumption_expired = module:measure("session_resumption_expired", "counter");
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
37 local resumption_age = module:metric(
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
38 "histogram",
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
39 "resumption_age", "seconds", "time the session had been hibernating at the time of a resumption",
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
40 {},
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
41 {buckets = { 0, 1, 2, 5, 10, 20, 50, 100, 200, 500 }}
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
42 ):with_labels();
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
43 local sessions_expired = module:measure("sessions_expired", "counter");
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
44 local sessions_started = module:measure("sessions_started", "counter");
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
45
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
46
11981
5d8264f464a2 mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents: 11980
diff changeset
47 local datetime = require "util.datetime";
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 local add_filter = require "util.filters".add_filter;
11981
5d8264f464a2 mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents: 11980
diff changeset
49 local jid = require "util.jid";
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
50 local smqueue = require "util.smqueue";
11981
5d8264f464a2 mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents: 11980
diff changeset
51 local st = require "util.stanza";
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 local timer = require "util.timer";
12112
08a949e63541 mod_smacks: Use more compact resumption tokens
Kim Alvefur <zash@zash.se>
parents: 12081
diff changeset
53 local new_id = require "util.id".short;
12054
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
54 local watchdog = require "util.watchdog";
12136
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
55 local it = require"util.iterators";
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56
11981
5d8264f464a2 mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents: 11980
diff changeset
57 local sessionmanager = require "core.sessionmanager";
5d8264f464a2 mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents: 11980
diff changeset
58 local core_process_stanza = prosody.core_process_stanza;
5d8264f464a2 mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents: 11980
diff changeset
59
5d8264f464a2 mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents: 11980
diff changeset
60 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
61 local xmlns_delay = "urn:xmpp:delay";
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 local xmlns_mam2 = "urn:xmpp:mam:2";
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 local xmlns_sm2 = "urn:xmpp:sm:2";
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 local xmlns_sm3 = "urn:xmpp:sm:3";
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 local sm2_attr = { xmlns = xmlns_sm2 };
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 local sm3_attr = { xmlns = xmlns_sm3 };
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
69 local queue_size = module:get_option_number("smacks_max_queue_size", 500);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 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
71 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
72 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
73 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
74 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
75 local delayed_ack_timeout = module:get_option_number("smacks_max_ack_delay", 30);
12136
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
76 local max_old_sessions = module:get_option_number("smacks_max_old_sessions", 10);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 local c2s_sessions = module:shared("/*/c2s/sessions");
12063
d308f6901397 mod_smacks: Simplify access to local user sessions
Kim Alvefur <zash@zash.se>
parents: 12062
diff changeset
79 local local_sessions = prosody.hosts[module.host].sessions;
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80
11977
9f7a6f7d13de mod_smacks: Factor out formatting of 'h' value
Kim Alvefur <zash@zash.se>
parents: 11976
diff changeset
81 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
82
12136
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
83 local all_old_sessions = module:open_store("smacks_h");
12051
c32ef09ab452 mod_smacks: Persist old counter values to storage
Kim Alvefur <zash@zash.se>
parents: 12050
diff changeset
84 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
85 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
86
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
87 local ack_errors = require"util.error".init("mod_smacks", xmlns_sm3, {
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
88 head = { condition = "undefined-condition"; text = "Client acknowledged more stanzas than sent by server" };
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
89 tail = { condition = "undefined-condition"; text = "Client acknowledged less stanzas than already acknowledged" };
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
90 pop = { condition = "internal-server-error"; text = "Something went wrong with Stream Management" };
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
91 overflow = { condition = "resource-constraint", text = "Too many unacked stanzas remaining, session can't be resumed" }
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
92 });
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
93
12076
ad1fe36eaae7 mod_smacks: Fix typo (thanks codespell)
Kim Alvefur <zash@zash.se>
parents: 12075
diff changeset
94 -- COMPAT note the use of compatibility wrapper in events (queue:table())
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
95
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
96 local function ack_delayed(session, stanza)
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 -- 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
98 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
99 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
100 session.log("debug", "Firing event 'smacks-ack-delayed', queue = %d",
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
101 session.outgoing_stanza_queue and session.outgoing_stanza_queue:count_unacked() or 0);
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
102 module:fire_event("smacks-ack-delayed", {origin = session, queue = session.outgoing_stanza_queue:table(), stanza = stanza});
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 session.delayed_ack_timer = nil;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 local function can_do_smacks(session, advertise_only)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 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
109
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 local session_type = session.type;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 if session.username then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 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
113 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
114 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 return true;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 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
117 return true;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 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
120 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 module:hook("stream-features",
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 function (event)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 if can_do_smacks(event.origin, true) then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 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
126 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
127 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 end);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 module:hook("s2s-stream-features",
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 function (event)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 if can_do_smacks(event.origin, true) then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 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
134 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
135 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 end);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137
11937
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
138 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
139 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
140 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
141 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
142 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
143 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
144 if force then return force end
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 local queue = session.outgoing_stanza_queue;
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
146 local expected_h = session.last_acknowledged_stanza + queue:count_unacked();
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 local max_unacked = max_unacked_stanzas;
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
148 if session.state == "inactive" then
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 max_unacked = max_inactive_unacked_stanzas;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 end
12078
2c13aaae35ac mod_smacks: Fix typos
Kim Alvefur <zash@zash.se>
parents: 12077
diff changeset
151 -- this check of last_requested_h prevents ack-loops if misbehaving clients report wrong
11937
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
152 -- 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
153 -- further requests until a higher h-value would be expected.
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
154 return queue:count_unacked() > max_unacked and expected_h ~= session.last_requested_h;
11937
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
155 end
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
156
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
157 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
158 local queue = session.outgoing_stanza_queue;
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
159 session.log("debug", "Sending <r> (inside timer, before send) from %s - #queue=%d", reason, queue:count_unacked());
11937
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
160 (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
161 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
162 session.awaiting_ack = true;
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
163 -- expected_h could be lower than this expression e.g. more stanzas added to the queue meanwhile)
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
164 session.last_requested_h = session.last_acknowledged_stanza + queue:count_unacked();
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
165 session.log("debug", "Sending <r> (inside timer, after send) from %s - #queue=%d", reason, queue:count_unacked());
11937
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
166 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
167 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
168 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
169 end);
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
170 end
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
171 end
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
172
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
173 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
174 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
175 request_ack(session, reason);
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
176 end
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
177 end
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
178
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 local function outgoing_stanza_filter(stanza, session)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 -- 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
181 -- supposed to be nil.
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 -- 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
183 -- 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
184 if session.resending_unacked then return stanza end
12073
4cbe7979a92a mod_smacks: Fix traceback when bouncing unacked stanzas
Kim Alvefur <zash@zash.se>
parents: 12072
diff changeset
185 if not session.smacks then return stanza end
11979
705c4c07a860 mod_smacks: Use stanza type checking function for correctness
Kim Alvefur <zash@zash.se>
parents: 11978
diff changeset
186 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
187 (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
188 and not stanza.name:find":";
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189
12045
a6c821720cb6 mod_smacks: Back out 48f8fa6cea7b - back in a86ae74da96c again
Kim Alvefur <zash@zash.se>
parents: 12044
diff changeset
190 if is_stanza then
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 local queue = session.outgoing_stanza_queue;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 local cached_stanza = st.clone(stanza);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193
11983
27f2539b4f87 mod_smacks: Remove dead conditional
Kim Alvefur <zash@zash.se>
parents: 11982
diff changeset
194 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
195 cached_stanza = cached_stanza:tag("delay", {
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 xmlns = xmlns_delay,
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 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
198 stamp = datetime.datetime()
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 });
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
202 queue:push(cached_stanza);
12125
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
203 tx_queued_stanzas(1);
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
204
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 if session.hibernating then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 session.log("debug", "hibernating since %s, stanza queued", datetime.datetime(session.hibernating));
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
207 -- FIXME queue implementation changed, anything depending on it being an array will break
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
208 module:fire_event("smacks-hibernation-stanza-queued", {origin = session, queue = queue:table(), stanza = cached_stanza});
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 return nil;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 return stanza;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 local function count_incoming_stanzas(stanza, session)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216 if not stanza.attr.xmlns then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 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
218 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
219 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 return stanza;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
221 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
223 local function wrap_session_out(session, resume)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224 if not resume then
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
225 session.outgoing_stanza_queue = smqueue.new(queue_size);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
226 session.last_acknowledged_stanza = 0;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 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
230
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231 return session;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
232 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233
11975
fcea4d9e7502 mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents: 11952
diff changeset
234 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
235 local session = event.session;
12075
9f4d88f54a54 mod_smacks: Avoid log noise when a non-smacks session is closed
Kim Alvefur <zash@zash.se>
parents: 12074
diff changeset
236 if session.smacks == nil then return end
11975
fcea4d9e7502 mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents: 11952
diff changeset
237 if session.resumption_token then
12069
b9e08cbd032b mod_smacks: Add more logging
Kim Alvefur <zash@zash.se>
parents: 12068
diff changeset
238 session.log("debug", "Revoking resumption token");
12053
03e9587fbfd2 mod_smacks: Switch storage for tracking resumption tokens
Kim Alvefur <zash@zash.se>
parents: 12052
diff changeset
239 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
240 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
241 session.resumption_token = nil;
12069
b9e08cbd032b mod_smacks: Add more logging
Kim Alvefur <zash@zash.se>
parents: 12068
diff changeset
242 else
b9e08cbd032b mod_smacks: Add more logging
Kim Alvefur <zash@zash.se>
parents: 12068
diff changeset
243 session.log("debug", "Session not resumable");
11975
fcea4d9e7502 mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents: 11952
diff changeset
244 end
12062
4972244fe87b mod_smacks: Cancel hibernation when session is closed
Kim Alvefur <zash@zash.se>
parents: 12061
diff changeset
245 if session.hibernating_watchdog then
12069
b9e08cbd032b mod_smacks: Add more logging
Kim Alvefur <zash@zash.se>
parents: 12068
diff changeset
246 session.log("debug", "Removing sleeping watchdog");
12062
4972244fe87b mod_smacks: Cancel hibernation when session is closed
Kim Alvefur <zash@zash.se>
parents: 12061
diff changeset
247 -- If the session is being replaced instead of resume, we don't want the
4972244fe87b mod_smacks: Cancel hibernation when session is closed
Kim Alvefur <zash@zash.se>
parents: 12061
diff changeset
248 -- old session around to time out and cause trouble for the new session
4972244fe87b mod_smacks: Cancel hibernation when session is closed
Kim Alvefur <zash@zash.se>
parents: 12061
diff changeset
249 session.hibernating_watchdog:cancel();
4972244fe87b mod_smacks: Cancel hibernation when session is closed
Kim Alvefur <zash@zash.se>
parents: 12061
diff changeset
250 session.hibernating_watchdog = nil;
12069
b9e08cbd032b mod_smacks: Add more logging
Kim Alvefur <zash@zash.se>
parents: 12068
diff changeset
251 else
b9e08cbd032b mod_smacks: Add more logging
Kim Alvefur <zash@zash.se>
parents: 12068
diff changeset
252 session.log("debug", "No watchdog set");
12062
4972244fe87b mod_smacks: Cancel hibernation when session is closed
Kim Alvefur <zash@zash.se>
parents: 12061
diff changeset
253 end
11975
fcea4d9e7502 mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents: 11952
diff changeset
254 -- 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
255 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
256 (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
257 xmlns = session.smacks;
11977
9f7a6f7d13de mod_smacks: Factor out formatting of 'h' value
Kim Alvefur <zash@zash.se>
parents: 11976
diff changeset
258 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
259 }));
fcea4d9e7502 mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents: 11952
diff changeset
260 end
fcea4d9e7502 mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents: 11952
diff changeset
261 end);
fcea4d9e7502 mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents: 11952
diff changeset
262
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
263 local function wrap_session_in(session, resume)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
264 if not resume then
12125
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
265 sessions_started(1);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
266 session.handled_stanza_count = 0;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
267 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
268 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
269
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
270 return session;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
271 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
272
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
273 local function wrap_session(session, resume)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
274 wrap_session_out(session, resume);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
275 wrap_session_in(session, resume);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
276 return session;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
277 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
278
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
279 function handle_enable(session, stanza, xmlns_sm)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
280 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
281 if not ok then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
282 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
283 (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
284 return true;
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
12136
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
287 if session.username then
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
288 local old_sessions, err = all_old_sessions:get(session.username);
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
289 module:log("debug", "Old sessions: %q", old_sessions)
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
290 if old_sessions then
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
291 local keep, count = {}, 0;
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
292 for token, info in it.sorted_pairs(old_sessions, function(a, b)
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
293 return (old_sessions[a].t or 0) > (old_sessions[b].t or 0);
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
294 end) do
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
295 count = count + 1;
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
296 if count > max_old_sessions then break end
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
297 keep[token] = info;
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
298 end
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
299 all_old_sessions:set(session.username, keep);
12137
4e61aaa4e9b2 mod_smacks: Log error to silence luacheck
Kim Alvefur <zash@zash.se>
parents: 12136
diff changeset
300 elseif err then
4e61aaa4e9b2 mod_smacks: Log error to silence luacheck
Kim Alvefur <zash@zash.se>
parents: 12136
diff changeset
301 module:log("error", "Unable to retrieve old resumption counters: %s", err);
12136
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
302 end
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
303 end
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
304
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
305 module:log("debug", "Enabling stream management");
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
306 session.smacks = xmlns_sm;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
307
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
308 wrap_session(session, false);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
309
12080
9744a0ceb767 mod_smacks: Only include max resumption time when resumption is activated
Kim Alvefur <zash@zash.se>
parents: 12078
diff changeset
310 local resume_max;
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
311 local resume_token;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
312 local resume = stanza.attr.resume;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
313 if resume == "true" or resume == "1" then
12112
08a949e63541 mod_smacks: Use more compact resumption tokens
Kim Alvefur <zash@zash.se>
parents: 12081
diff changeset
314 resume_token = new_id();
12053
03e9587fbfd2 mod_smacks: Switch storage for tracking resumption tokens
Kim Alvefur <zash@zash.se>
parents: 12052
diff changeset
315 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
316 session.resumption_token = resume_token;
12080
9744a0ceb767 mod_smacks: Only include max resumption time when resumption is activated
Kim Alvefur <zash@zash.se>
parents: 12078
diff changeset
317 resume_max = tostring(resume_timeout);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
318 end
12080
9744a0ceb767 mod_smacks: Only include max resumption time when resumption is activated
Kim Alvefur <zash@zash.se>
parents: 12078
diff changeset
319 (session.sends2s or session.send)(st.stanza("enabled", { xmlns = xmlns_sm, id = resume_token, resume = resume, max = resume_max }));
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
320 return true;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
321 end
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
322 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
323 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
324
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
325 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
326 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
327 -- 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
328 -- stanzas and counting them is weird.
12081
6cc3135138d7 mod_smacks: Add future todo
Kim Alvefur <zash@zash.se>
parents: 12080
diff changeset
329 -- TODO unify sendq and smqueue
11936
3f49c35607ca mod_smacks: Fix timer lifetimes to follow session instead of module
Kim Alvefur <zash@zash.se>
parents: 11935
diff changeset
330 timer.add_task(1e-6, function ()
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
331 if can_do_smacks(session) then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
332 if stanza:get_child("sm", xmlns_sm3) then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
333 session.sends2s(st.stanza("enable", sm3_attr));
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
334 session.smacks = xmlns_sm3;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
335 elseif stanza:get_child("sm", xmlns_sm2) then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
336 session.sends2s(st.stanza("enable", sm2_attr));
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
337 session.smacks = xmlns_sm2;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
338 else
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
339 return;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
340 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
341 wrap_session_out(session, false);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
342 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
343 end);
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 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
347 module:log("debug", "Enabling stream management");
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
348 session.smacks = xmlns_sm;
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 wrap_session_in(session, false);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
351
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
352 -- FIXME Resume?
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
353
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
354 return true;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
355 end
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
356 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
357 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
358
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
359 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
360 if not origin.smacks then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
361 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
362 return;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
363 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
364 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
365 -- Reply with <a>
11977
9f7a6f7d13de mod_smacks: Factor out formatting of 'h' value
Kim Alvefur <zash@zash.se>
parents: 11976
diff changeset
366 (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
367 -- 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
368 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
369 return true;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
370 end
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
371 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
372 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
373
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
374 function handle_a(origin, stanza)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
375 if not origin.smacks then return; end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
376 origin.awaiting_ack = nil;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
377 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
378 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
379 origin.awaiting_ack_timer = nil;
11934
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 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
382 timer.stop(origin.delayed_ack_timer)
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
383 origin.delayed_ack_timer = nil;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
384 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
385 -- Remove handled stanzas from outgoing_stanza_queue
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
386 local h = tonumber(stanza.attr.h);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
387 if not h then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
388 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
389 return;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
390 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
391 local queue = origin.outgoing_stanza_queue;
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
392 local handled_stanza_count = h-queue:count_acked();
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
393 local acked, err = ack_errors.coerce(queue:ack(h)); -- luacheck: ignore 211/acked
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
394 if err then
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
395 origin.log("warn", "The client says it handled %d new stanzas, but we sent %d :)",
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
396 handled_stanza_count, queue:count_unacked());
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
397 origin.log("debug", "Client h: %d, our h: %d", tonumber(stanza.attr.h), queue:count_acked());
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
398 for i, item in queue._queue:items() do
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
399 origin.log("debug", "Q item %d: %s", i, item);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
400 end
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
401 origin:close(err);
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
402 return;
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
403 end
12125
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
404 tx_acked_stanzas:sample(handled_stanza_count);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
405
12125
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
406 origin.log("debug", "#queue = %d (acked: %d)", queue:count_unacked(), handled_stanza_count);
11937
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
407 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
408 return true;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
409 end
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
410 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
411 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
412
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
413 local function handle_unacked_stanzas(session)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
414 local queue = session.outgoing_stanza_queue;
12125
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
415 local unacked = queue:count_unacked()
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
416 if unacked > 0 then
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
417 tx_dropped_stanzas:sample(unacked);
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
418 session.smacks = false; -- Disable queueing
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
419 session.outgoing_stanza_queue = nil;
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
420 for stanza in queue._queue:consume() do
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
421 if not module:fire_event("delivery/failure", { session = session, stanza = stanza }) then
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
422 if stanza.attr.type ~= "error" and stanza.attr.to ~= session.full_jid then
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
423 local reply = st.error_reply(stanza, "cancel", "recipient-unavailable");
11997
1ea01660c79a mod_smacks: Refactor, simplify, optimize
Kim Alvefur <zash@zash.se>
parents: 11996
diff changeset
424 core_process_stanza(session, reply);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
425 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
426 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
427 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
428 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
429 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
430
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
431 -- 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
432 -- 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
433 -- 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
434 module:hook("delivery/failure", function(event)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
435 local session, stanza = event.session, event.stanza;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
436 -- Only deal with authenticated (c2s) sessions
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
437 if session.username then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
438 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
439 ( 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
440 -- 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
441 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
442 if mam_result ~= nil then
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
443 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
444 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
445 -- 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
446 -- because messages are already in MAM at this point (no need to frighten users)
12114
e32f90c81519 mod_smacks: Compact code using new stanza API
Kim Alvefur <zash@zash.se>
parents: 12112
diff changeset
447 local stanza_id = stanza:get_child_with_attr("stanza-id", "urn:xmpp:sid:0", "by", jid.bare(session.full_jid));
e32f90c81519 mod_smacks: Compact code using new stanza API
Kim Alvefur <zash@zash.se>
parents: 12112
diff changeset
448 stanza_id = stanza_id and stanza_id.attr.id;
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
449 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
450 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
451 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
452 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
453 -- store message in offline store, if this client does not use mam *and* was the last client online
12063
d308f6901397 mod_smacks: Simplify access to local user sessions
Kim Alvefur <zash@zash.se>
parents: 12062
diff changeset
454 local sessions = local_sessions[session.username] and local_sessions[session.username].sessions or nil;
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
455 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
456 local ok = module:fire_event("message/offline/handle", { origin = session, username = session.username, stanza = stanza });
12048
f087bfd021ad mod_smacks: Fix typo
Kim Alvefur <zash@zash.se>
parents: 12047
diff changeset
457 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
458 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
459 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
460 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
461 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
462 end);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
463
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
464 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
465 local session = event.session;
12054
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
466 if not session.smacks then return end
12060
3099e03b10bd mod_smacks: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 12059
diff changeset
467 if not session.resumption_token then
3099e03b10bd mod_smacks: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 12059
diff changeset
468 local queue = session.outgoing_stanza_queue;
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
469 if queue:count_unacked() > 0 then
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
470 session.log("debug", "Destroying session with %d unacked stanzas", queue:count_unacked());
12060
3099e03b10bd mod_smacks: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 12059
diff changeset
471 handle_unacked_stanzas(session);
3099e03b10bd mod_smacks: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 12059
diff changeset
472 end
12054
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
473 return
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
474 end
12072
ef0f174488af mod_smacks: Fix duplicate sleep behavior when replacing a session
Kim Alvefur <zash@zash.se>
parents: 12070
diff changeset
475 if session.hibernating then return end
12054
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
476
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
477 session.hibernating = os_time();
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
478 session.hibernating_watchdog = watchdog.new(resume_timeout, function()
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
479 session.log("debug", "mod_smacks hibernation timeout reached...");
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
480 if session.destroyed then
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
481 session.log("debug", "The session has already been destroyed");
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
482 return
12061
31a7e0ac6928 mod_smacks: Skip hibernation logic if session was closed or replaced
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
483 elseif not session.resumption_token then
31a7e0ac6928 mod_smacks: Skip hibernation logic if session was closed or replaced
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
484 -- This should normally not happen, the watchdog should be canceled from session:close()
31a7e0ac6928 mod_smacks: Skip hibernation logic if session was closed or replaced
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
485 session.log("debug", "The session has already been resumed or replaced");
31a7e0ac6928 mod_smacks: Skip hibernation logic if session was closed or replaced
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
486 return
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
487 end
12054
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
488
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
489 session.log("debug", "Destroying session for hibernating too long");
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
490 session_registry[jid.join(session.username, session.host, session.resumption_token)] = nil;
12135
fa804c2db747 mod_smacks: Record timestamp with persisted counters
Kim Alvefur <zash@zash.se>
parents: 12125
diff changeset
491 old_session_registry:set(session.username, session.resumption_token,
fa804c2db747 mod_smacks: Record timestamp with persisted counters
Kim Alvefur <zash@zash.se>
parents: 12125
diff changeset
492 { h = session.handled_stanza_count; t = os.time() });
12054
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
493 session.resumption_token = nil;
12059
70a55fbe447c mod_smacks: Stop stanzas from being queued on hibernation timeout
Kim Alvefur <zash@zash.se>
parents: 12056
diff changeset
494 session.resending_unacked = true; -- stop outgoing_stanza_filter from re-queueing anything anymore
12054
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
495 sessionmanager.destroy_session(session, "Hibernating too long");
12125
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
496 sessions_expired(1);
12054
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
497 end);
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
498 if session.conn then
12070
e662c172ea0f mod_smacks: Remove references to connection on hibernation
Kim Alvefur <zash@zash.se>
parents: 12069
diff changeset
499 local conn = session.conn;
e662c172ea0f mod_smacks: Remove references to connection on hibernation
Kim Alvefur <zash@zash.se>
parents: 12069
diff changeset
500 c2s_sessions[conn] = nil;
e662c172ea0f mod_smacks: Remove references to connection on hibernation
Kim Alvefur <zash@zash.se>
parents: 12069
diff changeset
501 session.conn = nil;
e662c172ea0f mod_smacks: Remove references to connection on hibernation
Kim Alvefur <zash@zash.se>
parents: 12069
diff changeset
502 conn:close();
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
503 end
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
504 module:fire_event("smacks-hibernation-start", { origin = session; queue = session.outgoing_stanza_queue:table() });
12054
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
505 return true; -- Postpone destruction for now
11934
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 function handle_s2s_destroyed(event)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
509 local session = event.session;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
510 local queue = session.outgoing_stanza_queue;
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
511 if queue and queue:count_unacked() > 0 then
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
512 session.log("warn", "Destroying session with %d unacked stanzas", queue:count_unacked());
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
513 if s2s_resend then
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
514 for stanza in queue:consume() do
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
515 module:send(stanza);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
516 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
517 session.outgoing_stanza_queue = nil;
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 handle_unacked_stanzas(session);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
520 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
521 end
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
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
524 module:hook("s2sout-destroyed", handle_s2s_destroyed);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
525 module:hook("s2sin-destroyed", handle_s2s_destroyed);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
526
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
527 local function get_session_id(session)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
528 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
529 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
530
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
531 function handle_resume(session, stanza, xmlns_sm)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
532 if session.full_jid then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
533 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
534 session.send(st.stanza("failed", { xmlns = xmlns_sm })
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
535 :tag("unexpected-request", { xmlns = xmlns_errors })
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
536 );
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
537 return true;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
538 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
539
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
540 local id = stanza.attr.previd;
12053
03e9587fbfd2 mod_smacks: Switch storage for tracking resumption tokens
Kim Alvefur <zash@zash.se>
parents: 12052
diff changeset
541 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
542 if not original_session then
12051
c32ef09ab452 mod_smacks: Persist old counter values to storage
Kim Alvefur <zash@zash.se>
parents: 12050
diff changeset
543 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
544 if old_session then
12066
f07c8240a71d mod_smacks: Split log messages for when an old session exists in storage
Kim Alvefur <zash@zash.se>
parents: 12065
diff changeset
545 session.log("debug", "Tried to resume old expired session with id %s", id);
11977
9f7a6f7d13de mod_smacks: Factor out formatting of 'h' value
Kim Alvefur <zash@zash.se>
parents: 11976
diff changeset
546 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
547 :tag("item-not-found", { xmlns = xmlns_errors })
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
548 );
12052
d17b8fcf11c7 mod_smacks: Clear resumption token from persistent storage on resumption
Kim Alvefur <zash@zash.se>
parents: 12051
diff changeset
549 old_session_registry:set(session.username, id, nil);
12125
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
550 resumption_expired(1);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
551 else
12066
f07c8240a71d mod_smacks: Split log messages for when an old session exists in storage
Kim Alvefur <zash@zash.se>
parents: 12065
diff changeset
552 session.log("debug", "Tried to resume non-existent session with id %s", id);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
553 session.send(st.stanza("failed", { xmlns = xmlns_sm })
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
554 :tag("item-not-found", { xmlns = xmlns_errors })
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
555 );
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
556 end;
12054
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
557 else
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
558 if original_session.hibernating_watchdog then
12069
b9e08cbd032b mod_smacks: Add more logging
Kim Alvefur <zash@zash.se>
parents: 12068
diff changeset
559 original_session.log("debug", "Letting the watchdog go");
12054
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
560 original_session.hibernating_watchdog:cancel();
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
561 original_session.hibernating_watchdog = nil;
12074
b892f5489d79 mod_smacks: Check if session was really sleeping before logging message
Kim Alvefur <zash@zash.se>
parents: 12073
diff changeset
562 elseif session.hibernating then
12069
b9e08cbd032b mod_smacks: Add more logging
Kim Alvefur <zash@zash.se>
parents: 12068
diff changeset
563 original_session.log("error", "Hibernating session has no watchdog!")
12054
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
564 end
12125
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
565 -- zero age = was not hibernating yet
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
566 local age = 0;
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
567 if original_session.hibernating then
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
568 local now = os_time();
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
569 age = now - original_session.hibernating;
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
570 end
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
571 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
572 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
573 -- 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
574 if original_session.conn then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
575 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
576 local conn = original_session.conn;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
577 c2s_sessions[conn] = nil;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
578 conn:close();
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
579 end
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
580
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
581 local migrated_session_log = session.log;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
582 original_session.ip = session.ip;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
583 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
584 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
585 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
586 original_session.rawsend.conn = original_session.conn;
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
587 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
588 original_session.send.session = original_session;
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
589 original_session.close = session.close;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
590 original_session.filter = session.filter;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
591 original_session.filter.session = original_session;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
592 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
593 original_session.send.filter = original_session.filter;
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
594 original_session.stream = session.stream;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
595 original_session.secure = session.secure;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
596 original_session.hibernating = nil;
12046
372ec5cd0f51 mod_smacks: Increment a counter for each resumption
Kim Alvefur <zash@zash.se>
parents: 12045
diff changeset
597 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
598 session.log = original_session.log;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
599 session.type = original_session.type;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
600 wrap_session(original_session, true);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
601 -- 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
602 original_session.stream:set_session(original_session);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
603 -- Similar for connlisteners
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
604 c2s_sessions[session.conn] = original_session;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
605
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
606 local queue = original_session.outgoing_stanza_queue;
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
607 local h = tonumber(stanza.attr.h);
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
608
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
609 original_session.log("debug", "Pre-resumption #queue = %d", queue:count_unacked())
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
610 local acked, err = ack_errors.coerce(queue:ack(h)); -- luacheck: ignore 211/acked
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
611
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
612 if not err and not queue:resumable() then
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
613 err = ack_errors.new("overflow");
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
614 end
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
615
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
616 if err or not queue:resumable() then
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
617 original_session.send(st.stanza("failed",
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
618 { xmlns = xmlns_sm; h = format_h(original_session.handled_stanza_count); previd = id }));
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
619 original_session:close(err);
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
620 return false;
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
621 end
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
622
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
623 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
624 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
625
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
626 -- 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
627 -- ...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
628 -- 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
629 -- to the outgoing queue again
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
630
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
631 session.log("debug", "resending all unacked stanzas that are still queued after resume, #queue = %d", queue:count_unacked());
12047
81858fd6f198 mod_smacks: Flag both sessions to prevent duplicating queue on resumption
Kim Alvefur <zash@zash.se>
parents: 12046
diff changeset
632 -- 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
633 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
634 original_session.resending_unacked = true;
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
635 for _, queued_stanza in queue:resume() do
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
636 session.send(queued_stanza);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
637 end
12045
a6c821720cb6 mod_smacks: Back out 48f8fa6cea7b - back in a86ae74da96c again
Kim Alvefur <zash@zash.se>
parents: 12044
diff changeset
638 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
639 original_session.resending_unacked = nil;
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
640 session.log("debug", "all stanzas resent, now disabling send() in this migrated session, #queue = %d", queue:count_unacked());
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
641 function session.send(stanza) -- luacheck: ignore 432
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
642 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
643 return false;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
644 end
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
645 module:fire_event("smacks-hibernation-end", {origin = session, resumed = original_session, queue = queue:table()});
12067
97c377de8083 mod_smacks: Fix to not wait for acks from before a resumption
Kim Alvefur <zash@zash.se>
parents: 12066
diff changeset
646 original_session.awaiting_ack = nil; -- Don't wait for acks from before the resumption
12068
c3790ffdf467 mod_smacks: Remove useless delay in requesting ack on resumption
Kim Alvefur <zash@zash.se>
parents: 12067
diff changeset
647 request_ack_now_if_needed(original_session, true, "handle_resume", nil);
12125
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
648 resumption_age:sample(age);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
649 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
650 return true;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
651 end
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
652 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
653 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
654
11937
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
655 -- 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
656 -- 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
657 local request_ack_events = {
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
658 ["csi-client-active"] = true;
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
659 ["csi-flushing"] = false;
11938
6da703cb4c04 mod_smacks: Optimize scheduling of ack requests
Kim Alvefur <zash@zash.se>
parents: 11937
diff changeset
660 ["c2s-pre-ondrain"] = false;
6da703cb4c04 mod_smacks: Optimize scheduling of ack requests
Kim Alvefur <zash@zash.se>
parents: 11937
diff changeset
661 ["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
662 };
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
663
11937
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
664 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
665 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
666 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
667 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
668 end);
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
669 end
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
670
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
671 local function handle_read_timeout(event)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
672 local session = event.session;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
673 if session.smacks then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
674 if session.awaiting_ack then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
675 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
676 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
677 session.awaiting_ack_timer = nil;
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
678 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
679 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
680 timer.stop(session.delayed_ack_timer);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
681 session.delayed_ack_timer = nil;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
682 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
683 return false; -- Kick the session
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
684 end
11937
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
685 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
686 return true;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
687 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
688 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
689
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
690 module:hook("s2s-read-timeout", handle_read_timeout);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
691 module:hook("c2s-read-timeout", handle_read_timeout);
12064
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
692
12065
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
693 module:hook_global("server-stopping", function(event)
12077
061d516d8ac5 mod_smacks: Fix error on shutdown if loaded on Component
Kim Alvefur <zash@zash.se>
parents: 12076
diff changeset
694 if not local_sessions then
061d516d8ac5 mod_smacks: Fix error on shutdown if loaded on Component
Kim Alvefur <zash@zash.se>
parents: 12076
diff changeset
695 -- not a VirtualHost, no user sessions
061d516d8ac5 mod_smacks: Fix error on shutdown if loaded on Component
Kim Alvefur <zash@zash.se>
parents: 12076
diff changeset
696 return
061d516d8ac5 mod_smacks: Fix error on shutdown if loaded on Component
Kim Alvefur <zash@zash.se>
parents: 12076
diff changeset
697 end
12065
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
698 local reason = event.reason;
12078
2c13aaae35ac mod_smacks: Fix typos
Kim Alvefur <zash@zash.se>
parents: 12077
diff changeset
699 -- Close smacks-enabled sessions ourselves instead of letting mod_c2s close
12064
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
700 -- it, which invalidates the smacks session. This allows preserving the
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
701 -- counter value, so it can be communicated to the client when it tries to
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
702 -- resume the lost session after a restart.
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
703 for _, user in pairs(local_sessions) do
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
704 for _, session in pairs(user.sessions) do
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
705 if session.resumption_token then
12135
fa804c2db747 mod_smacks: Record timestamp with persisted counters
Kim Alvefur <zash@zash.se>
parents: 12125
diff changeset
706 if old_session_registry:set(session.username, session.resumption_token,
fa804c2db747 mod_smacks: Record timestamp with persisted counters
Kim Alvefur <zash@zash.se>
parents: 12125
diff changeset
707 { h = session.handled_stanza_count; t = os.time() }) then
12064
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
708 session.resumption_token = nil;
12065
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
709
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
710 -- Deal with unacked stanzas
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
711 if session.outgoing_stanza_queue then
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
712 handle_unacked_stanzas(session);
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
713 end
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
714
12064
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
715 if session.conn then
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
716 session.conn:close()
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
717 session.conn = nil;
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
718 -- Now when mod_c2s gets here, it will immediately destroy the
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
719 -- session since it is unconnected.
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
720 end
12065
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
721
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
722 -- And make sure nobody tries to send anything
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
723 session:close{ condition = "system-shutdown", text = reason };
12064
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
724 end
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
725 end
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
726 end
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
727 end
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
728 end, -90);