mod_admin_shell: Print s2s related events while waiting for ping
authorKim Alvefur <zash@zash.se>
Sun, 26 Dec 2021 16:26:36 +0100
changeset 12126 50795249b7be
parent 12125 2162e86029b6
child 12127 7ba686696250
mod_admin_shell: Print s2s related events while waiting for ping Gives a better idea of what's taking time.
plugins/mod_admin_shell.lua
--- a/plugins/mod_admin_shell.lua	Sun Dec 26 20:37:20 2021 +0000
+++ b/plugins/mod_admin_shell.lua	Sun Dec 26 16:26:36 2021 +0100
@@ -1387,7 +1387,34 @@
 	local iq = st.iq{ from=localhost, to=remotehost, type="get", id=new_id()}
 			:tag("ping", {xmlns="urn:xmpp:ping"});
 	local time_start = time.now();
-	return module:context(localhost):send_iq(iq, nil, timeout):next(function (pong)
+	local print = self.session.print;
+	local function onchange(what)
+		return function(event)
+			local s2s_session = event.session;
+			if (s2s_session.from_host == localhost and s2s_session.to_host == remotehost)
+				or (s2s_session.to_host == localhost and s2s_session.from_host == remotehost) then
+				local dir = available_columns.dir.mapper(s2s_session.direction, s2s_session);
+				print(("Session %s (%s%s%s) %s (%gs)"):format(s2s_session.id, localhost, dir, remotehost, what,
+					time.now() - time_start));
+			end
+		end
+	end
+	local oncreated = onchange("created");
+	local onauthenticated = onchange("authenticated");
+	local onestablished = onchange("established");
+	local ondestroyed = onchange("destroyed");
+	module:hook("s2s-created", oncreated, 1);
+	module:context(localhost):hook("s2s-authenticated", onauthenticated, 1);
+	module:hook("s2sout-established", onestablished, 1);
+	module:hook("s2sin-established", onestablished, 1);
+	module:hook("s2s-destroyed", ondestroyed, 1);
+	return module:context(localhost):send_iq(iq, nil, timeout):finally(function()
+		module:unhook("s2s-created", oncreated);
+		module:context(localhost):unhook("s2s-authenticated", onauthenticated);
+		module:unhook("s2sout-established", onestablished);
+		module:unhook("s2sin-established", onestablished);
+		module:unhook("s2s-destroyed", ondestroyed);
+	end):next(function(pong)
 		return ("pong from %s in %gs"):format(pong.stanza.attr.from, time.now() - time_start);
 	end);
 end