diff --git a/src/client/rest/RESTManager.js b/src/client/rest/RESTManager.js index 8a1b0f9e5..87731fca3 100644 --- a/src/client/rest/RESTManager.js +++ b/src/client/rest/RESTManager.js @@ -12,6 +12,7 @@ class RESTManager { this.userAgentManager = new UserAgentManager(this); this.methods = new RESTMethods(this); this.rateLimitedEndpoints = {}; + this.globallyRateLimited = false; } push(handler, apiRequest) { diff --git a/src/client/rest/RequestHandlers/RequestHandler.js b/src/client/rest/RequestHandlers/RequestHandler.js index 32c21234f..8b88bfe63 100644 --- a/src/client/rest/RequestHandlers/RequestHandler.js +++ b/src/client/rest/RequestHandlers/RequestHandler.js @@ -17,6 +17,18 @@ module.exports = class RequestHandler { this.queue = []; } + /** + * Whether or not the client is being rate limited on every endpoint. + * @type {Boolean} + */ + get globalLimit() { + return this.restManager.globallyRateLimited; + } + + set globalLimit(value) { + this.restManager.globallyRateLimited = value; + } + /** * Push a new API request into this bucket * @param {APIRequest} request the new request to push into the queue diff --git a/src/client/rest/RequestHandlers/Sequential.js b/src/client/rest/RequestHandlers/Sequential.js index c66651918..940da844b 100644 --- a/src/client/rest/RequestHandlers/Sequential.js +++ b/src/client/rest/RequestHandlers/Sequential.js @@ -36,7 +36,7 @@ module.exports = class SequentialRequestHandler extends RequestHandler { * @returns {Promise} */ execute(item) { - return new Promise((resolve, reject) => { + return new Promise(resolve => { item.request.gen().end((err, res) => { if (res && res.headers) { this.requestLimit = res.headers['x-ratelimit-limit']; @@ -48,8 +48,12 @@ module.exports = class SequentialRequestHandler extends RequestHandler { if (err.status === 429) { setTimeout(() => { this.waiting = false; + this.globalLimit = false; resolve(); - }, res.headers['retry-after']); + }, res.headers['retry-after'] + 500); + if (res.headers['x-ratelimit-global']) { + this.globalLimit = true; + } } else { this.queue.shift(); this.waiting = false; @@ -58,6 +62,7 @@ module.exports = class SequentialRequestHandler extends RequestHandler { } } else { this.queue.shift(); + this.globalLimit = false; const data = res && res.body ? res.body : {}; item.resolve(data); if (this.requestRemaining === 0) { @@ -76,7 +81,7 @@ module.exports = class SequentialRequestHandler extends RequestHandler { handle() { super.handle(); - if (this.waiting || this.queue.length === 0) { + if (this.waiting || this.queue.length === 0 || this.globalLimit) { return; } this.waiting = true;