comparison mod_ping_muc/mod_ping_muc.lua @ 5951:d6a695abb33c

mod_ping_muc: Delay ping a configurable amount of time If a server is restarting, checking immediately before it has a chance to complete its restart and get ready would often fail, preventing the possibility of transparent restarts as supported by Prosody's mod_muc. Reconnecting immediately when a connection is closed for being idle, or because the remote server is trying to reclaim some resources, is also counter-productive as the connection may fail. Also, if there is some Internet routing problem affecting s2s, it may help to wait a bit before checking, in case the problem resolved itself in the mean time.
author Kim Alvefur <zash@zash.se>
date Sun, 11 Aug 2024 16:10:24 +0200
parents 0772facc786f
children
comparison
equal deleted inserted replaced
5950:f89367e421d1 5951:d6a695abb33c
26 if s2s_session.hosts then 26 if s2s_session.hosts then
27 -- While rarely used, multiplexing is still supported 27 -- While rarely used, multiplexing is still supported
28 for host, state in pairs(s2s_session.hosts) do if state.authed then related_hosts:add(host); end end 28 for host, state in pairs(s2s_session.hosts) do if state.authed then related_hosts:add(host); end end
29 end 29 end
30 30
31 for _, user_session in pairs(local_sessions) do 31 local ping_delay = module:get_option_number("ping_muc_delay", 60, 1);
32 for _, session in pairs(user_session.sessions) do
33 if session.rooms_joined then
34 for room, info in pairs(session.rooms_joined) do
35 local nick = info.nick or info;
36 local room_nick = room .. "/" .. nick;
37 if related_hosts:contains(jid.host(room)) then
38 -- User is in a MUC room for which the s2s connection was lost. Now what?
39 32
40 -- Self-ping 33 module:add_timer(ping_delay, function ()
41 -- ========= 34 for _, user_session in pairs(local_sessions) do
42 -- 35 for _, session in pairs(user_session.sessions) do
43 -- Response of <iq type=result> means the user is still in the room 36 if session.rooms_joined then
44 -- (and self-ping is supported), so we do nothing. 37 for room, info in pairs(session.rooms_joined) do
45 -- 38 local nick = info.nick or info;
46 -- An error reply either means the user has fallen out of the room, 39 local room_nick = room .. "/" .. nick;
47 -- or that self-ping is unsupported. In the later case, whether the 40 if related_hosts:contains(jid.host(room)) then
48 -- user is still joined is indeterminate and we might as well 41 -- User is in a MUC room for which the s2s connection was lost. Now what?
49 -- pretend they fell out. 42
50 module:send_iq(st.iq({ type = "get"; id = id.medium(); from = session.full_jid; to = room_nick }) 43 -- Self-ping
51 :tag("ping", { xmlns = "urn:xmpp:ping"; })) 44 -- =========
52 :catch(function(err) 45 --
53 module:send( 46 -- Response of <iq type=result> means the user is still in the room
54 st.presence({ type = "unavailable"; id = id.medium(); to = session.full_jid; from = room_nick }) 47 -- (and self-ping is supported), so we do nothing.
55 :tag("x", { xmlns = "http://jabber.org/protocol/muc#user" }) 48 --
56 :tag("item", { affiliation = "none"; role = "none" }) 49 -- An error reply either means the user has fallen out of the room,
57 :text_tag("reason", err.text or "Connection to remote server lost") 50 -- or that self-ping is unsupported. In the later case, whether the
58 :up() 51 -- user is still joined is indeterminate and we might as well
59 :tag("status", { code = "110" }):up() 52 -- pretend they fell out.
60 :tag("status", { code = "333" }):up() 53 module:send_iq(st.iq({ type = "get"; id = id.medium(); from = session.full_jid; to = room_nick })
61 :reset()); 54 :tag("ping", { xmlns = "urn:xmpp:ping"; }))
62 end); 55 :catch(function(err)
63 -- TODO do this with some delay? 56 module:send(
57 st.presence({ type = "unavailable"; id = id.medium(); to = session.full_jid; from = room_nick })
58 :tag("x", { xmlns = "http://jabber.org/protocol/muc#user" })
59 :tag("item", { affiliation = "none"; role = "none" })
60 :text_tag("reason", err.text or "Connection to remote server lost")
61 :up()
62 :tag("status", { code = "110" }):up()
63 :tag("status", { code = "333" }):up()
64 :reset());
65 end);
66 end
64 end 67 end
65 end 68 end
66
67 end 69 end
68 end 70 end
69 end 71 end)
70 end); 72 end);