From 4c097d4e171632971eae6852ea21e88cc4ee42e5 Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Wed, 14 Apr 2021 19:10:55 +0200 Subject: [PATCH 1/5] add queueMinuteTimestamp --- src/ws/identify.ts | 1 + src/ws/process_queue.ts | 7 +++++++ src/ws/spawn_shards.ts | 2 +- src/ws/ws.ts | 2 ++ 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/ws/identify.ts b/src/ws/identify.ts index b64be1a21..6a6486195 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, + queueMinuteTimestamp: 0, }); socket.onopen = () => { diff --git a/src/ws/process_queue.ts b/src/ws/process_queue.ts index 7a37b3ead..3737e58b1 100644 --- a/src/ws/process_queue.ts +++ b/src/ws/process_queue.ts @@ -7,6 +7,7 @@ export async function processQueue(id: number) { if (!shard?.queue.length || shard.processingQueue) return; shard.processingQueue = true; + shard.queueMinuteTimestamp = Date.now(); let counter = 0; @@ -16,6 +17,12 @@ export async function processQueue(id: number) { return; } + const now = Date.now(); + if (now - shard.queueMinuteTimestamp > 60000) { + shard.queueMinuteTimestamp = now; + counter = 0; + } + // Send a request that is next in line const request = shard.queue.shift(); diff --git a/src/ws/spawn_shards.ts b/src/ws/spawn_shards.ts index d82c15d88..437556af6 100644 --- a/src/ws/spawn_shards.ts +++ b/src/ws/spawn_shards.ts @@ -61,7 +61,7 @@ export function spawnShards(firstShardId = 0) { while (shardId !== undefined) { ws.log("DEBUG", "Running while loop in getMembers function."); - await ws.tellClusterToIdentify(clusterId as number, shardId, bucketId); + await ws.tellClusterToIdentify(clusterId, shardId, bucketId); shardId = queue.shift(); } } diff --git a/src/ws/ws.ts b/src/ws/ws.ts index 1db6c8a74..22fc68d68 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. */ + queueMinuteTimestamp: number; } export interface WebSocketRequest { From 1019a05cc420473467a8fae85d3c5955fd8beabb Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Wed, 14 Apr 2021 19:23:26 +0200 Subject: [PATCH 2/5] bug --- src/ws/identify.ts | 2 +- src/ws/process_queue.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ws/identify.ts b/src/ws/identify.ts index 6a6486195..eefe04226 100644 --- a/src/ws/identify.ts +++ b/src/ws/identify.ts @@ -33,7 +33,7 @@ export async function identify(shardId: number, maxShards: number) { }, queue: [], processingQueue: false, - queueMinuteTimestamp: 0, + queueMinuteTimestamp: Date.now(), }); socket.onopen = () => { diff --git a/src/ws/process_queue.ts b/src/ws/process_queue.ts index 3737e58b1..3396f86b4 100644 --- a/src/ws/process_queue.ts +++ b/src/ws/process_queue.ts @@ -7,7 +7,6 @@ export async function processQueue(id: number) { if (!shard?.queue.length || shard.processingQueue) return; shard.processingQueue = true; - shard.queueMinuteTimestamp = Date.now(); let counter = 0; From 6a0f2fdef527d155d83fe0faca631d302c99532d Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Wed, 14 Apr 2021 20:59:51 +0200 Subject: [PATCH 3/5] Update resume.ts --- src/ws/resume.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ws/resume.ts b/src/ws/resume.ts index 4c35ed60e..c8d2e1d16 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, + queueMinuteTimestamp: Date.now(), }); // Resume on open From 3f77e8e9e13fd8368e4277c8c9c0edd1eec002cb Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Wed, 14 Apr 2021 21:00:11 +0200 Subject: [PATCH 4/5] Update process_queue.ts --- src/ws/process_queue.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ws/process_queue.ts b/src/ws/process_queue.ts index 3396f86b4..2782d6447 100644 --- a/src/ws/process_queue.ts +++ b/src/ws/process_queue.ts @@ -17,7 +17,7 @@ export async function processQueue(id: number) { } const now = Date.now(); - if (now - shard.queueMinuteTimestamp > 60000) { + if (now - shard.queueMinuteTimestamp >= 60000) { shard.queueMinuteTimestamp = now; counter = 0; } From 07587f42a1ebf8f3600f2eddfb1e233535be5b38 Mon Sep 17 00:00:00 2001 From: ITOH <72305210+itohatweb@users.noreply.github.com> Date: Wed, 14 Apr 2021 21:05:40 +0200 Subject: [PATCH 5/5] queueStartedAt --- src/ws/identify.ts | 2 +- src/ws/process_queue.ts | 4 ++-- src/ws/resume.ts | 2 +- src/ws/ws.ts | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ws/identify.ts b/src/ws/identify.ts index eefe04226..aa077638b 100644 --- a/src/ws/identify.ts +++ b/src/ws/identify.ts @@ -33,7 +33,7 @@ export async function identify(shardId: number, maxShards: number) { }, queue: [], processingQueue: false, - queueMinuteTimestamp: Date.now(), + queueStartedAt: Date.now(), }); socket.onopen = () => { diff --git a/src/ws/process_queue.ts b/src/ws/process_queue.ts index 2782d6447..218f189a4 100644 --- a/src/ws/process_queue.ts +++ b/src/ws/process_queue.ts @@ -17,8 +17,8 @@ export async function processQueue(id: number) { } const now = Date.now(); - if (now - shard.queueMinuteTimestamp >= 60000) { - shard.queueMinuteTimestamp = now; + if (now - shard.queueStartedAt >= 60000) { + shard.queueStartedAt = now; counter = 0; } diff --git a/src/ws/resume.ts b/src/ws/resume.ts index c8d2e1d16..e448b5e45 100644 --- a/src/ws/resume.ts +++ b/src/ws/resume.ts @@ -43,7 +43,7 @@ export async function resume(shardId: number) { }, queue: oldShard?.queue || [], processingQueue: false, - queueMinuteTimestamp: Date.now(), + queueStartedAt: Date.now(), }); // Resume on open diff --git a/src/ws/ws.ts b/src/ws/ws.ts index 22fc68d68..7af2baa8b 100644 --- a/src/ws/ws.ts +++ b/src/ws/ws.ts @@ -146,7 +146,7 @@ export interface DiscordenoShard { /** Whether or not the queue for this shard is being processed. */ processingQueue: boolean; /** When the first request for this minute has been sent. */ - queueMinuteTimestamp: number; + queueStartedAt: number; } export interface WebSocketRequest {