From c02cd9c30057242479589644947b4462fa6968b8 Mon Sep 17 00:00:00 2001 From: Kieran Date: Sat, 17 Jun 2023 17:38:36 +0100 Subject: [PATCH] dont re-connect on close code 4000 --- packages/system/src/Connection.ts | 49 ++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/packages/system/src/Connection.ts b/packages/system/src/Connection.ts index 8a096ad3..c0585c88 100644 --- a/packages/system/src/Connection.ts +++ b/packages/system/src/Connection.ts @@ -1,4 +1,5 @@ import { v4 as uuid } from "uuid"; +import debug from "debug"; import { unwrap, ExternalStore } from "@snort/shared"; import { DefaultConnectTimeout } from "./Const"; @@ -37,6 +38,7 @@ export interface ConnectionStateSnapshot { } export class Connection extends ExternalStore { + #log = debug("Connection"); Id: string; Address: string; Socket: WebSocket | null = null; @@ -54,7 +56,7 @@ export class Connection extends ExternalStore { Stats: ConnectionStats = new ConnectionStats(); HasStateChange: boolean = true; IsClosed: boolean; - ReconnectTimer: ReturnType | null; + ReconnectTimer?: ReturnType; EventsCallback: Map void>; OnConnected?: () => void; OnEvent?: (sub: string, e: TaggedRawEvent) => void; @@ -64,7 +66,7 @@ export class Connection extends ExternalStore { AwaitingAuth: Map; Authed = false; Ephemeral: boolean; - EphemeralTimeout: ReturnType | undefined; + EphemeralTimeout?: ReturnType; Down = true; constructor(addr: string, options: RelaySettings, auth?: AuthHandler, ephemeral: boolean = false) { @@ -73,7 +75,6 @@ export class Connection extends ExternalStore { this.Address = addr; this.Settings = options; this.IsClosed = false; - this.ReconnectTimer = null; this.EventsCallback = new Map(); this.AwaitingAuth = new Map(); this.Auth = auth; @@ -111,7 +112,7 @@ export class Connection extends ExternalStore { } } } catch (e) { - console.warn("Could not load relay information", e); + this.#log("Could not load relay information %O", e); } if (this.Socket) { @@ -133,7 +134,7 @@ export class Connection extends ExternalStore { this.IsClosed = true; if (this.ReconnectTimer !== null) { clearTimeout(this.ReconnectTimer); - this.ReconnectTimer = null; + this.ReconnectTimer = undefined; } this.Socket?.close(); this.notifyChange(); @@ -141,7 +142,7 @@ export class Connection extends ExternalStore { OnOpen() { this.ConnectTimeout = DefaultConnectTimeout; - console.log(`[${this.Address}] Open!`); + this.#log(`[${this.Address}] Open!`); this.Down = false; if (this.Ephemeral) { this.ResetEphemeralTimeout(); @@ -151,9 +152,22 @@ export class Connection extends ExternalStore { } OnClose(e: CloseEvent) { - if (!this.IsClosed) { + if (this.EphemeralTimeout) { + clearTimeout(this.EphemeralTimeout); + this.EphemeralTimeout = undefined; + } + if (this.ReconnectTimer) { + clearTimeout(this.ReconnectTimer); + this.ReconnectTimer = undefined; + } + + // remote server closed the connection, dont re-connect + if (e.code === 4000) { + this.IsClosed = true; + this.#log(`[${this.Address}] Closed! (Remote)`); + } else if (!this.IsClosed) { this.ConnectTimeout = this.ConnectTimeout * 2; - console.log( + this.#log( `[${this.Address}] Closed (${e.reason}), trying again in ${(this.ConnectTimeout / 1000) .toFixed(0) .toLocaleString()} sec` @@ -163,8 +177,8 @@ export class Connection extends ExternalStore { }, this.ConnectTimeout); this.Stats.Disconnects++; } else { - console.log(`[${this.Address}] Closed!`); - this.ReconnectTimer = null; + this.#log(`[${this.Address}] Closed!`); + this.ReconnectTimer = undefined; } this.OnDisconnect?.(this.Id); @@ -182,7 +196,7 @@ export class Connection extends ExternalStore { case "AUTH": { this._OnAuthAsync(msg[1]) .then(() => this.#sendPendingRaw()) - .catch(console.error); + .catch(this.#log); this.Stats.EventsReceived++; this.notifyChange(); break; @@ -202,7 +216,7 @@ export class Connection extends ExternalStore { } case "OK": { // feedback to broadcast call - console.debug(`${this.Address} OK: `, msg); + this.#log(`${this.Address} OK: %O`, msg); const id = msg[1]; if (this.EventsCallback.has(id)) { const cb = unwrap(this.EventsCallback.get(id)); @@ -212,11 +226,11 @@ export class Connection extends ExternalStore { break; } case "NOTICE": { - console.warn(`[${this.Address}] NOTICE: ${msg[1]}`); + this.#log(`[${this.Address}] NOTICE: ${msg[1]}`); break; } default: { - console.warn(`Unknown tag: ${tag}`); + this.#log(`Unknown tag: ${tag}`); break; } } @@ -224,7 +238,7 @@ export class Connection extends ExternalStore { } OnError(e: Event) { - console.error(e); + this.#log("Error: %O", e); this.notifyChange(); } @@ -282,12 +296,13 @@ export class Connection extends ExternalStore { cmd, cb: cbSent, }); - console.debug("Queuing:", this.Address, cmd); + this.#log("Queuing: %s %O", this.Address, cmd); } else { this.ActiveRequests.add(cmd[1]); this.#SendJson(cmd); cbSent(); } + this.ResetEphemeralTimeout(); this.notifyChange(); } @@ -330,7 +345,7 @@ export class Connection extends ExternalStore { this.ActiveRequests.add(p.cmd[1]); this.#SendJson(p.cmd); p.cb(); - console.debug("Sent pending REQ", this.Address, p.cmd); + this.#log("Sent pending REQ %s %O", this.Address, p.cmd); } } }