From c224023cbed18aaceed9844061b48790fd263ab5 Mon Sep 17 00:00:00 2001 From: abalabahaha Date: Wed, 20 Nov 2019 09:52:57 -0800 Subject: [PATCH 1/3] Speed up resume attempts --- lib/gateway/Shard.js | 6 +++++- lib/gateway/ShardManager.js | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/gateway/Shard.js b/lib/gateway/Shard.js index db3d101e0..45558cd8a 100644 --- a/lib/gateway/Shard.js +++ b/lib/gateway/Shard.js @@ -129,7 +129,7 @@ class Shard extends EventEmitter { this.emit("debug", `Queueing reconnect in ${this.reconnectInterval}ms | Attempt ${this.connectAttempts}`, this.id); setTimeout(() => { this.client.shards.connect(this); - }, this.reconnectInterval); + }, this.sessionID ? 0 : this.reconnectInterval); this.reconnectInterval = Math.min(Math.round(this.reconnectInterval * (Math.random() * 2 + 1)), 30000); } else if(!options.reconnect) { this.hardReset(); @@ -1633,8 +1633,10 @@ class Shard extends EventEmitter { err = new Error("Gateway received invalid message"); } else if(event.code === 4003) { err = new Error("Not authenticated"); + this.sessionID = null; } else if(event.code === 4004) { err = new Error("Authentication failed"); + this.sessionID = null; reconnect = false; this.emit("error", new Error(`Invalid token: ${this.client.token}`)); } else if(event.code === 4005) { @@ -1649,9 +1651,11 @@ class Shard extends EventEmitter { err = new Error("Gateway connection was ratelimited"); } else if(event.code === 4010) { err = new Error("Invalid shard key"); + this.sessionID = null; reconnect = false; } else if(event.code === 4011) { err = new Error("Shard has too many guilds (>2500)"); + this.sessionID = null; reconnect = false; } else if(event.code === 1006) { err = new Error("Connection reset by peer"); diff --git a/lib/gateway/ShardManager.js b/lib/gateway/ShardManager.js index 4fcce7ea0..31171197c 100644 --- a/lib/gateway/ShardManager.js +++ b/lib/gateway/ShardManager.js @@ -19,7 +19,7 @@ class ShardManager extends Collection { } connect(shard) { - if(this.lastConnect <= Date.now() - 5000 && !this.find((shard) => shard.connecting)) { + if(shard.sessionID || (this.lastConnect <= Date.now() - 5000 && !this.find((shard) => shard.connecting))) { shard.connect(); this.lastConnect = Date.now() + 7500; } else { From fe18deddf748223362fbc4f7325f4a18ea25755b Mon Sep 17 00:00:00 2001 From: abalabahaha Date: Sat, 23 Nov 2019 13:17:00 -0800 Subject: [PATCH 2/3] Fix reconnectInterval update --- lib/gateway/Shard.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/gateway/Shard.js b/lib/gateway/Shard.js index cfcc9d509..49dedffec 100644 --- a/lib/gateway/Shard.js +++ b/lib/gateway/Shard.js @@ -126,11 +126,16 @@ class Shard extends EventEmitter { * @prop {String} message The debug message * @prop {Number} id The ID of the shard */ - this.emit("debug", `Queueing reconnect in ${this.reconnectInterval}ms | Attempt ${this.connectAttempts}`, this.id); - setTimeout(() => { + if(this.sessionID) { + this.emit("debug", `Immediately reconnecting for potential resume | Attempt ${this.connectAttempts}`, this.id); this.client.shards.connect(this); - }, this.sessionID ? 0 : this.reconnectInterval); - this.reconnectInterval = Math.min(Math.round(this.reconnectInterval * (Math.random() * 2 + 1)), 30000); + } else { + this.emit("debug", `Queueing reconnect in ${this.reconnectInterval}ms | Attempt ${this.connectAttempts}`, this.id); + setTimeout(() => { + this.client.shards.connect(this); + }, this.reconnectInterval); + this.reconnectInterval = Math.min(Math.round(this.reconnectInterval * (Math.random() * 2 + 1)), 30000); + } } else if(!options.reconnect) { this.hardReset(); } @@ -254,12 +259,12 @@ class Shard extends EventEmitter { } const guild = this.client.guilds.get(packet.d.guild_id); if(!guild) { - this.emit("debug", "Rogue presence update: " + JSON.stringify(packet), this.id); + // this.emit("debug", "Rogue presence update: " + JSON.stringify(packet), this.id); break; } let member = guild.members.get(packet.d.id = packet.d.user.id); if(member && JSON.stringify(member.game) === JSON.stringify(packet.d.game) && member.status === packet.d.status) { - this.emit("debug", "Duplicate presence update: " + JSON.stringify(packet)); + // this.emit("debug", "Duplicate presence update: " + JSON.stringify(packet)); break; } let oldPresence = null; From b00fba341b99733558c1ef679a099a17d84a8ec6 Mon Sep 17 00:00:00 2001 From: abalabahaha Date: Sat, 23 Nov 2019 13:18:39 -0800 Subject: [PATCH 3/3] oops --- lib/gateway/Shard.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/gateway/Shard.js b/lib/gateway/Shard.js index 49dedffec..a7a47d52e 100644 --- a/lib/gateway/Shard.js +++ b/lib/gateway/Shard.js @@ -259,12 +259,12 @@ class Shard extends EventEmitter { } const guild = this.client.guilds.get(packet.d.guild_id); if(!guild) { - // this.emit("debug", "Rogue presence update: " + JSON.stringify(packet), this.id); + this.emit("debug", "Rogue presence update: " + JSON.stringify(packet), this.id); break; } let member = guild.members.get(packet.d.id = packet.d.user.id); if(member && JSON.stringify(member.game) === JSON.stringify(packet.d.game) && member.status === packet.d.status) { - // this.emit("debug", "Duplicate presence update: " + JSON.stringify(packet)); + this.emit("debug", "Duplicate presence update: " + JSON.stringify(packet)); break; } let oldPresence = null;