Skip to content

Commit 0e3ccf2

Browse files
committed
feat: implement reconnection state management in LavalinkNode
1 parent 1c66357 commit 0e3ccf2

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/structures/Node.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type { NodeManager } from "./NodeManager";
2020
import type {
2121
BaseNodeStats, LavalinkInfo, LavalinkNodeOptions, LyricsResult, ModifyRequest, NodeStats, SponsorBlockSegment
2222
} from "./Types/Node";
23+
import { ReconnectionState } from "./Types/Node";
2324
/**
2425
* Lavalink Node creator class
2526
*/
@@ -70,6 +71,8 @@ export class LavalinkNode {
7071
private reconnectTimeout?: NodeJS.Timeout = undefined;
7172
/** The Reconnection Attempt counter */
7273
private reconnectAttempts = 1;
74+
/** Reconnection current state */
75+
private reconnectionState: ReconnectionState = ReconnectionState.IDLE;
7376
/** The Socket of the Lavalink */
7477
private socket: WebSocket | null = null;
7578
/** Version of what the Lavalink Server should be */
@@ -1025,16 +1028,25 @@ export class LavalinkNode {
10251028
* ```
10261029
*/
10271030
private reconnect(instaReconnect = false): void {
1031+
// If already trying to reconnect or pending, return
1032+
if (this.reconnectionState != ReconnectionState.IDLE) {
1033+
return;
1034+
}
1035+
1036+
// Set reconnection state to pending
1037+
this.reconnectionState = ReconnectionState.PENDING;
10281038
this.NodeManager.emit("reconnectinprogress", this);
10291039
if (instaReconnect) {
10301040
if (this.reconnectAttempts >= this.options.retryAmount) {
10311041
const error = new Error(`Unable to connect after ${this.options.retryAmount} attempts.`)
10321042

10331043
this.NodeManager.emit("error", this, error);
1044+
this.reconnectionState = ReconnectionState.IDLE;
10341045
return this.destroy(DestroyReasons.NodeReconnectFail);
10351046
}
10361047

10371048
this.NodeManager.emit("reconnecting", this);
1049+
this.reconnectionState = ReconnectionState.RECONNECTING;
10381050
this.connect();
10391051
this.reconnectAttempts++;
10401052
return;
@@ -1044,10 +1056,12 @@ export class LavalinkNode {
10441056
if (this.reconnectAttempts >= this.options.retryAmount) {
10451057
const error = new Error(`Unable to connect after ${this.options.retryAmount} attempts.`)
10461058
this.NodeManager.emit("error", this, error);
1059+
this.reconnectionState = ReconnectionState.IDLE;
10471060
return this.destroy(DestroyReasons.NodeReconnectFail);
10481061
}
10491062

10501063
this.NodeManager.emit("reconnecting", this);
1064+
this.reconnectionState = ReconnectionState.RECONNECTING;
10511065
this.connect();
10521066
this.reconnectAttempts++;
10531067
}, this.options.retryDelay || 1000);
@@ -1057,6 +1071,9 @@ export class LavalinkNode {
10571071
private async open(): Promise<void> {
10581072
this.isAlive = true;
10591073

1074+
// Reset reconnection state on successful connection
1075+
this.reconnectionState = ReconnectionState.IDLE;
1076+
10601077
this.reconnectAttempts = 1;
10611078
if (this.reconnectTimeout) clearTimeout(this.reconnectTimeout);
10621079

@@ -1122,7 +1139,10 @@ export class LavalinkNode {
11221139

11231140
if (code !== 1000 || reason !== "Node-Destroy") {
11241141
if (this.NodeManager.nodes.has(this.id)) { // try to reconnect only when the node is still in the nodeManager.nodes list
1125-
this.reconnect();
1142+
// Only reconnect if not already in progress
1143+
if (this.reconnectionState === ReconnectionState.IDLE) {
1144+
this.reconnect();
1145+
}
11261146
}
11271147
}
11281148

0 commit comments

Comments
 (0)