backport(ClientOptions): add retryLimit (#2869)

This commit is contained in:
SpaceEEC
2018-10-04 00:20:53 +02:00
committed by Isabella
parent 9de3e098da
commit e0f522a745
6 changed files with 27 additions and 7 deletions

View File

@@ -509,7 +509,7 @@ class Client extends EventEmitter {
* @param {ClientOptions} [options=this.options] Options to validate
* @private
*/
_validateOptions(options = this.options) {
_validateOptions(options = this.options) { // eslint-disable-line complexity
if (typeof options.shardCount !== 'number' || isNaN(options.shardCount)) {
throw new TypeError('The shardCount option must be a number.');
}
@@ -540,6 +540,9 @@ class Client extends EventEmitter {
throw new TypeError('The restWsBridgeTimeout option must be a number.');
}
if (!(options.disabledEvents instanceof Array)) throw new TypeError('The disabledEvents option must be an Array.');
if (typeof options.retryLimit !== 'number' || isNaN(options.retryLimit)) {
throw new TypeError('The retryLimit options must be a number.');
}
}
}

View File

@@ -42,6 +42,7 @@ class RESTManager {
}
reject(error);
},
retries: 0,
});
});
}

View File

@@ -42,11 +42,17 @@ class BurstRequestHandler extends RequestHandler {
this.resetTimeout = null;
}, Number(res.headers['retry-after']) + this.client.options.restTimeOffset);
} else if (err.status >= 500 && err.status < 600) {
this.queue.unshift(item);
this.resetTimeout = this.client.setTimeout(() => {
if (item.retries === this.client.options.retryLimit) {
item.reject(err);
this.handle();
this.resetTimeout = null;
}, 1e3 + this.client.options.restTimeOffset);
} else {
item.retries++;
this.queue.unshift(item);
this.resetTimeout = this.client.setTimeout(() => {
this.handle();
this.resetTimeout = null;
}, 1e3 + this.client.options.restTimeOffset);
}
} else {
item.reject(err.status >= 400 && err.status < 500 ?
new DiscordAPIError(res.request.path, res.body, res.request.method) : err);

View File

@@ -72,8 +72,14 @@ class SequentialRequestHandler extends RequestHandler {
}, Number(res.headers['retry-after']) + this.client.options.restTimeOffset);
if (res.headers['x-ratelimit-global']) this.globalLimit = true;
} else if (err.status >= 500 && err.status < 600) {
this.queue.unshift(item);
this.client.setTimeout(resolve, 1e3 + this.client.options.restTimeOffset);
if (item.retries === this.client.options.retryLimit) {
item.reject(err);
resolve();
} else {
item.retries++;
this.queue.unshift(item);
this.client.setTimeout(resolve, 1e3 + this.client.options.restTimeOffset);
}
} else {
item.reject(err.status >= 400 && err.status < 500 ?
new DiscordAPIError(res.request.path, res.body, res.request.method) : err);

View File

@@ -24,6 +24,8 @@ exports.Package = require('../../package.json');
* corresponding websocket events
* @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)
* @property {number} [retryLimit=Infinity] How many times to retry on 5XX errors
* (Infinity for indefinite amount of retries)
* @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
* 100% certain you don't need, as many are important, but not obviously so. The safest one to disable with the
@@ -42,6 +44,7 @@ exports.DefaultOptions = {
disableEveryone: false,
sync: false,
restWsBridgeTimeout: 5000,
retryLimit: Infinity,
disabledEvents: [],
restTimeOffset: 500,

1
typings/index.d.ts vendored
View File

@@ -1647,6 +1647,7 @@ declare module 'discord.js' {
sync?: boolean;
restWsBridgeTimeout?: number;
restTimeOffset?: number;
retryLimit?: number;
disabledEvents?: WSEventType[];
ws?: WebSocketOptions;
http?: HTTPOptions;