mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-02 04:56:07 +00:00
fix: resharder
This commit is contained in:
parent
a1b0c20a30
commit
3374252a36
@ -116,8 +116,8 @@ export class Client<Ready extends boolean = boolean> 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;
|
||||
},
|
||||
|
@ -291,7 +291,7 @@ export function lazyLoadPackage<T>(mod: string): T | undefined {
|
||||
try {
|
||||
return require(mod);
|
||||
} catch (e) {
|
||||
console.log(`Cannot import ${mod}`);
|
||||
console.log(`Cannot import ${mod}`, e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -52,8 +52,82 @@ export class ShardManager extends Map<number, Shard> {
|
||||
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<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.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<number, Shard> {
|
||||
}, 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
|
||||
* https://discord.com/developers/docs/topics/gateway#sharding-max-concurrency
|
||||
|
Loading…
x
Reference in New Issue
Block a user