fix: ratelimits (#2615)

* Add timeDifference calcs back into ratelimits

And fix x-ratelimit-reset is in seconds, not ms: https://puu.sh/AIXxY/9b3989b248.png

* mutate reset time with date header instead

* fix defaulting of reset and remaining

if the reset header is not available, then the reset time should be Date.now() instead of the difference between the date header and Date.now()

If the date header is null, fall back to Date.now() since it's the best we can do (this should never happen, but safer to handle it just in case)

if remaining is 0 we don't want to default it back to 1 since 0 is falsy
This commit is contained in:
bdistin
2018-06-23 16:08:40 -05:00
committed by Isabella
parent de408d735b
commit 58e5017159
2 changed files with 7 additions and 13 deletions

View File

@@ -12,7 +12,6 @@ class RESTManager {
this.globallyRateLimited = false;
this.tokenPrefix = tokenPrefix;
this.versioned = true;
this.timeDifferences = [];
if (client.options.restSweepInterval > 0) {
client.setInterval(() => {
this.handlers.sweep(handler => handler._inactive);
@@ -24,15 +23,6 @@ class RESTManager {
return routeBuilder(this);
}
get timeDifference() {
return Math.round(this.timeDifferences.reduce((a, b) => a + b, 0) / this.timeDifferences.length);
}
set timeDifference(ms) {
this.timeDifferences.unshift(ms);
if (this.timeDifferences.length > 5) this.timeDifferences.length = 5;
}
getAuth() {
const token = this.client.token || this.client.accessToken;
const prefixed = !!this.client.application || (this.client.user && this.client.user.bot);

View File

@@ -29,7 +29,7 @@ class RequestHandler {
}
get _inactive() {
return this.queue.length === 0 && !this.limited && Date.now() > this.resetTime && this.busy !== true;
return this.queue.length === 0 && !this.limited && this.busy !== true;
}
/* eslint-disable prefer-promise-reject-errors */
@@ -78,8 +78,12 @@ class RequestHandler {
if (res && res.headers) {
if (res.headers.get('x-ratelimit-global')) this.manager.globallyRateLimited = true;
this.limit = Number(res.headers.get('x-ratelimit-limit') || Infinity);
this.resetTime = Number(res.headers.get('x-ratelimit-reset') || 0);
this.remaining = Number(res.headers.get('x-ratelimit-remaining') || 1);
const reset = res.headers.get('x-ratelimit-reset');
this.resetTime = reset !== null ?
(Number(reset) * 1e3) - new Date(res.headers.get('date') || Date.now()).getTime() + Date.now() :
Date.now();
const remaining = res.headers.get('x-ratelimit-remaining');
this.remaining = remaining !== null ? Number(remaining) : 1;
}
if (res.ok) {