Skip to content

Commit

Permalink
feat(WebSocketManager): let identify throw on depleted limits (#5283)
Browse files Browse the repository at this point in the history
* feat(WebSocketManager): let identify throw on depleted limits

* chore: remove WSM#sessionStartLimit
  • Loading branch information
almostSouji authored Mar 28, 2021
1 parent 8d14ee3 commit 624a446
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 44 deletions.
45 changes: 3 additions & 42 deletions src/client/websocket/WebSocketManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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}
Expand All @@ -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();
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}

Expand All @@ -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}`);
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 624a446

Please sign in to comment.