feat(gateway): Make resharding.getSessionInfo optional (#3854)

* Make getSessionInfo optional

* change resharding.getSessionInfo to throw in checkIfReshardingIsNeeded

* Change warn message on spawnShards
This commit is contained in:
Fleny
2024-08-12 16:32:59 +02:00
committed by GitHub
parent 4899d0aa66
commit b7063580e0
+12 -6
View File
@@ -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<CreateGatewayManagerOptions> {
/** Holds the pending shards that have been created and are pending all shards finish loading. */
pendingShards: Collection<number, Shard>
/** Handler to get shard count and other session info. */
getSessionInfo: () => Promise<Camelize<DiscordGetGatewayBot>>
getSessionInfo?: () => Promise<Camelize<DiscordGetGatewayBot>>
/** Handler to edit the shard id on any cached guilds. */
updateGuildsShardId: (guildIds: string[], shardId: number) => Promise<void>
/** Handler to check if resharding is necessary. */