Skip to content

Commit

Permalink
Revert "fix: respect NIP-01's note on reconnections to relays"
Browse files Browse the repository at this point in the history
This reverts commit d004560.
  • Loading branch information
jiftechnify committed Oct 1, 2024
1 parent 652f119 commit 033e88a
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions packages/nostr-fetch/src/relayPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>;
ensureSingleRelay(relayUrl: string, relayOpts: RelayOptions): Promise<Relay | undefined>;
Expand Down Expand Up @@ -46,7 +42,6 @@ type ConnectFailedRelay = {
type DisconnectedRelay = {
state: "disconnected";
relayUrl: string;
reconnectable: boolean;
};
type ManagedRelay = AliveRelay | ConnectingRelay | ConnectFailedRelay | DisconnectedRelay;

Expand Down Expand Up @@ -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?
);
}
Expand Down Expand Up @@ -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}`));

Expand Down

0 comments on commit 033e88a

Please sign in to comment.