From 4a54b100b78c778b2612ec5331fe4d22e183c6c0 Mon Sep 17 00:00:00 2001 From: execaman <151807496+execaman@users.noreply.github.com> Date: Thu, 13 Nov 2025 07:27:54 +0530 Subject: [PATCH] feat(Node): immediate connect attempt before reconnect cycle --- src/Node/Node.ts | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Node/Node.ts b/src/Node/Node.ts index cfc8357..1f11bb7 100644 --- a/src/Node/Node.ts +++ b/src/Node/Node.ts @@ -2,7 +2,7 @@ declare const $clientName: string; declare const $clientVersion: string; import { EventEmitter, once } from "node:events"; -import { setTimeout, clearTimeout } from "node:timers"; +import { clearImmediate, clearTimeout, setImmediate, setTimeout } from "node:timers"; import { WebSocket } from "ws"; import { CloseCodes, OPType } from "../Typings"; import { DefaultNodeOptions, DefaultRestOptions, Routes, SnowflakeRegex } from "../Constants"; @@ -349,16 +349,24 @@ export class Node extends EventEmitter { #onClose(code: number, reason: string) { this.#cleanup(); this.rest.dropSessionRequests(`Connection to node '${this.name}' closed`); - if (!this.#manualDisconnect && this.#reconnectAttempts < this.#reconnectLimit) { + if (this.#manualDisconnect || this.#reconnectAttempts === this.#reconnectLimit) { + this.#stopReconnecting(); + delete this.#socketConfig.headers["Session-Id"]; + const byLocal = this.#manualDisconnect; + this.#manualDisconnect = false; + this.emit("disconnect", code, reason, byLocal, this.name); + return; + } + if (this.#reconnectInit) { this.#reconnect(); this.emit("close", code, reason, this.name); return; } - this.#stopReconnecting(); - delete this.#socketConfig.headers["Session-Id"]; - const byLocal = this.#manualDisconnect; - this.#manualDisconnect = false; - this.emit("disconnect", code, reason, byLocal, this.name); + const immediate = setImmediate(() => { + clearImmediate(immediate); + this.#reconnectInit = true; + this.connect(); + }); } override toString() {