mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 03:53:29 +01:00
feat: @discordjs/ws (#8260)
Co-authored-by: Parbez <imranbarbhuiya.fsd@gmail.com>
This commit is contained in:
29
packages/ws/src/utils/IdentifyThrottler.ts
Normal file
29
packages/ws/src/utils/IdentifyThrottler.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { setTimeout as sleep } from 'node:timers/promises';
|
||||
import type { WebSocketManager } from '../ws/WebSocketManager';
|
||||
|
||||
export class IdentifyThrottler {
|
||||
private identifyState = {
|
||||
remaining: 0,
|
||||
resetsAt: Infinity,
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
const info = await this.manager.fetchGatewayInformation();
|
||||
this.identifyState = {
|
||||
remaining: info.session_start_limit.max_concurrency,
|
||||
resetsAt: Date.now() + 5_000,
|
||||
};
|
||||
}
|
||||
|
||||
this.identifyState.remaining--;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user