mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
feat(WebSocketManager): let identify throw on depleted limits (#5283)
* feat(WebSocketManager): let identify throw on depleted limits * chore: remove WSM#sessionStartLimit
This commit is contained in:
@@ -93,16 +93,6 @@ class WebSocketManager extends EventEmitter {
|
||||
* @private
|
||||
*/
|
||||
this.reconnecting = false;
|
||||
|
||||
/**
|
||||
* The current session limit of the client
|
||||
* @private
|
||||
* @type {?Object}
|
||||
* @property {number} total Total number of identifies available
|
||||
* @property {number} remaining Number of identifies remaining
|
||||
* @property {number} reset_after Number of milliseconds after which the limit resets
|
||||
*/
|
||||
this.sessionStartLimit = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,9 +129,7 @@ class WebSocketManager extends EventEmitter {
|
||||
throw error.httpStatus === 401 ? invalidToken : error;
|
||||
});
|
||||
|
||||
this.sessionStartLimit = sessionStartLimit;
|
||||
|
||||
const { total, remaining, reset_after } = sessionStartLimit;
|
||||
const { total, remaining } = sessionStartLimit;
|
||||
|
||||
this.debug(`Fetched Gateway Information
|
||||
URL: ${gatewayURL}
|
||||
@@ -165,8 +153,6 @@ class WebSocketManager extends EventEmitter {
|
||||
this.debug(`Spawning shards: ${shards.join(', ')}`);
|
||||
this.shardQueue = new Set(shards.map(id => new WebSocketShard(this, id)));
|
||||
|
||||
await this._handleSessionLimit(remaining, reset_after);
|
||||
|
||||
return this.createShards();
|
||||
}
|
||||
|
||||
@@ -226,7 +212,7 @@ class WebSocketManager extends EventEmitter {
|
||||
|
||||
if (shard.sessionID) {
|
||||
this.debug(`Session ID is present, attempting an immediate reconnect...`, shard);
|
||||
this.reconnect(true);
|
||||
this.reconnect();
|
||||
} else {
|
||||
shard.destroy({ reset: true, emit: false, log: false });
|
||||
this.reconnect();
|
||||
@@ -268,7 +254,6 @@ class WebSocketManager extends EventEmitter {
|
||||
if (this.shardQueue.size) {
|
||||
this.debug(`Shard Queue Size: ${this.shardQueue.size}; continuing in 5 seconds...`);
|
||||
await Util.delayFor(5000);
|
||||
await this._handleSessionLimit();
|
||||
return this.createShards();
|
||||
}
|
||||
|
||||
@@ -277,15 +262,13 @@ class WebSocketManager extends EventEmitter {
|
||||
|
||||
/**
|
||||
* Handles reconnects for this manager.
|
||||
* @param {boolean} [skipLimit=false] IF this reconnect should skip checking the session limit
|
||||
* @private
|
||||
* @returns {Promise<boolean>}
|
||||
*/
|
||||
async reconnect(skipLimit = false) {
|
||||
async reconnect() {
|
||||
if (this.reconnecting || this.status !== Status.READY) return false;
|
||||
this.reconnecting = true;
|
||||
try {
|
||||
if (!skipLimit) await this._handleSessionLimit();
|
||||
await this.createShards();
|
||||
} catch (error) {
|
||||
this.debug(`Couldn't reconnect or fetch information about the gateway. ${error}`);
|
||||
@@ -336,28 +319,6 @@ class WebSocketManager extends EventEmitter {
|
||||
for (const shard of this.shards.values()) shard.destroy({ closeCode: 1000, reset: true, emit: false, log: false });
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the timeout required if we cannot identify anymore.
|
||||
* @param {number} [remaining] The amount of remaining identify sessions that can be done today
|
||||
* @param {number} [resetAfter] The amount of time in which the identify counter resets
|
||||
* @private
|
||||
*/
|
||||
async _handleSessionLimit(remaining, resetAfter) {
|
||||
if (typeof remaining === 'undefined' && typeof resetAfter === 'undefined') {
|
||||
const { session_start_limit } = await this.client.api.gateway.bot.get();
|
||||
this.sessionStartLimit = session_start_limit;
|
||||
remaining = session_start_limit.remaining;
|
||||
resetAfter = session_start_limit.reset_after;
|
||||
this.debug(`Session Limit Information
|
||||
Total: ${session_start_limit.total}
|
||||
Remaining: ${remaining}`);
|
||||
}
|
||||
if (!remaining) {
|
||||
this.debug(`Exceeded identify threshold. Will attempt a connection in ${resetAfter}ms`);
|
||||
await Util.delayFor(resetAfter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes a packet and queues it if this WebSocketManager is not ready.
|
||||
* @param {Object} [packet] The packet to be handled
|
||||
|
||||
2
typings/index.d.ts
vendored
2
typings/index.d.ts
vendored
@@ -1772,7 +1772,6 @@ declare module 'discord.js' {
|
||||
private packetQueue: object[];
|
||||
private destroyed: boolean;
|
||||
private reconnecting: boolean;
|
||||
private sessionStartLimit: { total: number; remaining: number; reset_after: number } | null;
|
||||
|
||||
public readonly client: Client;
|
||||
public gateway: string | null;
|
||||
@@ -1789,7 +1788,6 @@ declare module 'discord.js' {
|
||||
private reconnect(): Promise<void>;
|
||||
private broadcast(packet: object): void;
|
||||
private destroy(): void;
|
||||
private _handleSessionLimit(remaining?: number, resetAfter?: number): Promise<void>;
|
||||
private handlePacket(packet?: object, shard?: WebSocketShard): boolean;
|
||||
private checkShardsReady(): void;
|
||||
private triggerClientReady(): void;
|
||||
|
||||
Reference in New Issue
Block a user