fix: RequestManager getting stuck on global ratelimit

fixes #2550
This commit is contained in:
Lewdcario
2018-05-13 14:17:26 -05:00
parent a732402c95
commit 75747f5b18
3 changed files with 30 additions and 23 deletions

View File

@@ -14,11 +14,7 @@ class RequestHandler {
} }
get limited() { get limited() {
return this.manager.globallyRateLimited || this.remaining <= 0; return (this.manager.globallyRateLimited || this.remaining <= 0) && Date.now() < this.resetTime;
}
set globallyLimited(limited) {
this.manager.globallyRateLimited = limited;
} }
push(request) { push(request) {
@@ -30,15 +26,25 @@ class RequestHandler {
return this.queue.length === 0 && !this.limited && Date.now() > this.resetTime && this.busy !== true; return this.queue.length === 0 && !this.limited && Date.now() > this.resetTime && this.busy !== true;
} }
/* eslint-disable prefer-promise-reject-errors */
execute(item) { execute(item) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const finish = timeout => { const finish = timeout => {
if (timeout || this.limited) { if (timeout || this.limited) {
if (!timeout) { if (!timeout) {
timeout = this.resetTime - Date.now() + this.manager.timeDifference + this.client.options.restTimeOffset; timeout = this.resetTime - Date.now() + this.client.options.restTimeOffset;
}
if (!this.manager.globalTimeout && this.manager.globallyRateLimited) {
this.manager.globalTimeout = setTimeout(() => {
this.manager.globalTimeout = undefined;
this.manager.globallyRateLimited = false;
this.busy = false;
this.handle();
}, timeout);
reject({ });
} else {
reject({ timeout });
} }
// eslint-disable-next-line prefer-promise-reject-errors
reject({ timeout });
if (this.client.listenerCount(RATE_LIMIT)) { if (this.client.listenerCount(RATE_LIMIT)) {
/** /**
* Emitted when the client hits a rate limit while making a request * Emitted when the client hits a rate limit while making a request
@@ -46,7 +52,6 @@ class RequestHandler {
* @param {Object} rateLimitInfo Object containing the rate limit info * @param {Object} rateLimitInfo Object containing the rate limit info
* @param {number} rateLimitInfo.timeout Timeout in ms * @param {number} rateLimitInfo.timeout Timeout in ms
* @param {number} rateLimitInfo.limit Number of requests that can be made to this endpoint * @param {number} rateLimitInfo.limit Number of requests that can be made to this endpoint
* @param {number} rateLimitInfo.timeDifference Delta-T in ms between your system and Discord servers
* @param {string} rateLimitInfo.method HTTP method used for request that triggered this event * @param {string} rateLimitInfo.method HTTP method used for request that triggered this event
* @param {string} rateLimitInfo.path Path used for request that triggered this event * @param {string} rateLimitInfo.path Path used for request that triggered this event
* @param {string} rateLimitInfo.route Route used for request that triggered this event * @param {string} rateLimitInfo.route Route used for request that triggered this event
@@ -54,7 +59,6 @@ class RequestHandler {
this.client.emit(RATE_LIMIT, { this.client.emit(RATE_LIMIT, {
timeout, timeout,
limit: this.limit, limit: this.limit,
timeDifference: this.manager.timeDifference,
method: item.request.method, method: item.request.method,
path: item.request.path, path: item.request.path,
route: item.request.route, route: item.request.route,
@@ -66,11 +70,10 @@ class RequestHandler {
}; };
item.request.gen().end((err, res) => { item.request.gen().end((err, res) => {
if (res && res.headers) { if (res && res.headers) {
if (res.headers['x-ratelimit-global']) this.globallyLimited = true; if (res.headers['x-ratelimit-global']) this.manager.globallyRateLimited = true;
this.limit = Number(res.headers['x-ratelimit-limit']); this.limit = Number(res.headers['x-ratelimit-limit']);
this.resetTime = Number(res.headers['x-ratelimit-reset']) * 1000; this.resetTime = Date.now() + Number(res.headers['retry-after']);
this.remaining = Number(res.headers['x-ratelimit-remaining']); this.remaining = Number(res.headers['x-ratelimit-remaining']);
this.manager.timeDifference = Date.now() - new Date(res.headers.date).getTime();
} }
if (err) { if (err) {
if (err.status === 429) { if (err.status === 429) {
@@ -94,7 +97,7 @@ class RequestHandler {
} }
reset() { reset() {
this.globallyLimited = false; this.manager.globallyRateLimited = false;
this.remaining = 1; this.remaining = 1;
} }
} }

View File

@@ -3,10 +3,12 @@ module.exports = function burst() {
this.execute(this.queue.shift()) this.execute(this.queue.shift())
.then(this.handle.bind(this)) .then(this.handle.bind(this))
.catch(({ timeout }) => { .catch(({ timeout }) => {
this.client.setTimeout(() => { if (timeout) {
this.reset(); this.client.setTimeout(() => {
this.handle(); this.reset();
}, timeout); this.handle();
}, timeout);
}
}); });
this.remaining--; this.remaining--;
this.handle(); this.handle();

View File

@@ -7,10 +7,12 @@ module.exports = function sequential() {
this.handle(); this.handle();
}) })
.catch(({ timeout }) => { .catch(({ timeout }) => {
this.client.setTimeout(() => { if (timeout) {
this.reset(); this.client.setTimeout(() => {
this.busy = false; this.reset();
this.handle(); this.busy = false;
}, timeout); this.handle();
}, timeout);
}
}); });
}; };