From 3cc5b5cea30842c16979a8f7c80fadeb54b80aa8 Mon Sep 17 00:00:00 2001 From: nexy7574 Date: Mon, 19 Aug 2024 18:48:36 +0100 Subject: [PATCH] Include federation latency --- app/modules/latency.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/modules/latency.py b/app/modules/latency.py index 85097ef..9b5d49f 100644 --- a/app/modules/latency.py +++ b/app/modules/latency.py @@ -1,4 +1,6 @@ import niobot +import time +import httpx class LatencyModule(niobot.Module): @@ -6,4 +8,17 @@ class LatencyModule(niobot.Module): async def latency(self, ctx: niobot.Context): """See the bot's latency.""" latency = ctx.latency - return await ctx.respond("Latency: {:,.2f}ms".format(latency)) + + homeserver = await niobot.resolve_homeserver(ctx.message.sender.split(":")[1]) + async with httpx.AsyncClient(headers={"User-Agent": niobot.__user_agent__}) as client: + timings = [] + for rnd in range(5): + start = time.time() + await client.get(f"{homeserver}/_matrix/client/v1/ping") + end = time.time() + timings.append(end - start) + fed_latency = sum(timings) / len(timings) * 1000 + + return await ctx.respond( + "Latency: {:,.2f}ms (federation latency to your homeserver: {:,.2f})".format(latency, fed_latency) + )