comparison mod_s2s_keepalive/mod_s2s_keepalive.lua @ 4203:c4002aae4ad3

mod_s2s_keepalive: Use timestamp as iq @id RFC 6120 implies that the id attribute must be unique within a stream. This should fix problems with remote servers that enforce uniqueness and don't answer duplicated ids. If it doesn't do that, then at least you can get a guesstimate at round-trip time from the difference between the result iq stanza and the timestamp it was logged without having to go look for when it was sent, or needing to keep state.
author Kim Alvefur <zash@zash.se>
date Wed, 14 Oct 2020 18:02:10 +0200
parents 580862decd77
children a5930a185806
comparison
equal deleted inserted replaced
4202:af93644dd5de 4203:c4002aae4ad3
1 local st = require "util.stanza"; 1 local st = require "util.stanza";
2 local watchdog = require "util.watchdog"; 2 local watchdog = require "util.watchdog";
3 local dt = require "util.datetime";
3 4
4 local keepalive_servers = module:get_option_set("keepalive_servers"); 5 local keepalive_servers = module:get_option_set("keepalive_servers");
5 local keepalive_interval = module:get_option_number("keepalive_interval", 60); 6 local keepalive_interval = module:get_option_number("keepalive_interval", 60);
6 local keepalive_timeout = module:get_option_number("keepalive_timeout", 593); 7 local keepalive_timeout = module:get_option_number("keepalive_timeout", 593);
7 8
12 local ping_hosts = {}; 13 local ping_hosts = {};
13 14
14 for remote_domain, session in pairs(s2sout) do 15 for remote_domain, session in pairs(s2sout) do
15 if session.type ~= "s2sout_unauthed" 16 if session.type ~= "s2sout_unauthed"
16 and (not(keepalive_servers) or keepalive_servers:contains(remote_domain)) then 17 and (not(keepalive_servers) or keepalive_servers:contains(remote_domain)) then
17 session.sends2s(st.iq({ to = remote_domain, type = "get", from = host, id = "keepalive" }) 18 session.sends2s(st.iq({ to = remote_domain, type = "get", from = host, id = "keepalive:"..dt.timestamp()})
18 :tag("ping", { xmlns = "urn:xmpp:ping" }) 19 :tag("ping", { xmlns = "urn:xmpp:ping" })
19 ); 20 );
20 end 21 end
21 end 22 end
22 23
30 end 31 end
31 end 32 end
32 33
33 -- ping remotes we only have s2sin from 34 -- ping remotes we only have s2sin from
34 for remote_domain in pairs(ping_hosts) do 35 for remote_domain in pairs(ping_hosts) do
35 module:send(st.iq({ to = remote_domain, type = "get", from = host, id = "keepalive" }) 36 module:send(st.iq({ to = remote_domain, type = "get", from = host, id = "keepalive:"..dt.timestamp() })
36 :tag("ping", { xmlns = "urn:xmpp:ping" }) 37 :tag("ping", { xmlns = "urn:xmpp:ping" })
37 ); 38 );
38 end 39 end
39 40
40 return keepalive_interval; 41 return keepalive_interval;
58 session.log("info", "Keepalive ping timed out, closing connection"); 59 session.log("info", "Keepalive ping timed out, closing connection");
59 session:close("connection-timeout"); 60 session:close("connection-timeout");
60 end); 61 end);
61 end); 62 end);
62 63
63 module:hook("iq-result/host/keepalive", function (event) 64 module:hook("iq-result/host", function (event)
65 local stanza = event.stanza;
66 if not (stanza.attr.id and stanza.attr.id:sub(1, #"keepalive:") == "keepalive:") then
67 return -- not a reply to this module
68 end
69
64 local origin = event.origin; 70 local origin = event.origin;
65 if origin.watchdog_keepalive then 71 if origin.watchdog_keepalive then
66 origin.watchdog_keepalive:reset(); 72 origin.watchdog_keepalive:reset();
67 end 73 end
68 if s2sout[origin.from_host] and s2sout[origin.from_host].watchdog_keepalive then 74 if s2sout[origin.from_host] and s2sout[origin.from_host].watchdog_keepalive then
69 s2sout[origin.from_host].watchdog_keepalive:reset(); 75 s2sout[origin.from_host].watchdog_keepalive:reset();
70 end 76 end
71 return true; 77 return true;
72 end); 78 end);
73 79
74 module:hook("iq-error/host/keepalive", function (event) 80 module:hook("iq-error/host", function (event)
75 local origin = event.origin; 81 local origin = event.origin;
76 if origin.dummy then return end -- Probably a sendq bounce 82 if origin.dummy then return end -- Probably a sendq bounce
83
84 local stanza = event.stanza;
85 if not (stanza.attr.id and stanza.attr.id:sub(1, #"keepalive:") == "keepalive:") then
86 return -- not a reply to this module
87 end
77 88
78 if origin.type == "s2sin" or origin.type == "s2sout" then 89 if origin.type == "s2sin" or origin.type == "s2sout" then
79 -- An error from the remote means connectivity is ok, 90 -- An error from the remote means connectivity is ok,
80 -- so treat it the same as a result 91 -- so treat it the same as a result
81 return module:fire_event("iq-result/host/keepalive", event); 92 return module:fire_event("iq-result/host", event);
82 end 93 end
83 end); 94 end);
84 95
85 module:add_timer(keepalive_interval, send_pings); 96 module:add_timer(keepalive_interval, send_pings);