From b7063580e0426dd4d72333a189c47bab5c7211dc Mon Sep 17 00:00:00 2001 From: Fleny Date: Mon, 12 Aug 2024 16:32:59 +0200 Subject: [PATCH] feat(gateway): Make `resharding.getSessionInfo` optional (#3854) * Make getSessionInfo optional * change resharding.getSessionInfo to throw in checkIfReshardingIsNeeded * Change warn message on spawnShards --- packages/gateway/src/manager.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/gateway/src/manager.ts b/packages/gateway/src/manager.ts index 98f69abcd..acec4be16 100644 --- a/packages/gateway/src/manager.ts +++ b/packages/gateway/src/manager.ts @@ -60,11 +60,6 @@ export function createGatewayManager(options: CreateGatewayManagerOptions): Gate checkInterval: 28800000, // 8 hours shards: new Collection(), pendingShards: new Collection(), - async getSessionInfo() { - throw new Error( - '[Resharding] was enabled but no getSessionInfo handler was provided. Please set a handler like: gateway.resharding.getSessionInfo = async () => { // insert code here to fetch getSessionInfo from rest process. }', - ) - }, async checkIfReshardingIsNeeded() { gateway.logger.warn('[Resharding] Checking if resharding is needed.') @@ -74,6 +69,10 @@ export function createGatewayManager(options: CreateGatewayManagerOptions): Gate return { needed: false } } + if (!gateway.resharding.getSessionInfo) { + throw new Error("[Resharding] Resharding is enabled but no 'resharding.getSessionInfo()' is not provided.") + } + gateway.logger.warn('[Resharding] Resharding is enabled.') const sessionInfo = await gateway.resharding.getSessionInfo() @@ -328,6 +327,13 @@ export function createGatewayManager(options: CreateGatewayManagerOptions): Gate // It is better to ensure there is always only one clearInterval(gateway.resharding.checkIntervalId) + if (!gateway.resharding.getSessionInfo) { + gateway.resharding.enabled = false + gateway.logger.warn("[Resharding] Resharding is enabled but 'resharding.getSessionInfo()' was not provided. Disabling resharding.") + + return + } + gateway.resharding.checkIntervalId = setInterval(async () => { const reshardingInfo = await gateway.resharding.checkIfReshardingIsNeeded() @@ -688,7 +694,7 @@ export interface GatewayManager extends Required { /** Holds the pending shards that have been created and are pending all shards finish loading. */ pendingShards: Collection /** Handler to get shard count and other session info. */ - getSessionInfo: () => Promise> + getSessionInfo?: () => Promise> /** Handler to edit the shard id on any cached guilds. */ updateGuildsShardId: (guildIds: string[], shardId: number) => Promise /** Handler to check if resharding is necessary. */