Annotate

plugins/mod_smacks.lua @ 13135:3fd24e1945b0

mod_storage_internal: Lazy-load archive items while iterating Very large list files previously ran into limits of the Lua parser, or just caused Prosody to freeze while parsing. Using the new index we can parse individual items one at a time. This probably won't reduce overall CPU usage, probably the opposite, but it will reduce the number of items in memory at once and allow collection of items after we iterated past them.
author Kim Alvefur <zash@zash.se>
date Wed, 12 May 2021 01:25:44 +0200
parent 13023:cbe58f747d48
child 13209:c8d949cf6b09
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
12471
a3b12eeedd4b mod_smacks: Improve activation of smacks on outgoing s2s
Kim Alvefur <zash@zash.se>
parents: 12272
diff changeset
5 -- Copyright (C) 2012-2022 Kim Alvefur
11934
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 --
12471
a3b12eeedd4b mod_smacks: Improve activation of smacks on outgoing s2s
Kim Alvefur <zash@zash.se>
parents: 12272
diff changeset
13 -- TODO unify sendq and smqueue
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14
11981
5d8264f464a2 mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents: 11980
diff changeset
15 local tonumber = tonumber;
5d8264f464a2 mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents: 11980
diff changeset
16 local tostring = tostring;
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local os_time = os.time;
11981
5d8264f464a2 mod_smacks: Reorder imports etc
Kim Alvefur <zash@zash.se>
parents: 11980
diff changeset
18
12125
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
19 -- 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
20 -- "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
21 -- iterate over all the queues.
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
22 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
23 local tx_dropped_stanzas = module:metric(
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
24 "histogram",
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
25 "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
26 {},
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
27 {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
28 ):with_labels();
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
29 local tx_acked_stanzas = module:metric(
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
30 "histogram",
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
31 "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
32 {},
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
33 {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
34 ):with_labels();
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
35
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
36 -- 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
37 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
38 local resumption_age = module:metric(
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
39 "histogram",
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
40 "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
41 {},
12272
fe0f5c47fda3 mod_smacks: Tweak resumption age buckets towards multiples of 60
Kim Alvefur <zash@zash.se>
parents: 12137
diff changeset
42 {buckets = { 0, 1, 2, 5, 10, 30, 60, 120, 300, 600 }}
12125
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
43 ):with_labels();
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
44 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
45 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
46
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
47
12977
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12851
diff changeset
48 local datetime = require "prosody.util.datetime";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12851
diff changeset
49 local add_filter = require "prosody.util.filters".add_filter;
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12851
diff changeset
50 local jid = require "prosody.util.jid";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12851
diff changeset
51 local smqueue = require "prosody.util.smqueue";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12851
diff changeset
52 local st = require "prosody.util.stanza";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12851
diff changeset
53 local timer = require "prosody.util.timer";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12851
diff changeset
54 local new_id = require "prosody.util.id".short;
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12851
diff changeset
55 local watchdog = require "prosody.util.watchdog";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12851
diff changeset
56 local it = require"prosody.util.iterators";
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57
12977
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12851
diff changeset
58 local sessionmanager = require "prosody.core.sessionmanager";
11981
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
12803
2e12290820e8 mod_smacks: Factor out resumption token table key generation
Kim Alvefur <zash@zash.se>
parents: 12802
diff changeset
87 local function registry_key(session, id)
2e12290820e8 mod_smacks: Factor out resumption token table key generation
Kim Alvefur <zash@zash.se>
parents: 12802
diff changeset
88 return jid.join(session.username, session.host, id or session.resumption_token);
2e12290820e8 mod_smacks: Factor out resumption token table key generation
Kim Alvefur <zash@zash.se>
parents: 12802
diff changeset
89 end
2e12290820e8 mod_smacks: Factor out resumption token table key generation
Kim Alvefur <zash@zash.se>
parents: 12802
diff changeset
90
12518
73ee3855f970 mod_smacks: Factor out some convenience functions
Kim Alvefur <zash@zash.se>
parents: 12504
diff changeset
91 local function track_session(session, id)
12803
2e12290820e8 mod_smacks: Factor out resumption token table key generation
Kim Alvefur <zash@zash.se>
parents: 12802
diff changeset
92 session_registry[registry_key(session, id)] = session;
12518
73ee3855f970 mod_smacks: Factor out some convenience functions
Kim Alvefur <zash@zash.se>
parents: 12504
diff changeset
93 session.resumption_token = id;
73ee3855f970 mod_smacks: Factor out some convenience functions
Kim Alvefur <zash@zash.se>
parents: 12504
diff changeset
94 end
73ee3855f970 mod_smacks: Factor out some convenience functions
Kim Alvefur <zash@zash.se>
parents: 12504
diff changeset
95
73ee3855f970 mod_smacks: Factor out some convenience functions
Kim Alvefur <zash@zash.se>
parents: 12504
diff changeset
96 local function save_old_session(session)
12803
2e12290820e8 mod_smacks: Factor out resumption token table key generation
Kim Alvefur <zash@zash.se>
parents: 12802
diff changeset
97 session_registry[registry_key(session)] = nil;
12518
73ee3855f970 mod_smacks: Factor out some convenience functions
Kim Alvefur <zash@zash.se>
parents: 12504
diff changeset
98 return old_session_registry:set(session.username, session.resumption_token,
73ee3855f970 mod_smacks: Factor out some convenience functions
Kim Alvefur <zash@zash.se>
parents: 12504
diff changeset
99 { h = session.handled_stanza_count; t = os.time() })
73ee3855f970 mod_smacks: Factor out some convenience functions
Kim Alvefur <zash@zash.se>
parents: 12504
diff changeset
100 end
73ee3855f970 mod_smacks: Factor out some convenience functions
Kim Alvefur <zash@zash.se>
parents: 12504
diff changeset
101
73ee3855f970 mod_smacks: Factor out some convenience functions
Kim Alvefur <zash@zash.se>
parents: 12504
diff changeset
102 local function clear_old_session(session, id)
12803
2e12290820e8 mod_smacks: Factor out resumption token table key generation
Kim Alvefur <zash@zash.se>
parents: 12802
diff changeset
103 session_registry[registry_key(session, id)] = nil;
12518
73ee3855f970 mod_smacks: Factor out some convenience functions
Kim Alvefur <zash@zash.se>
parents: 12504
diff changeset
104 return old_session_registry:set(session.username, id or session.resumption_token, nil)
73ee3855f970 mod_smacks: Factor out some convenience functions
Kim Alvefur <zash@zash.se>
parents: 12504
diff changeset
105 end
73ee3855f970 mod_smacks: Factor out some convenience functions
Kim Alvefur <zash@zash.se>
parents: 12504
diff changeset
106
12977
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12851
diff changeset
107 local ack_errors = require"prosody.util.error".init("mod_smacks", xmlns_sm3, {
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
108 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
109 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
110 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
111 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
112 });
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
113
12977
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 12851
diff changeset
114 local enable_errors = require "prosody.util.error".init("mod_smacks", xmlns_sm3, {
12688
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
115 already_enabled = { condition = "unexpected-request", text = "Stream management is already enabled" };
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
116 bind_required = { condition = "unexpected-request", text = "Client must bind a resource before enabling stream management" };
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
117 unavailable = { condition = "service-unavailable", text = "Stream management is not available for this stream" };
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
118 -- Resumption
12682
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
119 expired = { condition = "item-not-found", text = "Session expired, and cannot be resumed" };
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
120 already_bound = { condition = "unexpected-request", text = "Cannot resume another session after a resource is bound" };
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
121 unknown_session = { condition = "item-not-found", text = "Unknown session" };
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
122 });
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
123
12076
ad1fe36eaae7 mod_smacks: Fix typo (thanks codespell)
Kim Alvefur <zash@zash.se>
parents: 12075
diff changeset
124 -- 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
125
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
126 local function ack_delayed(session, stanza)
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 -- 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
128 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
129 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
130 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
131 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
132 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
133 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 session.delayed_ack_timer = nil;
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
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 local function can_do_smacks(session, advertise_only)
12688
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
138 if session.smacks then return false, enable_errors.new("already_enabled"); end
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 local session_type = session.type;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 if session.username then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 if not(advertise_only) and not(session.resource) then -- Fail unless we're only advertising sm
12688
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
143 return false, enable_errors.new("bind_required");
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 return true;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 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
147 return true;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 end
12688
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
149 return false, enable_errors.new("unavailable");
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 module:hook("stream-features",
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 function (event)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 if can_do_smacks(event.origin, true) then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 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
156 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
157 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 end);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 module:hook("s2s-stream-features",
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 function (event)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 if can_do_smacks(event.origin, true) then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 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
164 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
165 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 end);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167
11937
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
168 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
169 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
170 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
171 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
172 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
173 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
174 if force then return force end
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 local queue = session.outgoing_stanza_queue;
12522
1671cb924002 mod_smacks: Fix to use current method of counting acked stanzas
Kim Alvefur <zash@zash.se>
parents: 12272
diff changeset
176 local expected_h = queue:count_acked() + queue:count_unacked();
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 local max_unacked = max_unacked_stanzas;
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
178 if session.state == "inactive" then
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 max_unacked = max_inactive_unacked_stanzas;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 end
12078
2c13aaae35ac mod_smacks: Fix typos
Kim Alvefur <zash@zash.se>
parents: 12077
diff changeset
181 -- 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
182 -- 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
183 -- 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
184 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
185 end
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
186
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
187 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
188 local queue = session.outgoing_stanza_queue;
12524
dd5ab9a6b599 mod_smacks: Remove debug log references to timer (not used anymore)
Kim Alvefur <zash@zash.se>
parents: 12523
diff changeset
189 session.log("debug", "Sending <r> from %s - #queue=%d", reason, queue:count_unacked());
12549
3729a6bdb562 mod_smacks: Fix #1761 by setting a flag earlier
Kim Alvefur <zash@zash.se>
parents: 12526
diff changeset
190 session.awaiting_ack = true;
11937
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
191 (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
192 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
193 -- expected_h could be lower than this expression e.g. more stanzas added to the queue meanwhile)
12522
1671cb924002 mod_smacks: Fix to use current method of counting acked stanzas
Kim Alvefur <zash@zash.se>
parents: 12272
diff changeset
194 session.last_requested_h = queue:count_acked() + queue:count_unacked();
11937
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
195 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
196 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
197 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
198 end);
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
199 end
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
200 end
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
201
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
202 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
203 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
204 request_ack(session, reason);
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
205 end
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
206 end
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
207
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 local function outgoing_stanza_filter(stanza, session)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 -- 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
210 -- supposed to be nil.
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 -- 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
212 -- stanzas/out filter can get called before this one and adds the xmlns.
12073
4cbe7979a92a mod_smacks: Fix traceback when bouncing unacked stanzas
Kim Alvefur <zash@zash.se>
parents: 12072
diff changeset
213 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
214 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
215 (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
216 and not stanza.name:find":";
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217
12045
a6c821720cb6 mod_smacks: Back out 48f8fa6cea7b - back in a86ae74da96c again
Kim Alvefur <zash@zash.se>
parents: 12044
diff changeset
218 if is_stanza then
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 local queue = session.outgoing_stanza_queue;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 local cached_stanza = st.clone(stanza);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
221
11983
27f2539b4f87 mod_smacks: Remove dead conditional
Kim Alvefur <zash@zash.se>
parents: 11982
diff changeset
222 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
223 cached_stanza = cached_stanza:tag("delay", {
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
224 xmlns = xmlns_delay,
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
225 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
226 stamp = datetime.datetime()
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
227 });
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
230 queue:push(cached_stanza);
12125
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
231 tx_queued_stanzas(1);
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
232
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
233 if session.hibernating then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
234 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
235 -- 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
236 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
237 return nil;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
238 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
239 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
240 return stanza;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
241 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
242
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
243 local function count_incoming_stanzas(stanza, session)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
244 if not stanza.attr.xmlns then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
245 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
246 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
247 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
248 return stanza;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
249 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
250
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
251 local function wrap_session_out(session, resume)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
252 if not resume then
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
253 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
254 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
255
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256 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
257
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
258 return session;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
259 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
260
11975
fcea4d9e7502 mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents: 11952
diff changeset
261 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
262 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
263 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
264 if session.resumption_token then
12069
b9e08cbd032b mod_smacks: Add more logging
Kim Alvefur <zash@zash.se>
parents: 12068
diff changeset
265 session.log("debug", "Revoking resumption token");
12518
73ee3855f970 mod_smacks: Factor out some convenience functions
Kim Alvefur <zash@zash.se>
parents: 12504
diff changeset
266 clear_old_session(session);
11975
fcea4d9e7502 mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents: 11952
diff changeset
267 session.resumption_token = nil;
12069
b9e08cbd032b mod_smacks: Add more logging
Kim Alvefur <zash@zash.se>
parents: 12068
diff changeset
268 else
b9e08cbd032b mod_smacks: Add more logging
Kim Alvefur <zash@zash.se>
parents: 12068
diff changeset
269 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
270 end
12062
4972244fe87b mod_smacks: Cancel hibernation when session is closed
Kim Alvefur <zash@zash.se>
parents: 12061
diff changeset
271 if session.hibernating_watchdog then
12069
b9e08cbd032b mod_smacks: Add more logging
Kim Alvefur <zash@zash.se>
parents: 12068
diff changeset
272 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
273 -- 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
274 -- 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
275 session.hibernating_watchdog:cancel();
4972244fe87b mod_smacks: Cancel hibernation when session is closed
Kim Alvefur <zash@zash.se>
parents: 12061
diff changeset
276 session.hibernating_watchdog = nil;
12069
b9e08cbd032b mod_smacks: Add more logging
Kim Alvefur <zash@zash.se>
parents: 12068
diff changeset
277 else
b9e08cbd032b mod_smacks: Add more logging
Kim Alvefur <zash@zash.se>
parents: 12068
diff changeset
278 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
279 end
11975
fcea4d9e7502 mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents: 11952
diff changeset
280 -- 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
281 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
282 (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
283 xmlns = session.smacks;
11977
9f7a6f7d13de mod_smacks: Factor out formatting of 'h' value
Kim Alvefur <zash@zash.se>
parents: 11976
diff changeset
284 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
285 }));
fcea4d9e7502 mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents: 11952
diff changeset
286 end
fcea4d9e7502 mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents: 11952
diff changeset
287 end);
fcea4d9e7502 mod_smacks: Use new pre-session-close event instead of monkeypatch
Kim Alvefur <zash@zash.se>
parents: 11952
diff changeset
288
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
289 local function wrap_session_in(session, resume)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290 if not resume then
12125
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
291 sessions_started(1);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
292 session.handled_stanza_count = 0;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
293 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
294 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
295
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
296 return session;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
297 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
298
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
299 local function wrap_session(session, resume)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
300 wrap_session_out(session, resume);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
301 wrap_session_in(session, resume);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
302 return session;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
303 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
304
12688
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
305 function do_enable(session, stanza)
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
306 local ok, err = can_do_smacks(session);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
307 if not ok then
12688
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
308 session.log("warn", "Failed to enable smacks: %s", err.text); -- TODO: XEP doesn't say we can send error text, should it?
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
309 return nil, err;
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
310 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
311
12136
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
312 if session.username then
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
313 local old_sessions, err = all_old_sessions:get(session.username);
12519
d935af51c644 mod_smacks: Use session logging for remaining log messages
Kim Alvefur <zash@zash.se>
parents: 12518
diff changeset
314 session.log("debug", "Old sessions: %q", old_sessions)
12136
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
315 if old_sessions then
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
316 local keep, count = {}, 0;
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
317 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
318 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
319 end) do
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
320 count = count + 1;
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
321 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
322 keep[token] = info;
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
323 end
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
324 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
325 elseif err then
12519
d935af51c644 mod_smacks: Use session logging for remaining log messages
Kim Alvefur <zash@zash.se>
parents: 12518
diff changeset
326 session.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
327 end
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
328 end
6366240d2edb mod_smacks: Limit "old" session resumption counters
Kim Alvefur <zash@zash.se>
parents: 12135
diff changeset
329
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
330 local resume_token;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
331 local resume = stanza.attr.resume;
12796
d7853bbc88ea mod_smacks: Disable resumption behavior on s2s
Kim Alvefur <zash@zash.se>
parents: 12549
diff changeset
332 if (resume == "true" or resume == "1") and session.username then
d7853bbc88ea mod_smacks: Disable resumption behavior on s2s
Kim Alvefur <zash@zash.se>
parents: 12549
diff changeset
333 -- resumption on s2s is not currently supported
12112
08a949e63541 mod_smacks: Use more compact resumption tokens
Kim Alvefur <zash@zash.se>
parents: 12081
diff changeset
334 resume_token = new_id();
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
335 end
12688
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
336
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
337 return {
12719
268b1a9162a9 mod_smacks: Add type field to results so actions can be later distinguished
Matthew Wild <mwild1@gmail.com>
parents: 12691
diff changeset
338 type = "enabled";
12688
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
339 id = resume_token;
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
340 resume_max = resume_token and tostring(resume_timeout) or nil;
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
341 session = session;
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
342 finish = function ()
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
343 session.log("debug", "Enabling stream management");
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
344
12720
0a137436490f mod_smacks: Set session flag during successful enable
Matthew Wild <mwild1@gmail.com>
parents: 12719
diff changeset
345 session.smacks = stanza.attr.xmlns;
12800
06ba2f8cee47 mod_smacks: Only track resumable sessions
Kim Alvefur <zash@zash.se>
parents: 12745
diff changeset
346 if resume_token then
06ba2f8cee47 mod_smacks: Only track resumable sessions
Kim Alvefur <zash@zash.se>
parents: 12745
diff changeset
347 track_session(session, resume_token);
06ba2f8cee47 mod_smacks: Only track resumable sessions
Kim Alvefur <zash@zash.se>
parents: 12745
diff changeset
348 end
12688
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
349 wrap_session(session, false);
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
350 end;
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
351 };
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
352 end
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
353
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
354 function handle_enable(session, stanza, xmlns_sm)
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
355 local enabled, err = do_enable(session, stanza);
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
356 if not enabled then
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
357 (session.sends2s or session.send)(st.stanza("failed", { xmlns = xmlns_sm }):add_error(err));
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
358 return true;
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
359 end
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
360
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
361 (session.sends2s or session.send)(st.stanza("enabled", {
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
362 xmlns = xmlns_sm;
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
363 id = enabled.id;
12745
2cbf0e9314ff mod_smacks: Change boolean attribute from '1' to 'true' for compatibility
Matthew Wild <mwild1@gmail.com>
parents: 12739
diff changeset
364 resume = enabled.id and "true" or nil; -- COMPAT w/ Conversations 2.10.10 requires 'true' not '1'
12688
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
365 max = enabled.resume_max;
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
366 }));
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
367
12691
90d90b540b6b mod_smacks: Set session.smacks after sending <enabled/> to fix traceback
Matthew Wild <mwild1@gmail.com>
parents: 12689
diff changeset
368 session.smacks = xmlns_sm;
12688
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
369 enabled.finish();
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
370
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
371 return true;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
372 end
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
373 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
374 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
375
12471
a3b12eeedd4b mod_smacks: Improve activation of smacks on outgoing s2s
Kim Alvefur <zash@zash.se>
parents: 12272
diff changeset
376 module:hook_tag("http://etherx.jabber.org/streams", "features", function(session, stanza)
a3b12eeedd4b mod_smacks: Improve activation of smacks on outgoing s2s
Kim Alvefur <zash@zash.se>
parents: 12272
diff changeset
377 if can_do_smacks(session) then
a3b12eeedd4b mod_smacks: Improve activation of smacks on outgoing s2s
Kim Alvefur <zash@zash.se>
parents: 12272
diff changeset
378 session.smacks_feature = stanza:get_child("sm", xmlns_sm3) or stanza:get_child("sm", xmlns_sm2);
a3b12eeedd4b mod_smacks: Improve activation of smacks on outgoing s2s
Kim Alvefur <zash@zash.se>
parents: 12272
diff changeset
379 end
a3b12eeedd4b mod_smacks: Improve activation of smacks on outgoing s2s
Kim Alvefur <zash@zash.se>
parents: 12272
diff changeset
380 end);
a3b12eeedd4b mod_smacks: Improve activation of smacks on outgoing s2s
Kim Alvefur <zash@zash.se>
parents: 12272
diff changeset
381
a3b12eeedd4b mod_smacks: Improve activation of smacks on outgoing s2s
Kim Alvefur <zash@zash.se>
parents: 12272
diff changeset
382 module:hook("s2sout-established", function (event)
a3b12eeedd4b mod_smacks: Improve activation of smacks on outgoing s2s
Kim Alvefur <zash@zash.se>
parents: 12272
diff changeset
383 local session = event.session;
a3b12eeedd4b mod_smacks: Improve activation of smacks on outgoing s2s
Kim Alvefur <zash@zash.se>
parents: 12272
diff changeset
384 if not session.smacks_feature then return end
a3b12eeedd4b mod_smacks: Improve activation of smacks on outgoing s2s
Kim Alvefur <zash@zash.se>
parents: 12272
diff changeset
385
a3b12eeedd4b mod_smacks: Improve activation of smacks on outgoing s2s
Kim Alvefur <zash@zash.se>
parents: 12272
diff changeset
386 session.smacks = session.smacks_feature.attr.xmlns;
12504
c589874fe348 mod_smacks: Initialize queue before sending <enable>
Kim Alvefur <zash@zash.se>
parents: 12471
diff changeset
387 wrap_session_out(session, false);
12471
a3b12eeedd4b mod_smacks: Improve activation of smacks on outgoing s2s
Kim Alvefur <zash@zash.se>
parents: 12272
diff changeset
388 session.sends2s(st.stanza("enable", { xmlns = session.smacks }));
a3b12eeedd4b mod_smacks: Improve activation of smacks on outgoing s2s
Kim Alvefur <zash@zash.se>
parents: 12272
diff changeset
389 end);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
390
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
391 function handle_enabled(session, stanza, xmlns_sm) -- luacheck: ignore 212/stanza
12519
d935af51c644 mod_smacks: Use session logging for remaining log messages
Kim Alvefur <zash@zash.se>
parents: 12518
diff changeset
392 session.log("debug", "Enabling stream management");
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
393 session.smacks = xmlns_sm;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
394
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
395 wrap_session_in(session, false);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
396
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
397 -- FIXME Resume?
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
398
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
399 return true;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
400 end
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
401 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
402 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
403
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
404 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
405 if not origin.smacks then
12519
d935af51c644 mod_smacks: Use session logging for remaining log messages
Kim Alvefur <zash@zash.se>
parents: 12518
diff changeset
406 origin.log("debug", "Received ack request from non-smack-enabled session");
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
407 return;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
408 end
12519
d935af51c644 mod_smacks: Use session logging for remaining log messages
Kim Alvefur <zash@zash.se>
parents: 12518
diff changeset
409 origin.log("debug", "Received ack request, acking for %d", origin.handled_stanza_count);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
410 -- Reply with <a>
11977
9f7a6f7d13de mod_smacks: Factor out formatting of 'h' value
Kim Alvefur <zash@zash.se>
parents: 11976
diff changeset
411 (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
412 -- 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
413 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
414 return true;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
415 end
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
416 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
417 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
418
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
419 function handle_a(origin, stanza)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
420 if not origin.smacks then return; end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
421 origin.awaiting_ack = nil;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
422 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
423 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
424 origin.awaiting_ack_timer = nil;
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 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
427 timer.stop(origin.delayed_ack_timer)
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
428 origin.delayed_ack_timer = nil;
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 -- Remove handled stanzas from outgoing_stanza_queue
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
431 local h = tonumber(stanza.attr.h);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
432 if not h then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
433 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
434 return;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
435 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
436 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
437 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
438 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
439 if err then
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
440 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
441 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
442 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
443 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
444 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
445 end
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
446 origin:close(err);
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
447 return;
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
448 end
12125
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
449 tx_acked_stanzas:sample(handled_stanza_count);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
450
12125
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
451 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
452 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
453 return true;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
454 end
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
455 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
456 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
457
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
458 local function handle_unacked_stanzas(session)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
459 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
460 local unacked = queue:count_unacked()
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
461 if unacked > 0 then
12528
8e780079a424 mod_smacks: Indicate that bounces are generated by the server
Kim Alvefur <zash@zash.se>
parents: 12527
diff changeset
462 local error_from = jid.join(session.username, session.host or module.host);
12125
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
463 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
464 session.smacks = false; -- Disable queueing
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
465 session.outgoing_stanza_queue = nil;
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
466 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
467 if not module:fire_event("delivery/failure", { session = session, stanza = stanza }) then
12525
8087f5357f53 mod_smacks: Fix bounce of stanzas directed to full JID on unclean disconnect
Kim Alvefur <zash@zash.se>
parents: 12522
diff changeset
468 if stanza.attr.type ~= "error" and stanza.attr.from ~= session.full_jid then
12528
8e780079a424 mod_smacks: Indicate that bounces are generated by the server
Kim Alvefur <zash@zash.se>
parents: 12527
diff changeset
469 local reply = st.error_reply(stanza, "cancel", "recipient-unavailable", nil, error_from);
12526
252ed01896dd mod_smacks: Bounce unhandled stanzas from local origin (fix #1759)
Kim Alvefur <zash@zash.se>
parents: 12525
diff changeset
470 module:send(reply);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
471 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
472 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
473 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
474 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
475 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
476
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
477 -- 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
478 -- 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
479 -- 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
480 module:hook("delivery/failure", function(event)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
481 local session, stanza = event.session, event.stanza;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
482 -- Only deal with authenticated (c2s) sessions
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
483 if session.username then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
484 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
485 ( 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
486 -- 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
487 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
488 if mam_result ~= nil then
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
489 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
490 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
491 -- 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
492 -- 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
493 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
494 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
495 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
496 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
497 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
498 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
499 -- 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
500 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
501 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
502 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
503 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
504 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
505 end
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 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
508 end);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
509
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
510 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
511 local session = event.session;
12054
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
512 if not session.smacks then return end
12060
3099e03b10bd mod_smacks: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 12059
diff changeset
513 if not session.resumption_token then
3099e03b10bd mod_smacks: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 12059
diff changeset
514 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
515 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
516 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
517 handle_unacked_stanzas(session);
3099e03b10bd mod_smacks: Fix indentation
Kim Alvefur <zash@zash.se>
parents: 12059
diff changeset
518 end
12054
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
519 return
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
520 end
12072
ef0f174488af mod_smacks: Fix duplicate sleep behavior when replacing a session
Kim Alvefur <zash@zash.se>
parents: 12070
diff changeset
521 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
522
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
523 session.hibernating = os_time();
13022
a313b7260145 mod_smacks: Replace existing watchdog when starting hibernation
Kim Alvefur <zash@zash.se>
parents: 13021
diff changeset
524 if session.hibernating_watchdog then
a313b7260145 mod_smacks: Replace existing watchdog when starting hibernation
Kim Alvefur <zash@zash.se>
parents: 13021
diff changeset
525 session.log("debug", "Session already has a sleeping watchdog, replacing it");
a313b7260145 mod_smacks: Replace existing watchdog when starting hibernation
Kim Alvefur <zash@zash.se>
parents: 13021
diff changeset
526 session.hibernating_watchdog:cancel();
a313b7260145 mod_smacks: Replace existing watchdog when starting hibernation
Kim Alvefur <zash@zash.se>
parents: 13021
diff changeset
527 end
13021
14ca500f8621 mod_smacks: Fix stray watchdog closing sessions
Kim Alvefur <zash@zash.se>
parents: 12850
diff changeset
528 session.hibernating_watchdog = watchdog.new(resume_timeout, function(this_dog)
14ca500f8621 mod_smacks: Fix stray watchdog closing sessions
Kim Alvefur <zash@zash.se>
parents: 12850
diff changeset
529 if this_dog ~= session.hibernating_watchdog then
14ca500f8621 mod_smacks: Fix stray watchdog closing sessions
Kim Alvefur <zash@zash.se>
parents: 12850
diff changeset
530 -- This really shouldn't happen?
14ca500f8621 mod_smacks: Fix stray watchdog closing sessions
Kim Alvefur <zash@zash.se>
parents: 12850
diff changeset
531 session.log("debug", "Releasing a stray watchdog");
14ca500f8621 mod_smacks: Fix stray watchdog closing sessions
Kim Alvefur <zash@zash.se>
parents: 12850
diff changeset
532 return
14ca500f8621 mod_smacks: Fix stray watchdog closing sessions
Kim Alvefur <zash@zash.se>
parents: 12850
diff changeset
533 end
12054
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
534 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
535 if session.destroyed then
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
536 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
537 return
12061
31a7e0ac6928 mod_smacks: Skip hibernation logic if session was closed or replaced
Kim Alvefur <zash@zash.se>
parents: 12060
diff changeset
538 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
539 -- 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
540 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
541 return
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
542 end
12054
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
543
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
544 session.log("debug", "Destroying session for hibernating too long");
12518
73ee3855f970 mod_smacks: Factor out some convenience functions
Kim Alvefur <zash@zash.se>
parents: 12504
diff changeset
545 save_old_session(session);
12054
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
546 session.resumption_token = nil;
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
547 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
548 sessions_expired(1);
12054
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
549 end);
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
550 if session.conn then
12070
e662c172ea0f mod_smacks: Remove references to connection on hibernation
Kim Alvefur <zash@zash.se>
parents: 12069
diff changeset
551 local conn = session.conn;
e662c172ea0f mod_smacks: Remove references to connection on hibernation
Kim Alvefur <zash@zash.se>
parents: 12069
diff changeset
552 c2s_sessions[conn] = nil;
e662c172ea0f mod_smacks: Remove references to connection on hibernation
Kim Alvefur <zash@zash.se>
parents: 12069
diff changeset
553 session.conn = nil;
e662c172ea0f mod_smacks: Remove references to connection on hibernation
Kim Alvefur <zash@zash.se>
parents: 12069
diff changeset
554 conn:close();
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
555 end
12850
101c758428aa mod_smacks: Log something when hibernation starts
Kim Alvefur <zash@zash.se>
parents: 12796
diff changeset
556 session.log("debug", "Session going into hibernation (not being destroyed)")
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
557 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
558 return true; -- Postpone destruction for now
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
559 end);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
560
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
561 local function handle_s2s_destroyed(event)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
562 local session = event.session;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
563 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
564 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
565 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
566 if s2s_resend then
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
567 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
568 module:send(stanza);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
569 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
570 session.outgoing_stanza_queue = nil;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
571 else
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
572 handle_unacked_stanzas(session);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
573 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
574 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
575 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
576
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
577 module:hook("s2sout-destroyed", handle_s2s_destroyed);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
578 module:hook("s2sin-destroyed", handle_s2s_destroyed);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
579
12682
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
580 function do_resume(session, stanza)
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
581 if session.full_jid then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
582 session.log("warn", "Tried to resume after resource binding");
12688
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
583 return nil, enable_errors.new("already_bound");
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
584 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
585
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
586 local id = stanza.attr.previd;
12803
2e12290820e8 mod_smacks: Factor out resumption token table key generation
Kim Alvefur <zash@zash.se>
parents: 12802
diff changeset
587 local original_session = session_registry[registry_key(session, id)];
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
588 if not original_session then
12051
c32ef09ab452 mod_smacks: Persist old counter values to storage
Kim Alvefur <zash@zash.se>
parents: 12050
diff changeset
589 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
590 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
591 session.log("debug", "Tried to resume old expired session with id %s", id);
12518
73ee3855f970 mod_smacks: Factor out some convenience functions
Kim Alvefur <zash@zash.se>
parents: 12504
diff changeset
592 clear_old_session(session, id);
12125
649268c9f603 mod_smacks: sprinkle some metrics on it
Jonas Schäfer <jonas@wielicki.name>
parents: 12114
diff changeset
593 resumption_expired(1);
12688
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
594 return nil, enable_errors.new("expired", { h = old_session.h });
12054
0116fa57f05c mod_smacks: Set a watchdog to watch sleeping sessions
Kim Alvefur <zash@zash.se>
parents: 12053
diff changeset
595 end
12682
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
596 session.log("debug", "Tried to resume non-existent session with id %s", id);
12688
36ba170c4fd0 mod_smacks: Split enable handling to stages, to allow easier SASL2 integration
Matthew Wild <mwild1@gmail.com>
parents: 12682
diff changeset
597 return nil, enable_errors.new("unknown_session");
12682
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
598 end
12677
3b9771d496ed mod_smacks: Long overdue cleanup of resumption code, fixes some old TODOs
Matthew Wild <mwild1@gmail.com>
parents: 12550
diff changeset
599
12682
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
600 if original_session.hibernating_watchdog then
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
601 original_session.log("debug", "Letting the watchdog go");
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
602 original_session.hibernating_watchdog:cancel();
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
603 original_session.hibernating_watchdog = nil;
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
604 elseif session.hibernating then
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
605 original_session.log("error", "Hibernating session has no watchdog!")
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
606 end
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
607 -- zero age = was not hibernating yet
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
608 local age = 0;
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
609 if original_session.hibernating then
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
610 local now = os_time();
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
611 age = now - original_session.hibernating;
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
612 end
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
613
12682
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
614 session.log("debug", "mod_smacks resuming existing session %s...", original_session.id);
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
615
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
616 local queue = original_session.outgoing_stanza_queue;
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
617 local h = tonumber(stanza.attr.h);
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
618
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
619 original_session.log("debug", "Pre-resumption #queue = %d", queue:count_unacked())
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
620 local acked, err = ack_errors.coerce(queue:ack(h)); -- luacheck: ignore 211/acked
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
621
12682
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
622 if not err and not queue:resumable() then
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
623 err = ack_errors.new("overflow");
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
624 end
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
625
12682
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
626 if err then
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
627 session.log("debug", "Resumption failed: %s", err);
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
628 return nil, err;
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
629 end
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
630
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
631 -- Update original_session with the parameters (connection, etc.) from the new session
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
632 sessionmanager.update_session(original_session, session);
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
633
12682
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
634 return {
12719
268b1a9162a9 mod_smacks: Add type field to results so actions can be later distinguished
Matthew Wild <mwild1@gmail.com>
parents: 12691
diff changeset
635 type = "resumed";
12682
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
636 session = original_session;
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
637 id = id;
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
638 -- Return function to complete the resumption and resync unacked stanzas
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
639 -- This is two steps so we can support SASL2/ISR
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
640 finish = function ()
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
641 -- Ok, we need to re-send any stanzas that the client didn't see
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
642 -- ...they are what is now left in the outgoing stanza queue
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
643 -- We have to use the send of "session" because we don't want to add our resent stanzas
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
644 -- to the outgoing queue again
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
645
12682
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
646 original_session.log("debug", "resending all unacked stanzas that are still queued after resume, #queue = %d", queue:count_unacked());
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
647 for _, queued_stanza in queue:resume() do
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
648 original_session.send(queued_stanza);
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
649 end
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
650 original_session.log("debug", "all stanzas resent, enabling stream management on resumed stream, #queue = %d", queue:count_unacked());
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
651
12682
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
652 -- Add our own handlers to the resumed session (filters have been reset in the update)
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
653 wrap_session(original_session, true);
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
654
12682
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
655 -- Let everyone know that we are no longer hibernating
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
656 module:fire_event("smacks-hibernation-end", {origin = session, resumed = original_session, queue = queue:table()});
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
657 original_session.awaiting_ack = nil; -- Don't wait for acks from before the resumption
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
658 request_ack_now_if_needed(original_session, true, "handle_resume", nil);
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
659 resumption_age:sample(age);
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
660 end;
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
661 };
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
662 end
12056
e62025f949f9 mod_smacks: Limit queue memory consumption using new util
Kim Alvefur <zash@zash.se>
parents: 12054
diff changeset
663
12682
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
664 function handle_resume(session, stanza, xmlns_sm)
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
665 local resumed, err = do_resume(session, stanza);
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
666 if not resumed then
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
667 session.send(st.stanza("failed", { xmlns = xmlns_sm, h = format_h(err.context.h) })
12739
0dc80024fdd2 Backed out changeset 1bc2220cd6ec
Jonas Schäfer <jonas@wielicki.name>
parents: 12720
diff changeset
668 :tag(err.condition, { xmlns = xmlns_errors }));
12682
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
669 return true;
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
670 end
12677
3b9771d496ed mod_smacks: Long overdue cleanup of resumption code, fixes some old TODOs
Matthew Wild <mwild1@gmail.com>
parents: 12550
diff changeset
671
12682
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
672 session = resumed.session;
12677
3b9771d496ed mod_smacks: Long overdue cleanup of resumption code, fixes some old TODOs
Matthew Wild <mwild1@gmail.com>
parents: 12550
diff changeset
673
12682
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
674 -- Inform client of successful resumption
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
675 session.send(st.stanza("resumed", { xmlns = xmlns_sm,
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
676 h = format_h(session.handled_stanza_count), previd = resumed.id }));
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
677
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
678 -- Complete resume (sync stanzas, etc.)
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
679 resumed.finish();
464a22f2751c mod_smacks: Split resumption into multiple stages, to simplify ISR integration
Matthew Wild <mwild1@gmail.com>
parents: 12678
diff changeset
680
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
681 return true;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
682 end
12677
3b9771d496ed mod_smacks: Long overdue cleanup of resumption code, fixes some old TODOs
Matthew Wild <mwild1@gmail.com>
parents: 12550
diff changeset
683
11935
4d0d10fabb82 mod_smacks: Clean up compat code etc
Kim Alvefur <zash@zash.se>
parents: 11934
diff changeset
684 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
685 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
686
11937
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
687 -- 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
688 -- 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
689 local request_ack_events = {
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
690 ["csi-client-active"] = true;
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
691 ["csi-flushing"] = false;
11938
6da703cb4c04 mod_smacks: Optimize scheduling of ack requests
Kim Alvefur <zash@zash.se>
parents: 11937
diff changeset
692 ["c2s-pre-ondrain"] = false;
6da703cb4c04 mod_smacks: Optimize scheduling of ack requests
Kim Alvefur <zash@zash.se>
parents: 11937
diff changeset
693 ["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
694 };
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
695
11937
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
696 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
697 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
698 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
699 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
700 end);
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
701 end
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
702
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
703 local function handle_read_timeout(event)
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
704 local session = event.session;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
705 if session.smacks then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
706 if session.awaiting_ack then
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
707 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
708 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
709 session.awaiting_ack_timer = nil;
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
710 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
711 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
712 timer.stop(session.delayed_ack_timer);
11934
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
713 session.delayed_ack_timer = nil;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
714 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
715 return false; -- Kick the session
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
716 end
11937
364c3f018e3a mod_smacks: Refactor ack requesting to avoid some timer roundtrips
Kim Alvefur <zash@zash.se>
parents: 11936
diff changeset
717 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
718 return true;
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
719 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
720 end
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
721
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
722 module:hook("s2s-read-timeout", handle_read_timeout);
65cdb1b21db3 mod_smacks: Import from prosody-modules @ eb63890ae8fc
Kim Alvefur <zash@zash.se>
parents:
diff changeset
723 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
724
12065
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
725 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
726 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
727 -- 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
728 return
061d516d8ac5 mod_smacks: Fix error on shutdown if loaded on Component
Kim Alvefur <zash@zash.se>
parents: 12076
diff changeset
729 end
12065
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
730 local reason = event.reason;
12078
2c13aaae35ac mod_smacks: Fix typos
Kim Alvefur <zash@zash.se>
parents: 12077
diff changeset
731 -- 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
732 -- 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
733 -- 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
734 -- resume the lost session after a restart.
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
735 for _, user in pairs(local_sessions) do
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
736 for _, session in pairs(user.sessions) do
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
737 if session.resumption_token then
12518
73ee3855f970 mod_smacks: Factor out some convenience functions
Kim Alvefur <zash@zash.se>
parents: 12504
diff changeset
738 if save_old_session(session) then
12064
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
739 session.resumption_token = nil;
12065
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
740
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
741 -- Deal with unacked stanzas
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
742 if session.outgoing_stanza_queue then
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
743 handle_unacked_stanzas(session);
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
744 end
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
745
12064
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
746 if session.conn then
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
747 session.conn:close()
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
748 session.conn = nil;
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
749 -- 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
750 -- session since it is unconnected.
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
751 end
12065
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
752
9102cbd2aec4 mod_smacks: Bounce unacked stanzas on shutdown
Kim Alvefur <zash@zash.se>
parents: 12064
diff changeset
753 -- 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
754 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
755 end
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
756 end
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
757 end
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
758 end
d2380fd5e421 mod_smacks: Preserve counter values on shutdown
Kim Alvefur <zash@zash.se>
parents: 12063
diff changeset
759 end, -90);