diff --git a/src/ws/identify.ts b/src/ws/identify.ts index aa077638b..e566feb00 100644 --- a/src/ws/identify.ts +++ b/src/ws/identify.ts @@ -34,6 +34,7 @@ export async function identify(shardId: number, maxShards: number) { queue: [], processingQueue: false, queueStartedAt: Date.now(), + queueCounter: 0, }); socket.onopen = () => { diff --git a/src/ws/process_queue.ts b/src/ws/process_queue.ts index 218f189a4..5a87770ac 100644 --- a/src/ws/process_queue.ts +++ b/src/ws/process_queue.ts @@ -8,8 +8,6 @@ export async function processQueue(id: number) { shard.processingQueue = true; - let counter = 0; - while (shard.queue.length) { if (shard.ws.readyState !== WebSocket.OPEN) { shard.processingQueue = false; @@ -19,7 +17,7 @@ export async function processQueue(id: number) { const now = Date.now(); if (now - shard.queueStartedAt >= 60000) { shard.queueStartedAt = now; - counter = 0; + shard.queueCounter = 0; } // Send a request that is next in line @@ -28,12 +26,12 @@ export async function processQueue(id: number) { shard.ws.send(JSON.stringify(request)); // Counter is useful for preventing 120/m requests. - counter++; + shard.queueCounter++; // Handle if the requests have been maxed - if (counter >= 118) { + if (shard.queueCounter >= 118) { await delay(60000); - counter = 0; + shard.queueCounter = 0; continue; } } diff --git a/src/ws/ws.ts b/src/ws/ws.ts index 7af2baa8b..2b3006b1e 100644 --- a/src/ws/ws.ts +++ b/src/ws/ws.ts @@ -1,4 +1,4 @@ -import { DiscordGatewayPayload } from "../types/gateway/gateway_payload.ts"; +import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts"; import { Collection } from "../util/collection.ts"; import { cleanupLoadingShards } from "./cleanup_loading_shards.ts"; import { createShard } from "./create_shard.ts"; @@ -7,12 +7,11 @@ import { handleDiscordPayload } from "./handle_discord_payload.ts"; import { handleOnMessage } from "./handle_on_message.ts"; import { heartbeat } from "./heartbeat.ts"; import { identify } from "./identify.ts"; +import { processQueue } from "./process_queue.ts"; import { resharder } from "./resharder.ts"; import { spawnShards } from "./spawn_shards.ts"; import { startGateway } from "./start_gateway.ts"; -import { processQueue } from "./process_queue.ts"; import { tellClusterToIdentify } from "./tell_cluster_to_identify.ts"; -import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts"; // CONTROLLER LIKE INTERFACE FOR WS HANDLING export const ws = { @@ -147,6 +146,8 @@ export interface DiscordenoShard { processingQueue: boolean; /** When the first request for this minute has been sent. */ queueStartedAt: number; + /** The request counter of the queue */ + queueCounter: number; } export interface WebSocketRequest {