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 1 commit
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
77 changes: 56 additions & 21 deletions lib/gateway/ShardManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,39 @@

const Base = require("../structures/Base");
const Collection = require("../util/Collection");
const Endpoints = require("../rest/Endpoints");
const Shard = require("./Shard");

const SHARD_IDENTIFY_RATELIMIT = 5000; // 5s
HcgRandon marked this conversation as resolved.
Show resolved Hide resolved

class ShardManager extends Collection {
constructor(client) {
super(Shard);
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.requestHandler.request("GET", Endpoints.GATEWAY_BOT, true);
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 +101,52 @@ 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?
// alternatively if we have a sessionID, we can skip this check
if(!shard.sessionID && Date.now() - lastConnect < SHARD_IDENTIFY_RATELIMIT) {
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();
}, 1000);
}
}

_readyPacketCB() {
this.lastConnect = Date.now();
this.tryConnect();
}

Expand Down