diff --git a/src/ws/cleanup_loading_shards.ts b/src/ws/cleanup_loading_shards.ts index dd32b2457..e1f61df17 100644 --- a/src/ws/cleanup_loading_shards.ts +++ b/src/ws/cleanup_loading_shards.ts @@ -1,21 +1,20 @@ -import { delay } from "../util/utils.ts"; import { ws } from "./ws.ts"; /** The handler to clean up shards that identified but never received a READY. */ -export async function cleanupLoadingShards() { - while (ws.loadingShards.size) { - ws.log("DEBUG", "Running while loop in cleanupLoadingShards function."); - const now = Date.now(); - ws.loadingShards.forEach((loadingShard) => { - ws.log("DEBUG", `Running forEach loop in cleanupLoadingShards function.`); - // Not a minute yet. Max should be few seconds but do a minute to be safe. - if (now < loadingShard.startedAt + 60000) return; +export function cleanupLoadingShards() { + if (!ws.loadingShards.size) return; - loadingShard.reject( - `[Identify Failure] Shard ${loadingShard.shardId} has not received READY event in over a minute.` - ); - }); + ws.log("DEBUG", "Running setTimeout in cleanupLoadingShards function."); + const now = Date.now(); + ws.loadingShards.forEach((loadingShard) => { + ws.log("DEBUG", `Running forEach loop in cleanupLoadingShards function.`); + // Not a minute yet. Max should be few seconds but do a minute to be safe. + if (now < loadingShard.startedAt + 60000) return; - await delay(1000); - } + loadingShard.reject( + `[Identify Failure] Shard ${loadingShard.shardId} has not received READY event in over a minute.` + ); + }); + + if (ws.loadingShards.size) setTimeout(cleanupLoadingShards, 10000); } diff --git a/src/ws/handle_on_message.ts b/src/ws/handle_on_message.ts index 262816a8d..808475c1a 100644 --- a/src/ws/handle_on_message.ts +++ b/src/ws/handle_on_message.ts @@ -95,11 +95,11 @@ export async function handleOnMessage(message: any, shardId: number) { ws.loadingShards.get(shardId)?.resolve(true); ws.loadingShards.delete(shardId); - // Wait 5 seconds to spawn next shard + // Wait few seconds to spawn next shard setTimeout(() => { const bucket = ws.buckets.get(shardId % ws.botGatewayData.sessionStartLimit.maxConcurrency); if (bucket) bucket.createNextShard.shift()?.(); - }, 5000); + }, ws.spawnShardDelay); } // Update the sequence number if it is present diff --git a/src/ws/heartbeat.ts b/src/ws/heartbeat.ts index 90bee69ba..d695136f2 100644 --- a/src/ws/heartbeat.ts +++ b/src/ws/heartbeat.ts @@ -28,7 +28,7 @@ export async function heartbeat(shardId: number, interval: number) { shard.heartbeat.interval = interval; shard.heartbeat.intervalId = setInterval(async () => { - ws.log("DEBUG", `Running setInterval in heartbeat file.`); + ws.log("DEBUG", `Running setInterval in heartbeat file. Shard: ${shardId}`); const currentShard = ws.shards.get(shardId); if (!currentShard) return; diff --git a/src/ws/start_gateway.ts b/src/ws/start_gateway.ts index 58a3990fc..e91415493 100644 --- a/src/ws/start_gateway.ts +++ b/src/ws/start_gateway.ts @@ -38,5 +38,4 @@ export async function startGateway(options: StartGatewayOptions) { ws.lastShardId = options.lastShardId || ws.botGatewayData.shards - 1; ws.spawnShards(ws.firstShardId); - await ws.cleanupLoadingShards(); } diff --git a/src/ws/ws.ts b/src/ws/ws.ts index 9aa3be071..10c1188ae 100644 --- a/src/ws/ws.ts +++ b/src/ws/ws.ts @@ -26,6 +26,8 @@ export const ws = { reshard: true, /** The percentage at which resharding should occur. */ reshardPercentage: 80, + /** The delay in milliseconds to wait before spawning next shard. OPTIMAL IS ABOVE 2500. YOU DON"T WANT TO HIT THE RATE LIMIT!!! */ + spawnShardDelay: 2500, /** The maximum shard Id number. Useful for zero-downtime updates or resharding. */ maxShards: 0, /** Whether or not the resharder should automatically switch to LARGE BOT SHARDING when you are above 100K servers. */