2 |
2 |
3 local t_insert, t_remove = table.insert, table.remove; |
3 local t_insert, t_remove = table.insert, table.remove; |
4 local math_min = math.min; |
4 local math_min = math.min; |
5 local tonumber, tostring = tonumber, tostring; |
5 local tonumber, tostring = tonumber, tostring; |
6 local add_filter = require "util.filters".add_filter; |
6 local add_filter = require "util.filters".add_filter; |
|
7 local timer = require "util.timer"; |
7 |
8 |
8 local xmlns_sm = "urn:xmpp:sm:2"; |
9 local xmlns_sm = "urn:xmpp:sm:2"; |
9 |
10 |
10 local sm_attr = { xmlns = xmlns_sm }; |
11 local sm_attr = { xmlns = xmlns_sm }; |
11 |
12 |
|
13 local resume_timeout = 300; |
12 local max_unacked_stanzas = 0; |
14 local max_unacked_stanzas = 0; |
13 |
15 |
14 module:add_event_hook("stream-features", |
16 module:add_event_hook("stream-features", |
15 function (session, features) |
17 function (session, features) |
16 features:tag("sm", sm_attr):tag("optional"):up():up(); |
18 features:tag("sm", sm_attr):tag("optional"):up():up(); |
119 end |
121 end |
120 |
122 |
121 local _destroy_session = sessionmanager.destroy_session; |
123 local _destroy_session = sessionmanager.destroy_session; |
122 function sessionmanager.destroy_session(session, err) |
124 function sessionmanager.destroy_session(session, err) |
123 if session.smacks then |
125 if session.smacks then |
124 local queue = session.outgoing_stanza_queue; |
126 if not session.resumption_token then |
125 if #queue > 0 then |
127 local queue = session.outgoing_stanza_queue; |
126 module:log("warn", "Destroying session with %d unacked stanzas:", #queue); |
128 if #queue > 0 then |
127 for i=1,#queue do |
129 module:log("warn", "Destroying session with %d unacked stanzas:", #queue); |
128 module:log("warn", "::%s", tostring(queue[i])); |
130 for i=1,#queue do |
|
131 module:log("warn", "::%s", tostring(queue[i])); |
|
132 end |
|
133 handle_unacked_stanzas(session); |
129 end |
134 end |
130 handle_unacked_stanzas(session); |
135 else |
|
136 session.hibernating = true; |
|
137 timer.add_task(resume_timeout, function () |
|
138 if session.hibernating then |
|
139 session.resumption_token = nil; |
|
140 sessionmanager.destroy_session(session); -- Re-destroy |
|
141 end |
|
142 end); |
|
143 return; -- Postpone destruction for now |
131 end |
144 end |
132 end |
145 end |
133 return _destroy_session(session, err); |
146 return _destroy_session(session, err); |
134 end |
147 end |