From 3374252a36bd84ea9613df3d57794d8d818cf95f Mon Sep 17 00:00:00 2001 From: MARCROCK22 Date: Mon, 9 Sep 2024 02:29:57 +0000 Subject: [PATCH] fix: resharder --- src/client/client.ts | 4 +- src/common/it/utils.ts | 2 +- src/websocket/discord/sharder.ts | 141 ++++++++++++++++--------------- 3 files changed, 77 insertions(+), 70 deletions(-) diff --git a/src/client/client.ts b/src/client/client.ts index ffef110..12b103a 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -116,8 +116,8 @@ export class Client extends BaseClient { compress: this.options?.gateway?.compress, resharding: { getInfo: () => this.proxy.gateway.bot.get(), - interval: this.options?.resharding?.interval ?? 0, - percentage: this.options?.resharding?.percentage ?? 0, + interval: this.options?.resharding?.interval as number, + percentage: this.options?.resharding?.percentage as number, reloadGuilds: ids => { this.__handleGuilds = this.__handleGuilds?.concat(ids) ?? ids; }, diff --git a/src/common/it/utils.ts b/src/common/it/utils.ts index c09b8e1..cb43bfd 100644 --- a/src/common/it/utils.ts +++ b/src/common/it/utils.ts @@ -291,7 +291,7 @@ export function lazyLoadPackage(mod: string): T | undefined { try { return require(mod); } catch (e) { - console.log(`Cannot import ${mod}`); + console.log(`Cannot import ${mod}`, e); return; } } diff --git a/src/websocket/discord/sharder.ts b/src/websocket/discord/sharder.ts index 3a6113a..bf28bb5 100644 --- a/src/websocket/discord/sharder.ts +++ b/src/websocket/discord/sharder.ts @@ -52,8 +52,82 @@ export class ShardManager extends Map { workerData = worker_threads.workerData; if (worker_threads.parentPort) parentPort = worker_threads.parentPort; } + } + get totalShards() { + return this.options.totalShards ?? this.options.info.shards; + } + + get shardStart() { + return this.options.shardStart ?? 0; + } + + get shardEnd() { + return this.options.shardEnd ?? this.totalShards; + } + + get remaining() { + return this.options.info.session_start_limit.remaining; + } + + get concurrency() { + return this.options.info.session_start_limit.max_concurrency; + } + + get latency() { + let acc = 0; + + this.forEach(s => (acc += s.latency)); + + return acc / this.size; + } + + calculateShardId(guildId: string) { + return calculateShardId(guildId, this.totalShards); + } + + spawn(shardId: number) { + this.debugger?.info(`Spawn shard ${shardId}`); + let shard = this.get(shardId); + + shard ??= new Shard(shardId, { + token: this.options.token, + intents: this.options.intents, + info: { ...this.options.info, shards: this.totalShards }, + handlePayload: this.options.handlePayload, + properties: this.options.properties, + debugger: this.debugger, + compress: this.options.compress ?? false, + presence: this.options.presence?.(shardId, -1), + }); + + this.set(shardId, shard); + + return shard; + } + + async spawnShards(): Promise { + const buckets = this.spawnBuckets(); + + this.debugger?.info('Spawn shards'); + for (const bucket of buckets) { + for (const shard of bucket) { + if (!shard) { + break; + } + this.debugger?.info(`${shard.id} add to connect queue`); + this.connectQueue.push(shard.connect.bind(shard)); + } + } + await this.startResharder(); + } + + async startResharder() { if (this.options.resharding.interval <= 0) return; + if (this.shardStart !== 0 || this.shardEnd !== this.totalShards) + return this.debugger?.debug('Cannot start resharder'); + + this.debugger?.debug('Resharder enabled'); setInterval(async () => { this.debugger?.debug('Checking if reshard is needed'); const info = await this.options.resharding.getInfo(); @@ -125,73 +199,6 @@ export class ShardManager extends Map { }, this.options.resharding.interval); } - get totalShards() { - return this.options.totalShards ?? this.options.info.shards; - } - - get shardStart() { - return this.options.shardStart ?? 0; - } - - get shardEnd() { - return this.options.shardEnd ?? this.totalShards; - } - - get remaining() { - return this.options.info.session_start_limit.remaining; - } - - get concurrency() { - return this.options.info.session_start_limit.max_concurrency; - } - - get latency() { - let acc = 0; - - this.forEach(s => (acc += s.latency)); - - return acc / this.size; - } - - calculateShardId(guildId: string) { - return calculateShardId(guildId, this.totalShards); - } - - spawn(shardId: number) { - this.debugger?.info(`Spawn shard ${shardId}`); - let shard = this.get(shardId); - - shard ??= new Shard(shardId, { - token: this.options.token, - intents: this.options.intents, - info: { ...this.options.info, shards: this.totalShards }, - handlePayload: this.options.handlePayload, - properties: this.options.properties, - debugger: this.debugger, - compress: this.options.compress ?? false, - presence: this.options.presence?.(shardId, -1), - }); - - this.set(shardId, shard); - - return shard; - } - - async spawnShards(): Promise { - const buckets = this.spawnBuckets(); - - this.debugger?.info('Spawn shards'); - for (const bucket of buckets) { - for (const shard of bucket) { - if (!shard) { - break; - } - this.debugger?.info(`${shard.id} add to connect queue`); - this.connectQueue.push(shard.connect.bind(shard)); - } - } - } - /* * spawns buckets in order * https://discord.com/developers/docs/topics/gateway#sharding-max-concurrency