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