Skip to content

Commit

Permalink
Speed up resume attempts (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
abalabahaha committed Nov 24, 2019
1 parent 1567f9d commit 16c6df5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions lib/gateway/Shard.js
Expand Up @@ -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.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();
}
Expand Down Expand Up @@ -1627,8 +1632,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) {
Expand All @@ -1643,9 +1650,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");
Expand Down
2 changes: 1 addition & 1 deletion lib/gateway/ShardManager.js
Expand Up @@ -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 {
Expand Down

0 comments on commit 16c6df5

Please sign in to comment.