mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-19 21:13:30 +01:00
backport(ClientOptions): add retryLimit (#2869)
This commit is contained in:
@@ -509,7 +509,7 @@ class Client extends EventEmitter {
|
|||||||
* @param {ClientOptions} [options=this.options] Options to validate
|
* @param {ClientOptions} [options=this.options] Options to validate
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_validateOptions(options = this.options) {
|
_validateOptions(options = this.options) { // eslint-disable-line complexity
|
||||||
if (typeof options.shardCount !== 'number' || isNaN(options.shardCount)) {
|
if (typeof options.shardCount !== 'number' || isNaN(options.shardCount)) {
|
||||||
throw new TypeError('The shardCount option must be a number.');
|
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.');
|
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 (!(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.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ class RESTManager {
|
|||||||
}
|
}
|
||||||
reject(error);
|
reject(error);
|
||||||
},
|
},
|
||||||
|
retries: 0,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,11 +42,17 @@ class BurstRequestHandler extends RequestHandler {
|
|||||||
this.resetTimeout = null;
|
this.resetTimeout = null;
|
||||||
}, Number(res.headers['retry-after']) + this.client.options.restTimeOffset);
|
}, Number(res.headers['retry-after']) + this.client.options.restTimeOffset);
|
||||||
} else if (err.status >= 500 && err.status < 600) {
|
} else if (err.status >= 500 && err.status < 600) {
|
||||||
this.queue.unshift(item);
|
if (item.retries === this.client.options.retryLimit) {
|
||||||
this.resetTimeout = this.client.setTimeout(() => {
|
item.reject(err);
|
||||||
this.handle();
|
this.handle();
|
||||||
this.resetTimeout = null;
|
} else {
|
||||||
}, 1e3 + this.client.options.restTimeOffset);
|
item.retries++;
|
||||||
|
this.queue.unshift(item);
|
||||||
|
this.resetTimeout = this.client.setTimeout(() => {
|
||||||
|
this.handle();
|
||||||
|
this.resetTimeout = null;
|
||||||
|
}, 1e3 + this.client.options.restTimeOffset);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
item.reject(err.status >= 400 && err.status < 500 ?
|
item.reject(err.status >= 400 && err.status < 500 ?
|
||||||
new DiscordAPIError(res.request.path, res.body, res.request.method) : err);
|
new DiscordAPIError(res.request.path, res.body, res.request.method) : err);
|
||||||
|
|||||||
@@ -72,8 +72,14 @@ class SequentialRequestHandler extends RequestHandler {
|
|||||||
}, Number(res.headers['retry-after']) + this.client.options.restTimeOffset);
|
}, Number(res.headers['retry-after']) + this.client.options.restTimeOffset);
|
||||||
if (res.headers['x-ratelimit-global']) this.globalLimit = true;
|
if (res.headers['x-ratelimit-global']) this.globalLimit = true;
|
||||||
} else if (err.status >= 500 && err.status < 600) {
|
} else if (err.status >= 500 && err.status < 600) {
|
||||||
this.queue.unshift(item);
|
if (item.retries === this.client.options.retryLimit) {
|
||||||
this.client.setTimeout(resolve, 1e3 + this.client.options.restTimeOffset);
|
item.reject(err);
|
||||||
|
resolve();
|
||||||
|
} else {
|
||||||
|
item.retries++;
|
||||||
|
this.queue.unshift(item);
|
||||||
|
this.client.setTimeout(resolve, 1e3 + this.client.options.restTimeOffset);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
item.reject(err.status >= 400 && err.status < 500 ?
|
item.reject(err.status >= 400 && err.status < 500 ?
|
||||||
new DiscordAPIError(res.request.path, res.body, res.request.method) : err);
|
new DiscordAPIError(res.request.path, res.body, res.request.method) : err);
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ exports.Package = require('../../package.json');
|
|||||||
* 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} [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
|
* @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
|
||||||
* 100% certain you don't need, as many are important, but not obviously so. The safest one to disable with the
|
* 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,
|
disableEveryone: false,
|
||||||
sync: false,
|
sync: false,
|
||||||
restWsBridgeTimeout: 5000,
|
restWsBridgeTimeout: 5000,
|
||||||
|
retryLimit: Infinity,
|
||||||
disabledEvents: [],
|
disabledEvents: [],
|
||||||
restTimeOffset: 500,
|
restTimeOffset: 500,
|
||||||
|
|
||||||
|
|||||||
1
typings/index.d.ts
vendored
1
typings/index.d.ts
vendored
@@ -1647,6 +1647,7 @@ declare module 'discord.js' {
|
|||||||
sync?: boolean;
|
sync?: boolean;
|
||||||
restWsBridgeTimeout?: number;
|
restWsBridgeTimeout?: number;
|
||||||
restTimeOffset?: number;
|
restTimeOffset?: number;
|
||||||
|
retryLimit?: number;
|
||||||
disabledEvents?: WSEventType[];
|
disabledEvents?: WSEventType[];
|
||||||
ws?: WebSocketOptions;
|
ws?: WebSocketOptions;
|
||||||
http?: HTTPOptions;
|
http?: HTTPOptions;
|
||||||
|
|||||||
Reference in New Issue
Block a user