fix: resharder

This commit is contained in:
MARCROCK22 2024-09-09 02:29:57 +00:00
parent a1b0c20a30
commit 3374252a36
3 changed files with 77 additions and 70 deletions

View File

@ -116,8 +116,8 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
compress: this.options?.gateway?.compress, compress: this.options?.gateway?.compress,
resharding: { resharding: {
getInfo: () => this.proxy.gateway.bot.get(), getInfo: () => this.proxy.gateway.bot.get(),
interval: this.options?.resharding?.interval ?? 0, interval: this.options?.resharding?.interval as number,
percentage: this.options?.resharding?.percentage ?? 0, percentage: this.options?.resharding?.percentage as number,
reloadGuilds: ids => { reloadGuilds: ids => {
this.__handleGuilds = this.__handleGuilds?.concat(ids) ?? ids; this.__handleGuilds = this.__handleGuilds?.concat(ids) ?? ids;
}, },

View File

@ -291,7 +291,7 @@ export function lazyLoadPackage<T>(mod: string): T | undefined {
try { try {
return require(mod); return require(mod);
} catch (e) { } catch (e) {
console.log(`Cannot import ${mod}`); console.log(`Cannot import ${mod}`, e);
return; return;
} }
} }

View File

@ -52,8 +52,82 @@ export class ShardManager extends Map<number, Shard> {
workerData = worker_threads.workerData; workerData = worker_threads.workerData;
if (worker_threads.parentPort) parentPort = worker_threads.parentPort; 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<void> {
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.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 () => { setInterval(async () => {
this.debugger?.debug('Checking if reshard is needed'); this.debugger?.debug('Checking if reshard is needed');
const info = await this.options.resharding.getInfo(); const info = await this.options.resharding.getInfo();
@ -125,73 +199,6 @@ export class ShardManager extends Map<number, Shard> {
}, this.options.resharding.interval); }, 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<void> {
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 * spawns buckets in order
* https://discord.com/developers/docs/topics/gateway#sharding-max-concurrency * https://discord.com/developers/docs/topics/gateway#sharding-max-concurrency