Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up resume attempts #563

Merged
merged 5 commits into from Nov 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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