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

Improved max_concurrency aware ShardManager #1370

Merged
merged 5 commits into from
Jun 2, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions examples/sharding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const Eris = require("eris");

// Replace TOKEN with your bot account's token
const bot = new Eris("Bot TOKEN", {
firstShardID: 0,
lastShardID: 15,
maxShards: 16,
getAllUsers: false,
intents: ["guilds", "guildMembers", "guildPresences"]
});

bot.on("ready", () => { // When the bot is ready
console.log("Ready!"); // Log "Ready!"
console.timeEnd("ready");
});

bot.on("error", (err) => {
console.error(err); // or your preferred logger
});

bot.on("shardReady", (id) => {
console.log(`Shard ${id} ready!`);
});

console.time("ready");
bot.connect(); // Get the bot to connect to Discord
2 changes: 1 addition & 1 deletion lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ class Client extends EventEmitter {
}

for(let i = this.options.firstShardID; i <= this.options.lastShardID; ++i) {
this.shards.spawn(i);
await this.shards.spawn(i);
}
} catch(err) {
if(!this.options.autoreconnect) {
Expand Down
2 changes: 1 addition & 1 deletion lib/gateway/Shard.js
Original file line number Diff line number Diff line change
Expand Up @@ -1854,7 +1854,7 @@ class Shard extends EventEmitter {
this.connectTimeout = null;
this.status = "ready";
this.presence.status = "online";
this.client.shards._readyPacketCB();
this.client.shards._readyPacketCB(this.id);

if(packet.t === "RESUMED") {
// Can only heartbeat after resume succeeds, discord/discord-api-docs#1619
Expand Down
79 changes: 57 additions & 22 deletions lib/gateway/ShardManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,28 @@ class ShardManager extends Collection {
this._client = client;

this.connectQueue = [];
this.lastConnect = 0;
this.connectTimeout = null;

this.concurrency = null;
this.buckets = new Map();
}

connect(shard) {
if(shard.sessionID || (this.lastConnect <= Date.now() - 5000 && !this.find((shard) => shard.connecting))) {
shard.connect();
this.lastConnect = Date.now() + 7500;
} else {
this.connectQueue.push(shard);
this.tryConnect();
async connect(shard) {
// fetch max_concurrency if needed
if(!this.concurrency) {
const gateway = await this._client.getBotGateway();
if(gateway.session_start_limit && gateway.session_start_limit.max_concurrency) {
this.concurrency = gateway.session_start_limit.max_concurrency;
} else {
this.concurrency = 1;
}
}

this.connectQueue.push(shard);
this.tryConnect();
}

spawn(id) {
async spawn(id) {
let shard = this.get(id);
if(!shard) {
shard = this.add(new Shard(id, this._client));
Expand Down Expand Up @@ -91,27 +98,55 @@ class ShardManager extends Collection {
});
}
if(shard.status === "disconnected") {
this.connect(shard);
return this.connect(shard);
}
}

tryConnect() {
if(this.connectQueue.length > 0) {
if(this.lastConnect <= Date.now() - 5000) {
const shard = this.connectQueue.shift();
shard.connect();
this.lastConnect = Date.now() + 7500;
} else if(!this.connectTimeout) {
this.connectTimeout = setTimeout(() => {
this.connectTimeout = null;
this.tryConnect();
}, 1000);
// nothing in queue
if(this.connectQueue.length === 0) {
return;
}

// loop over the connectQueue
for(const shard of this.connectQueue) {
// find the bucket for our shard
const rateLimitKey = (shard.id % this.concurrency) || 0;
const lastConnect = this.buckets.get(rateLimitKey) || 0;

// has enough time passed since the last connect for this bucket (5s/bucket)?
// alternatively if we have a sessionID, we can skip this check
if(!shard.sessionID && Date.now() - lastConnect < 5000) {
continue;
}

// Are there any connecting shards in the same bucket we should wait on?
if(this.some((s) => s.connecting && ((s.id % this.concurrency) || 0) === rateLimitKey)) {
continue;
}

// connect the shard
shard.connect();
this.buckets.set(rateLimitKey, Date.now());

// remove the shard from the queue
const index = this.connectQueue.findIndex((s) => s.id === shard.id);
this.connectQueue.splice(index, 1);
}

// set the next timeout if we have more shards to connect
if(!this.connectTimeout && this.connectQueue.length > 0) {
this.connectTimeout = setTimeout(() => {
this.connectTimeout = null;
this.tryConnect();
}, 500);
}
}

_readyPacketCB() {
this.lastConnect = Date.now();
_readyPacketCB(shardID) {
const rateLimitKey = (shardID % this.concurrency) || 0;
this.buckets.set(rateLimitKey, Date.now());

this.tryConnect();
}

Expand Down