mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 17:13:31 +01:00
refactor(WebSocketShard): identify throttling (#8888)
* refactor(WebSocketShard): identify throttling * chore: add worker handling * refactor: worker handling * chore: update tests * chore: use satisfies where applicable * chore: add informative comment * chore: apply suggestions * refactor(SimpleContextFetchingStrategy): support multiple managers
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import { setTimeout as sleep } from 'node:timers/promises';
|
||||
import type { WebSocketManager } from '../ws/WebSocketManager';
|
||||
import { AsyncQueue } from '@sapphire/async-queue';
|
||||
import type { WebSocketManager } from '../ws/WebSocketManager.js';
|
||||
|
||||
export class IdentifyThrottler {
|
||||
private readonly queue = new AsyncQueue();
|
||||
|
||||
private identifyState = {
|
||||
remaining: 0,
|
||||
resetsAt: Number.POSITIVE_INFINITY,
|
||||
@@ -10,20 +13,27 @@ export class IdentifyThrottler {
|
||||
public constructor(private readonly manager: WebSocketManager) {}
|
||||
|
||||
public async waitForIdentify(): Promise<void> {
|
||||
if (this.identifyState.remaining <= 0) {
|
||||
const diff = this.identifyState.resetsAt - Date.now();
|
||||
if (diff <= 5_000) {
|
||||
const time = diff + Math.random() * 1_500;
|
||||
await sleep(time);
|
||||
await this.queue.wait();
|
||||
|
||||
try {
|
||||
if (this.identifyState.remaining <= 0) {
|
||||
const diff = this.identifyState.resetsAt - Date.now();
|
||||
if (diff <= 5_000) {
|
||||
// To account for the latency the IDENTIFY payload goes through, we add a bit more wait time
|
||||
const time = diff + Math.random() * 1_500;
|
||||
await sleep(time);
|
||||
}
|
||||
|
||||
const info = await this.manager.fetchGatewayInformation();
|
||||
this.identifyState = {
|
||||
remaining: info.session_start_limit.max_concurrency,
|
||||
resetsAt: Date.now() + 5_000,
|
||||
};
|
||||
}
|
||||
|
||||
const info = await this.manager.fetchGatewayInformation();
|
||||
this.identifyState = {
|
||||
remaining: info.session_start_limit.max_concurrency,
|
||||
resetsAt: Date.now() + 5_000,
|
||||
};
|
||||
this.identifyState.remaining--;
|
||||
} finally {
|
||||
this.queue.shift();
|
||||
}
|
||||
|
||||
this.identifyState.remaining--;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user