mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 03:23:29 +01:00
feature: allow sweeping of inactive request buckets
This commit is contained in:
@@ -11,19 +11,6 @@ class BaseClient extends EventEmitter {
|
|||||||
constructor(options = {}) {
|
constructor(options = {}) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
/**
|
|
||||||
* The options the client was instantiated with
|
|
||||||
* @type {ClientOptions}
|
|
||||||
*/
|
|
||||||
this.options = Util.mergeDefault(DefaultOptions, options);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The REST manager of the client
|
|
||||||
* @type {RESTManager}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
this.rest = new RESTManager(this, options._tokenType);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Timeouts set by {@link BaseClient#setTimeout} that are still active
|
* Timeouts set by {@link BaseClient#setTimeout} that are still active
|
||||||
* @type {Set<Timeout>}
|
* @type {Set<Timeout>}
|
||||||
@@ -37,6 +24,19 @@ class BaseClient extends EventEmitter {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
this._intervals = new Set();
|
this._intervals = new Set();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The options the client was instantiated with
|
||||||
|
* @type {ClientOptions}
|
||||||
|
*/
|
||||||
|
this.options = Util.mergeDefault(DefaultOptions, options);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The REST manager of the client
|
||||||
|
* @type {RESTManager}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
this.rest = new RESTManager(this, options._tokenType);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -468,6 +468,9 @@ class Client extends BaseClient {
|
|||||||
if (typeof options.restWsBridgeTimeout !== 'number' || isNaN(options.restWsBridgeTimeout)) {
|
if (typeof options.restWsBridgeTimeout !== 'number' || isNaN(options.restWsBridgeTimeout)) {
|
||||||
throw new TypeError('CLIENT_INVALID_OPTION', 'restWsBridgeTimeout', 'a number');
|
throw new TypeError('CLIENT_INVALID_OPTION', 'restWsBridgeTimeout', 'a number');
|
||||||
}
|
}
|
||||||
|
if (typeof options.restSweepInterval !== 'number' || isNaN(options.restSweepInterval)) {
|
||||||
|
throw new TypeError('CLIENT_INVALID_OPTION', 'restSweepInterval', 'a number');
|
||||||
|
}
|
||||||
if (typeof options.internalSharding !== 'boolean') {
|
if (typeof options.internalSharding !== 'boolean') {
|
||||||
throw new TypeError('CLIENT_INVALID_OPTION', 'internalSharding', 'a boolean');
|
throw new TypeError('CLIENT_INVALID_OPTION', 'internalSharding', 'a boolean');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,15 @@ class RESTManager {
|
|||||||
this.tokenPrefix = tokenPrefix;
|
this.tokenPrefix = tokenPrefix;
|
||||||
this.versioned = true;
|
this.versioned = true;
|
||||||
this.timeDifferences = [];
|
this.timeDifferences = [];
|
||||||
|
if (client.options.restSweepInterval > 0) {
|
||||||
|
client.setInterval(() => {
|
||||||
|
for (const handler in this.handlers) {
|
||||||
|
if (this.handlers[handler] && this.handlers[handler]._inactive) {
|
||||||
|
this.handlers[handler] = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, client.options.restSweepInterval * 1000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get api() {
|
get api() {
|
||||||
|
|||||||
@@ -26,6 +26,13 @@ class RequestHandler {
|
|||||||
this.handle();
|
this.handle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get _inactive() {
|
||||||
|
return this.queue.length === 0 &&
|
||||||
|
!this.limited &&
|
||||||
|
Date.now() > this.resetTime &&
|
||||||
|
(typeof this.busy === 'undefined' || this.busy === false);
|
||||||
|
}
|
||||||
|
|
||||||
execute(item) {
|
execute(item) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const finish = timeout => {
|
const finish = timeout => {
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ const browser = exports.browser = typeof window !== 'undefined';
|
|||||||
* corresponding websocket events
|
* corresponding websocket events
|
||||||
* @property {number} [restTimeOffset=500] Extra time in millseconds to wait before continuing to make REST
|
* @property {number} [restTimeOffset=500] Extra time in millseconds to wait before continuing to make REST
|
||||||
* requests (higher values will reduce rate-limiting errors on bad connections)
|
* requests (higher values will reduce rate-limiting errors on bad connections)
|
||||||
|
* @property {number} [restSweepInterval=60] How frequently to delete inactive request buckets, in seconds
|
||||||
|
* (or 0 for never)
|
||||||
* @property {PresenceData} [presence] Presence data to use upon login
|
* @property {PresenceData} [presence] Presence data to use upon login
|
||||||
* @property {WSEventType[]} [disabledEvents] An array of disabled websocket events. Events in this array will not be
|
* @property {WSEventType[]} [disabledEvents] An array of disabled websocket events. Events in this array will not be
|
||||||
* processed, potentially resulting in performance improvements for larger bots. Only disable events you are
|
* processed, potentially resulting in performance improvements for larger bots. Only disable events you are
|
||||||
@@ -48,6 +50,7 @@ exports.DefaultOptions = {
|
|||||||
restWsBridgeTimeout: 5000,
|
restWsBridgeTimeout: 5000,
|
||||||
disabledEvents: [],
|
disabledEvents: [],
|
||||||
restTimeOffset: 500,
|
restTimeOffset: 500,
|
||||||
|
restSweepInterval: 60,
|
||||||
presence: {},
|
presence: {},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user