mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-02 04:56:07 +00:00
feat: workerClient.calculateShardId
This commit is contained in:
parent
b4324f9487
commit
23c9c2a710
@ -1,7 +1,14 @@
|
|||||||
import { type UUID, randomUUID } from 'node:crypto';
|
import { type UUID, randomUUID } from 'node:crypto';
|
||||||
import { ApiHandler, Logger } from '..';
|
import { ApiHandler, Logger } from '..';
|
||||||
import { WorkerAdapter } from '../cache';
|
import { WorkerAdapter } from '../cache';
|
||||||
import { type DeepPartial, LogLevels, type MakeRequired, type When, lazyLoadPackage } from '../common';
|
import {
|
||||||
|
type DeepPartial,
|
||||||
|
LogLevels,
|
||||||
|
type MakeRequired,
|
||||||
|
type When,
|
||||||
|
calculateShardId,
|
||||||
|
lazyLoadPackage,
|
||||||
|
} from '../common';
|
||||||
import { EventHandler } from '../events';
|
import { EventHandler } from '../events';
|
||||||
import type { GatewayDispatchPayload, GatewaySendPayload } from '../types';
|
import type { GatewayDispatchPayload, GatewaySendPayload } from '../types';
|
||||||
import { Shard, type ShardManagerOptions, ShardSocketCloseCodes, type WorkerData, properties } from '../websocket';
|
import { Shard, type ShardManagerOptions, ShardSocketCloseCodes, type WorkerData, properties } from '../websocket';
|
||||||
@ -377,12 +384,18 @@ export class WorkerClient<Ready extends boolean = boolean> extends BaseClient {
|
|||||||
return this.onPacket(packet, shardId);
|
return this.onPacket(packet, shardId);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
workerData.totalShards = data.totalShards;
|
||||||
|
workerData.shards = [...this.shards.keys()];
|
||||||
this.resharding.clear();
|
this.resharding.clear();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
calculateShardId(guildId: string) {
|
||||||
|
return calculateShardId(guildId, this.workerData.totalShards);
|
||||||
|
}
|
||||||
|
|
||||||
private generateNonce(): UUID {
|
private generateNonce(): UUID {
|
||||||
const uuid = randomUUID();
|
const uuid = randomUUID();
|
||||||
if (this.promises.has(uuid)) return this.generateNonce();
|
if (this.promises.has(uuid)) return this.generateNonce();
|
||||||
@ -553,7 +566,7 @@ export function generateShardInfo(shard: Shard): WorkerShardInfo {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
interface WorkerClientOptions extends BaseClientOptions {
|
export interface WorkerClientOptions extends BaseClientOptions {
|
||||||
commands?: NonNullable<Client['options']>['commands'];
|
commands?: NonNullable<Client['options']>['commands'];
|
||||||
handlePayload?: ShardManagerOptions['handlePayload'];
|
handlePayload?: ShardManagerOptions['handlePayload'];
|
||||||
gateway?: ClientOptions['gateway'];
|
gateway?: ClientOptions['gateway'];
|
||||||
|
@ -288,6 +288,7 @@ export class WorkerManager extends Map<
|
|||||||
for (const [id] of this.entries()) {
|
for (const [id] of this.entries()) {
|
||||||
this.postMessage(id, {
|
this.postMessage(id, {
|
||||||
type: 'CONNECT_ALL_SHARDS_RESHARDING',
|
type: 'CONNECT_ALL_SHARDS_RESHARDING',
|
||||||
|
totalShards: this.options.totalShards,
|
||||||
} satisfies ConnnectAllShardsResharding);
|
} satisfies ConnnectAllShardsResharding);
|
||||||
}
|
}
|
||||||
this.forEach(w => {
|
this.forEach(w => {
|
||||||
@ -656,7 +657,12 @@ export type ManagerSpawnShardsResharding = CreateManagerMessage<
|
|||||||
Pick<ShardOptions, 'info' | 'properties' | 'compress'>
|
Pick<ShardOptions, 'info' | 'properties' | 'compress'>
|
||||||
>;
|
>;
|
||||||
export type DisconnectAllShardsResharding = CreateManagerMessage<'DISCONNECT_ALL_SHARDS_RESHARDING'>;
|
export type DisconnectAllShardsResharding = CreateManagerMessage<'DISCONNECT_ALL_SHARDS_RESHARDING'>;
|
||||||
export type ConnnectAllShardsResharding = CreateManagerMessage<'CONNECT_ALL_SHARDS_RESHARDING'>;
|
export type ConnnectAllShardsResharding = CreateManagerMessage<
|
||||||
|
'CONNECT_ALL_SHARDS_RESHARDING',
|
||||||
|
{
|
||||||
|
totalShards: number;
|
||||||
|
}
|
||||||
|
>;
|
||||||
export type ManagerSendPayload = CreateManagerMessage<
|
export type ManagerSendPayload = CreateManagerMessage<
|
||||||
'SEND_PAYLOAD',
|
'SEND_PAYLOAD',
|
||||||
GatewaySendPayload & { shardId: number; nonce: string }
|
GatewaySendPayload & { shardId: number; nonce: string }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user