Skip to content

Commit

Permalink
feat: add ClientOptions#retryLimit (#2805)
Browse files Browse the repository at this point in the history
* feat: add ClientOptions#retryLimit

* hydra needs to learn how to code right

* a default would probably help

* move incrementor & update comment

* clarify docs on Infinity
  • Loading branch information
Lewdcario authored and iCrawl committed Sep 1, 2018
1 parent 3970c50 commit be4d6f9
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ class Client extends BaseClient {
if (!(options.disabledEvents instanceof Array)) {
throw new TypeError('CLIENT_INVALID_OPTION', 'disabledEvents', 'an Array');
}
if (typeof options.retryLimit !== 'number' || isNaN(options.retryLimit)) {
throw new TypeError('CLIENT_INVALID_OPTION', 'retryLimit', 'a number');
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/rest/RESTManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class RESTManager {
request: apiRequest,
resolve,
reject,
retries: 0,
}).catch(reject);
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/rest/RequestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ class RequestHandler {
await Util.delayFor(this.retryAfter);
return this.run();
} else if (res.status >= 500 && res.status < 600) {
// Retry once for possible serverside issues
if (item.retried) {
// Retry the specified number of times for possible serverside issues
if (item.retries === this.manager.client.options.retryLimit) {
return reject(
new HTTPError(res.statusText, res.constructor.name, res.status, item.request.method, request.route)
);
} else {
item.retried = true;
item.retries++;
this.queue.unshift(item);
return this.run();
}
Expand Down
2 changes: 2 additions & 0 deletions src/util/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const browser = exports.browser = typeof window !== 'undefined';
* 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 {number} [retryLimit=1] How many times to retry on 5XX errors (Infinity for indefinite amount of retries)
* @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
* processed, potentially resulting in performance improvements for larger bots. Only disable events you are
Expand All @@ -42,6 +43,7 @@ exports.DefaultOptions = {
disableEveryone: false,
restWsBridgeTimeout: 5000,
disabledEvents: [],
retryLimit: 1,
restTimeOffset: 500,
restSweepInterval: 60,
presence: {},
Expand Down
1 change: 1 addition & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,7 @@ declare module 'discord.js' {
disableEveryone?: boolean;
restWsBridgeTimeout?: number;
restTimeOffset?: number;
retryLimit?: number,
disabledEvents?: WSEventType[];
ws?: WebSocketOptions;
http?: HTTPOptions;
Expand Down

0 comments on commit be4d6f9

Please sign in to comment.