From 5a0a2087e55698c86c476c68627e2c8032086a9f Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 21 Sep 2024 14:48:50 -0500 Subject: [PATCH] relay: fix connection metrics --- src/controllers/nostr/relay.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/controllers/nostr/relay.ts b/src/controllers/nostr/relay.ts index 9f47b382..b2bb8dba 100644 --- a/src/controllers/nostr/relay.ts +++ b/src/controllers/nostr/relay.ts @@ -26,14 +26,16 @@ const LIMITER_LIMIT = 300; const limiter = new TTLCache(); +/** Connections for metrics purposes. */ +const connections = new Set(); + /** Set up the Websocket connection. */ function connectStream(socket: WebSocket, ip: string | undefined) { - let opened = false; const controllers = new Map(); socket.onopen = () => { - opened = true; - relayConnectionsGauge.inc(); + connections.add(socket); + relayConnectionsGauge.set(connections.size); }; socket.onmessage = (e) => { @@ -63,9 +65,8 @@ function connectStream(socket: WebSocket, ip: string | undefined) { }; socket.onclose = () => { - if (opened) { - relayConnectionsGauge.dec(); - } + connections.delete(socket); + relayConnectionsGauge.set(connections.size); for (const controller of controllers.values()) { controller.abort();