Fix request handling (#1180)

* clean up ratelimiters, and disable burst until some big questions are handled

* burst mode is a work

* fix burst again
This commit is contained in:
Gus Caplan
2017-02-11 06:04:24 -06:00
committed by Amish Shah
parent dd6dd6fb59
commit ec1ed15c88
3 changed files with 31 additions and 49 deletions

View File

@@ -3,8 +3,15 @@ const RequestHandler = require('./RequestHandler');
class BurstRequestHandler extends RequestHandler {
constructor(restManager, endpoint) {
super(restManager, endpoint);
this.requestRemaining = 1;
this.first = true;
this.client = restManager.client;
this.limit = Infinity;
this.resetTime = null;
this.remaining = 1;
this.timeDifference = 0;
this.resetTimeout = null;
}
push(request) {
@@ -12,58 +19,45 @@ class BurstRequestHandler extends RequestHandler {
this.handle();
}
handleNext(time) {
if (this.waiting) return;
this.waiting = true;
this.restManager.client.setTimeout(() => {
this.requestRemaining = this.requestLimit;
this.waiting = false;
this.handle();
}, time);
}
execute(item) {
if (!item) return;
item.request.gen().end((err, res) => {
if (res && res.headers) {
this.requestLimit = res.headers['x-ratelimit-limit'];
this.requestResetTime = Number(res.headers['x-ratelimit-reset']) * 1000;
this.requestRemaining = Number(res.headers['x-ratelimit-remaining']);
this.limit = Number(res.headers['x-ratelimit-limit']);
this.resetTime = Number(res.headers['x-ratelimit-reset']) * 1000;
this.remaining = Number(res.headers['x-ratelimit-remaining']);
this.timeDifference = Date.now() - new Date(res.headers.date).getTime();
this.handleNext(
this.requestResetTime - Date.now() + this.timeDifference + this.restManager.client.options.restTimeOffset
);
}
if (err) {
if (err.status === 429) {
this.requestRemaining = 0;
this.queue.unshift(item);
this.restManager.client.setTimeout(() => {
if (res.headers['x-ratelimit-global']) this.globalLimit = true;
if (this.resetTimeout) return;
this.resetTimeout = this.client.setTimeout(() => {
this.remaining = this.limit;
this.globalLimit = false;
this.handle();
}, Number(res.headers['retry-after']) + this.restManager.client.options.restTimeOffset);
if (res.headers['x-ratelimit-global']) this.globalLimit = true;
this.resetTimeout = null;
}, Number(res.headers['retry-after']) + this.client.options.restTimeOffset);
} else {
item.reject(err);
this.handle();
}
} else {
this.globalLimit = false;
const data = res && res.body ? res.body : {};
item.resolve(data);
if (this.first) {
this.first = false;
this.handle();
}
this.handle();
}
});
}
handle() {
super.handle();
if (this.requestRemaining < 1 || this.queue.length === 0 || this.globalLimit) return;
while (this.queue.length > 0 && this.requestRemaining > 0) {
this.execute(this.queue.shift());
this.requestRemaining--;
}
if (this.remaining <= 0 || this.queue.length === 0 || this.globalLimit) return;
this.execute(this.queue.shift());
this.remaining--;
this.handle();
}
}