diff --git a/src/ws/identify.ts b/src/ws/identify.ts index b64be1a21..aa077638b 100644 --- a/src/ws/identify.ts +++ b/src/ws/identify.ts @@ -33,6 +33,7 @@ export async function identify(shardId: number, maxShards: number) { }, queue: [], processingQueue: false, + queueStartedAt: Date.now(), }); socket.onopen = () => { diff --git a/src/ws/process_queue.ts b/src/ws/process_queue.ts index 7a37b3ead..218f189a4 100644 --- a/src/ws/process_queue.ts +++ b/src/ws/process_queue.ts @@ -16,6 +16,12 @@ export async function processQueue(id: number) { return; } + const now = Date.now(); + if (now - shard.queueStartedAt >= 60000) { + shard.queueStartedAt = now; + counter = 0; + } + // Send a request that is next in line const request = shard.queue.shift(); diff --git a/src/ws/resume.ts b/src/ws/resume.ts index 4c35ed60e..e448b5e45 100644 --- a/src/ws/resume.ts +++ b/src/ws/resume.ts @@ -12,8 +12,11 @@ export async function resume(shardId: number) { const oldShard = ws.shards.get(shardId); if (oldShard) { - // HOW TO CLOSE OLD SHARD SOCKET!!! - oldShard.ws.close(3065, "Resuming the shard, closing old shard."); + // ONLY CLOSE IF SHARD SOCKET IS STILL CONNECTED + if (oldShard.ws.readyState === WebSocket.OPEN) { + // HOW TO CLOSE OLD SHARD SOCKET!!! + oldShard.ws.close(3065, "Resuming the shard, closing old shard."); + } // STOP OLD HEARTBEAT clearInterval(oldShard.heartbeat.intervalId); } @@ -40,6 +43,7 @@ export async function resume(shardId: number) { }, queue: oldShard?.queue || [], processingQueue: false, + queueStartedAt: Date.now(), }); // Resume on open diff --git a/src/ws/ws.ts b/src/ws/ws.ts index 1db6c8a74..7af2baa8b 100644 --- a/src/ws/ws.ts +++ b/src/ws/ws.ts @@ -145,6 +145,8 @@ export interface DiscordenoShard { queue: WebSocketRequest[]; /** Whether or not the queue for this shard is being processed. */ processingQueue: boolean; + /** When the first request for this minute has been sent. */ + queueStartedAt: number; } export interface WebSocketRequest {