mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-03 05:26:07 +00:00
fix shardStart & shardEnd
This commit is contained in:
parent
5f8d71fc3f
commit
d8dd63170a
@ -96,8 +96,8 @@ export class BaseClient {
|
||||
if (cache) {
|
||||
this.cache = new Cache(
|
||||
this.cache?.intents ?? 0,
|
||||
cache.adapter,
|
||||
cache.disabledCache ?? this.cache?.disabledCache,
|
||||
cache?.adapter ?? this.cache?.adapter ?? new MemoryAdapter(),
|
||||
cache.disabledCache ?? this.cache?.disabledCache ?? [],
|
||||
this,
|
||||
);
|
||||
}
|
||||
@ -289,7 +289,7 @@ export type RuntimeConfig = OmitInsert<InternalRuntimeConfig, 'intents', { inten
|
||||
|
||||
export type ServicesOptions = {
|
||||
rest?: ApiHandler;
|
||||
cache?: { adapter: Adapter; disabledCache?: Cache['disabledCache'] };
|
||||
cache?: { adapter?: Adapter; disabledCache?: Cache['disabledCache'] };
|
||||
langs?: {
|
||||
default?: string;
|
||||
aliases?: Record<string, LocaleString[]>;
|
||||
|
@ -98,6 +98,7 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
|
||||
debug: debugRC,
|
||||
shardStart: this.options?.shards?.start,
|
||||
shardEnd: this.options?.shards?.end,
|
||||
totalShards: this.options?.shards?.total ?? this.options?.shards?.end,
|
||||
});
|
||||
}
|
||||
|
||||
@ -180,6 +181,7 @@ export interface ClientOptions extends BaseClientOptions {
|
||||
shards?: {
|
||||
start: number;
|
||||
end: number;
|
||||
total?: number;
|
||||
};
|
||||
commands?: {
|
||||
prefix: (message: Message) => Promise<string[]> | string[];
|
||||
|
@ -16,6 +16,7 @@ const ShardManagerDefaults: Partial<ShardManagerOptions> = {
|
||||
intents: 0,
|
||||
properties,
|
||||
version: 10,
|
||||
shardStart: 0,
|
||||
handlePayload: (shardId: number, packet: GatewayDispatchPayload): void => {
|
||||
console.info(`Packet ${packet.t} on shard ${shardId}`);
|
||||
},
|
||||
|
@ -36,6 +36,18 @@ export class ShardManager extends Map<number, Shard> {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
@ -63,7 +75,7 @@ export class ShardManager extends Map<number, Shard> {
|
||||
shard ??= new Shard(shardId, {
|
||||
token: this.options.token,
|
||||
intents: this.options.intents,
|
||||
info: { ...this.options.info, shards: (this.options.shardEnd ?? this.options.totalShards)! },
|
||||
info: { ...this.options.info, shards: this.totalShards },
|
||||
handlePayload: this.options.handlePayload,
|
||||
properties: this.options.properties,
|
||||
debugger: this.debugger,
|
||||
@ -97,17 +109,10 @@ export class ShardManager extends Map<number, Shard> {
|
||||
*/
|
||||
spawnBuckets(): Shard[][] {
|
||||
this.debugger?.info('#0 Preparing buckets');
|
||||
const chunks = SequentialBucket.chunk(
|
||||
new Array(
|
||||
this.options.shardStart !== undefined && this.options.shardEnd !== undefined
|
||||
? this.options.shardEnd - this.options.shardStart
|
||||
: this.options.totalShards,
|
||||
),
|
||||
this.concurrency,
|
||||
);
|
||||
const chunks = SequentialBucket.chunk(new Array(this.shardEnd - this.shardStart), this.concurrency);
|
||||
chunks.forEach((arr: any[], index: number) => {
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
const id = i + (index > 0 ? index * this.concurrency : 0) + (this.options.shardStart ?? 0);
|
||||
const id = i + (index > 0 ? index * this.concurrency : 0) + this.shardStart;
|
||||
chunks[index][i] = this.spawn(id);
|
||||
}
|
||||
});
|
||||
|
@ -29,7 +29,7 @@ export class WorkerManager extends Map<number, Worker & { ready?: boolean }> {
|
||||
rest!: ApiHandler;
|
||||
constructor(options: MakePartial<WorkerManagerOptions, 'token' | 'intents' | 'info' | 'handlePayload'>) {
|
||||
super();
|
||||
this.options = MergeOptions<WorkerManager['options']>(WorkerManagerDefaults, options);
|
||||
this.options = options as WorkerManager['options'];
|
||||
this.cacheAdapter = new MemoryAdapter();
|
||||
}
|
||||
|
||||
@ -54,7 +54,15 @@ export class WorkerManager extends Map<number, Worker & { ready?: boolean }> {
|
||||
}
|
||||
|
||||
get totalShards() {
|
||||
return this.options.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 shardsPerWorker() {
|
||||
@ -82,11 +90,11 @@ export class WorkerManager extends Map<number, Worker & { ready?: boolean }> {
|
||||
}
|
||||
|
||||
calculateShardId(guildId: string) {
|
||||
return Number((BigInt(guildId) >> 22n) % BigInt(this.options.info.shards ?? 1));
|
||||
return Number((BigInt(guildId) >> 22n) % BigInt(this.totalShards ?? 1));
|
||||
}
|
||||
|
||||
calculateWorkerId(shardId: number) {
|
||||
const workerId = Math.floor((shardId - this.options.shardStart) / this.shardsPerWorker);
|
||||
const workerId = Math.floor((shardId - this.shardStart) / this.shardsPerWorker);
|
||||
if (workerId >= this.workers) {
|
||||
throw new Error('Invalid shardId');
|
||||
}
|
||||
@ -97,17 +105,13 @@ export class WorkerManager extends Map<number, Worker & { ready?: boolean }> {
|
||||
this.debugger?.info('Preparing buckets');
|
||||
|
||||
const chunks = SequentialBucket.chunk<number>(
|
||||
new Array(
|
||||
this.options.shardStart !== undefined && this.options.shardEnd !== undefined
|
||||
? this.options.shardEnd - this.options.shardStart
|
||||
: this.options.totalShards,
|
||||
),
|
||||
new Array(this.shardEnd - this.shardStart),
|
||||
this.options.shardsPerWorker,
|
||||
);
|
||||
|
||||
chunks.forEach((shards, index) => {
|
||||
for (let i = 0; i < shards.length; i++) {
|
||||
const id = i + (index > 0 ? index * this.options.shardsPerWorker : 0) + (this.options.shardStart ?? 0);
|
||||
const id = i + (index > 0 ? index * this.options.shardsPerWorker : 0) + this.shardStart;
|
||||
chunks[index][i] = id;
|
||||
}
|
||||
});
|
||||
@ -134,7 +138,10 @@ export class WorkerManager extends Map<number, Worker & { ready?: boolean }> {
|
||||
worker.postMessage({
|
||||
type: 'SPAWN_SHARDS',
|
||||
compress: this.options.compress ?? false,
|
||||
info: this.options.info,
|
||||
info: {
|
||||
...this.options.info,
|
||||
shards: this.totalShards,
|
||||
},
|
||||
properties: this.options.properties,
|
||||
} satisfies ManagerSpawnShards);
|
||||
}
|
||||
@ -379,12 +386,10 @@ export class WorkerManager extends Map<number, Worker & { ready?: boolean }> {
|
||||
debug: this.options.debug,
|
||||
});
|
||||
this.options.info ??= await new Router(this.rest).createProxy().gateway.bot.get();
|
||||
this.options.totalShards ??= this.options.info.shards;
|
||||
this.options.shardEnd ??= this.options.info.shards;
|
||||
this.options.totalShards ??= this.options.shardEnd;
|
||||
this.options = MergeOptions<Required<WorkerManagerOptions>>(WorkerManagerDefaults, this.options);
|
||||
this.options.workers ??= Math.ceil(this.options.totalShards / this.options.shardsPerWorker);
|
||||
this.options.info.shards = this.options.totalShards;
|
||||
this.options.shardEnd ??= this.options.totalShards;
|
||||
this.options.shardStart ??= 0;
|
||||
this.connectQueue = new ConnectQueue(5.5e3, this.concurrency);
|
||||
|
||||
if (this.options.debug) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user