From 033e88ac4300109b5517d6c3597fc61fa06d0763 Mon Sep 17 00:00:00 2001 From: jiftechnify Date: Tue, 1 Oct 2024 23:09:43 +0900 Subject: [PATCH] Revert "fix: respect NIP-01's note on reconnections to relays" This reverts commit d0045600837a432d31a5143654086d401ca56591. --- packages/nostr-fetch/src/relayPool.ts | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/packages/nostr-fetch/src/relayPool.ts b/packages/nostr-fetch/src/relayPool.ts index 8f794a4..ac59ab1 100644 --- a/packages/nostr-fetch/src/relayPool.ts +++ b/packages/nostr-fetch/src/relayPool.ts @@ -10,10 +10,6 @@ import { } from "@nostr-fetch/kernel/utils"; import { WebSocketReadyState } from "@nostr-fetch/kernel/webSocket"; -// [NIP-01](https://github.com/nostr-protocol/nips/blob/master/01.md#other-notes) says: -// > When a websocket is closed by the relay with a status code 4000 that means the client shouldn't try to connect again. -const CLOSE_CODE_RELAY_NOT_RECONNECTABLE = 4000; - export interface RelayPool { ensureRelays(relayUrls: string[], relayOpts: RelayOptions): Promise; ensureSingleRelay(relayUrl: string, relayOpts: RelayOptions): Promise; @@ -46,7 +42,6 @@ type ConnectFailedRelay = { type DisconnectedRelay = { state: "disconnected"; relayUrl: string; - reconnectable: boolean; }; type ManagedRelay = AliveRelay | ConnectingRelay | ConnectFailedRelay | DisconnectedRelay; @@ -78,8 +73,9 @@ class RelayPoolImpl implements RelayPool { #relayShouldBeReconnected(relay: ManagedRelay): boolean { return ( + // TODO: make the threshold configuarable (relay.state === "connectFailed" && currUnixtimeMilli() - relay.failedAt > 30 * 1000) || - (relay.state === "disconnected" && relay.reconnectable) || + relay.state === "disconnected" || (relay.state === "alive" && relay.relay.wsReadyState === WebSocketReadyState.CLOSED) // is it possible? ); } @@ -110,19 +106,11 @@ class RelayPoolImpl implements RelayPool { r.on("connect", () => logger?.log("info", "connected")); r.on("disconnect", (ev) => { logger?.log("info", `disconnected: ${JSON.stringify(ev)}`); - this.#relays.set(r.url, { - state: "disconnected", - relayUrl: r.url, - reconnectable: ev.code !== CLOSE_CODE_RELAY_NOT_RECONNECTABLE, - }); + this.#relays.set(r.url, { state: "disconnected", relayUrl: r.url }); }); r.on("error", () => { logger?.log("error", "WebSocket error"); - this.#relays.set(r.url, { - state: "disconnected", - relayUrl: r.url, - reconnectable: true, - }); + this.#relays.set(r.url, { state: "disconnected", relayUrl: r.url }); }); r.on("notice", (notice) => logger?.log("warn", `NOTICE: ${notice}`));