From d62a5b71b17dd77df759ff34f9b96829e48d6483 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Tue, 15 Mar 2022 02:45:07 +0000 Subject: [PATCH] fix: concurrency typo thx giveawayboat <3 --- gateway/calculateMaxShards.ts | 9 +++++++++ gateway/gateway_manager.ts | 4 ++++ gateway/mod.ts | 1 + gateway/resharder.ts | 7 ++----- gateway/spawnShards.ts | 6 ++++++ 5 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 gateway/calculateMaxShards.ts diff --git a/gateway/calculateMaxShards.ts b/gateway/calculateMaxShards.ts new file mode 100644 index 000000000..915112a4a --- /dev/null +++ b/gateway/calculateMaxShards.ts @@ -0,0 +1,9 @@ +/** Handler used to determine max number of shards to use based upon the max concurrency. */ +export function calculateMaxShards(maxShards: number, maxConcurrency: number): number { + if (maxShards < 100) return maxShards; + + return Math.ceil( + maxShards / + (maxConcurrency === 1 ? 16 : maxConcurrency), + ) * maxConcurrency; +} diff --git a/gateway/gateway_manager.ts b/gateway/gateway_manager.ts index ba7f65b50..6daa8b783 100644 --- a/gateway/gateway_manager.ts +++ b/gateway/gateway_manager.ts @@ -23,6 +23,7 @@ import { DiscordenoShard } from "./shard.ts"; import { GatewayIntents } from "../types/shared.ts"; import { StatusUpdate } from "../helpers/misc/editBotStatus.ts"; import { DiscordGatewayPayload } from "../types/discord.ts"; +import { calculateMaxShards } from "./calculateMaxShards.ts"; /** Create a new Gateway Manager. * @@ -97,6 +98,7 @@ export function createGatewayManager( resume: options.resume ?? resume, safeRequestsPerShard: options.safeRequestsPerShard ?? safeRequestsPerShard, handleDiscordPayload: options.handleDiscordPayload, + calculateMaxShards: options.calculateMaxShards ?? calculateMaxShards, }; } @@ -222,4 +224,6 @@ export interface GatewayManager { resume: typeof resume; /** Calculates the number of requests in a shard that are safe to be used. */ safeRequestsPerShard: typeof safeRequestsPerShard; + /** Calculates the number of shards to use based on the max concurrency */ + calculateMaxShards: typeof calculateMaxShards; } diff --git a/gateway/mod.ts b/gateway/mod.ts index d41ed7e6c..68e1d432d 100644 --- a/gateway/mod.ts +++ b/gateway/mod.ts @@ -12,3 +12,4 @@ export * from "./tellWorkerToIdentify.ts"; export * from "./shard.ts"; export * from "./gateway_manager.ts"; export * from "./safeRequestsPerShard.ts"; +export * from "./calculateMaxShards.ts"; diff --git a/gateway/resharder.ts b/gateway/resharder.ts index fdf43ae35..13013c6ed 100644 --- a/gateway/resharder.ts +++ b/gateway/resharder.ts @@ -38,12 +38,9 @@ export async function resharder( gateway.sessionStartLimitResetAfter = results.sessionStartLimit.resetAfter; gateway.maxConcurrency = results.sessionStartLimit.maxConcurrency; // If more than 100K servers, begin switching to 16x sharding - if (gateway.maxShards > 60 && gateway.useOptimalLargeBotSharding) { + if (gateway.useOptimalLargeBotSharding) { gateway.debug("[Resharding] Using optimal large bot sharding solution."); - gateway.maxShards = Math.ceil( - gateway.maxShards / - (results.sessionStartLimit.maxConcurrency === 1 ? 16 : results.sessionStartLimit.maxConcurrency), - ); + gateway.maxShards = gateway.calculateMaxShards(gateway.maxShards, results.sessionStartLimit.maxConcurrency); } gateway.spawnShards(gateway, gateway.firstShardId); diff --git a/gateway/spawnShards.ts b/gateway/spawnShards.ts index 1b0ea07a7..10112fb62 100644 --- a/gateway/spawnShards.ts +++ b/gateway/spawnShards.ts @@ -38,6 +38,12 @@ export function prepareBuckets(gateway: GatewayManager, firstShardId: number, la } export function spawnShards(gateway: GatewayManager, firstShardId = 0) { + // PREPARES THE MAX SHARD COUNT BY CONCURRENCY + if (gateway.useOptimalLargeBotSharding) { + gateway.debug("[Resharding] Using optimal large bot sharding solution."); + gateway.maxShards = gateway.calculateMaxShards(gateway.maxShards, gateway.maxConcurrency); + } + // PREPARES ALL SHARDS IN SPECIFIC BUCKETS prepareBuckets(gateway, firstShardId, gateway.lastShardId ? gateway.lastShardId + 1 : gateway.maxShards);