fix: don't reset ws.processQueue counter on every new loop

This commit is contained in:
ITOH
2021-04-15 09:14:40 +02:00
parent 6d497b1277
commit f3f7eacc1c
3 changed files with 9 additions and 9 deletions

View File

@@ -34,6 +34,7 @@ export async function identify(shardId: number, maxShards: number) {
queue: [],
processingQueue: false,
queueStartedAt: Date.now(),
queueCounter: 0,
});
socket.onopen = () => {

View File

@@ -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;
}
}

View File

@@ -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 {