mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 19:43:29 +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
|
* @private
|
||||||
*/
|
*/
|
||||||
this.reconnecting = false;
|
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;
|
throw error.httpStatus === 401 ? invalidToken : error;
|
||||||
});
|
});
|
||||||
|
|
||||||
this.sessionStartLimit = sessionStartLimit;
|
const { total, remaining } = sessionStartLimit;
|
||||||
|
|
||||||
const { total, remaining, reset_after } = sessionStartLimit;
|
|
||||||
|
|
||||||
this.debug(`Fetched Gateway Information
|
this.debug(`Fetched Gateway Information
|
||||||
URL: ${gatewayURL}
|
URL: ${gatewayURL}
|
||||||
@@ -165,8 +153,6 @@ class WebSocketManager extends EventEmitter {
|
|||||||
this.debug(`Spawning shards: ${shards.join(', ')}`);
|
this.debug(`Spawning shards: ${shards.join(', ')}`);
|
||||||
this.shardQueue = new Set(shards.map(id => new WebSocketShard(this, id)));
|
this.shardQueue = new Set(shards.map(id => new WebSocketShard(this, id)));
|
||||||
|
|
||||||
await this._handleSessionLimit(remaining, reset_after);
|
|
||||||
|
|
||||||
return this.createShards();
|
return this.createShards();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,7 +212,7 @@ class WebSocketManager extends EventEmitter {
|
|||||||
|
|
||||||
if (shard.sessionID) {
|
if (shard.sessionID) {
|
||||||
this.debug(`Session ID is present, attempting an immediate reconnect...`, shard);
|
this.debug(`Session ID is present, attempting an immediate reconnect...`, shard);
|
||||||
this.reconnect(true);
|
this.reconnect();
|
||||||
} else {
|
} else {
|
||||||
shard.destroy({ reset: true, emit: false, log: false });
|
shard.destroy({ reset: true, emit: false, log: false });
|
||||||
this.reconnect();
|
this.reconnect();
|
||||||
@@ -268,7 +254,6 @@ class WebSocketManager extends EventEmitter {
|
|||||||
if (this.shardQueue.size) {
|
if (this.shardQueue.size) {
|
||||||
this.debug(`Shard Queue Size: ${this.shardQueue.size}; continuing in 5 seconds...`);
|
this.debug(`Shard Queue Size: ${this.shardQueue.size}; continuing in 5 seconds...`);
|
||||||
await Util.delayFor(5000);
|
await Util.delayFor(5000);
|
||||||
await this._handleSessionLimit();
|
|
||||||
return this.createShards();
|
return this.createShards();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -277,15 +262,13 @@ class WebSocketManager extends EventEmitter {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles reconnects for this manager.
|
* Handles reconnects for this manager.
|
||||||
* @param {boolean} [skipLimit=false] IF this reconnect should skip checking the session limit
|
|
||||||
* @private
|
* @private
|
||||||
* @returns {Promise<boolean>}
|
* @returns {Promise<boolean>}
|
||||||
*/
|
*/
|
||||||
async reconnect(skipLimit = false) {
|
async reconnect() {
|
||||||
if (this.reconnecting || this.status !== Status.READY) return false;
|
if (this.reconnecting || this.status !== Status.READY) return false;
|
||||||
this.reconnecting = true;
|
this.reconnecting = true;
|
||||||
try {
|
try {
|
||||||
if (!skipLimit) await this._handleSessionLimit();
|
|
||||||
await this.createShards();
|
await this.createShards();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.debug(`Couldn't reconnect or fetch information about the gateway. ${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 });
|
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.
|
* Processes a packet and queues it if this WebSocketManager is not ready.
|
||||||
* @param {Object} [packet] The packet to be handled
|
* @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 packetQueue: object[];
|
||||||
private destroyed: boolean;
|
private destroyed: boolean;
|
||||||
private reconnecting: boolean;
|
private reconnecting: boolean;
|
||||||
private sessionStartLimit: { total: number; remaining: number; reset_after: number } | null;
|
|
||||||
|
|
||||||
public readonly client: Client;
|
public readonly client: Client;
|
||||||
public gateway: string | null;
|
public gateway: string | null;
|
||||||
@@ -1789,7 +1788,6 @@ declare module 'discord.js' {
|
|||||||
private reconnect(): Promise<void>;
|
private reconnect(): Promise<void>;
|
||||||
private broadcast(packet: object): void;
|
private broadcast(packet: object): void;
|
||||||
private destroy(): void;
|
private destroy(): void;
|
||||||
private _handleSessionLimit(remaining?: number, resetAfter?: number): Promise<void>;
|
|
||||||
private handlePacket(packet?: object, shard?: WebSocketShard): boolean;
|
private handlePacket(packet?: object, shard?: WebSocketShard): boolean;
|
||||||
private checkShardsReady(): void;
|
private checkShardsReady(): void;
|
||||||
private triggerClientReady(): void;
|
private triggerClientReady(): void;
|
||||||
|
|||||||
Reference in New Issue
Block a user