Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove special treatment for WebSocket close status code 4000 #327

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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