refactor: clean up rate limit handling (#2694)

* refactor: clean up rate limit handling

* requested changes

* remove request mode option

* fix dupe requests

* hardcode reaction ratelimits

* suggested changes

* fix small thing

* re-add restTimeOffset

* move restTimeOffset a bit

* i swear i know english its my native language ok

* requested changes

* fix: a bit *too* pre-emptive with ratelimits, now less so

* fix: dapi error shoudl reject with path

* fix: make errors in execute catchable

* fix promise return

* rebase is hard
This commit is contained in:
Isabella
2018-08-23 23:29:44 -05:00
committed by Crawl
parent 82993fbe51
commit 13f46b924b
8 changed files with 215 additions and 175 deletions

View File

@@ -1,4 +1,4 @@
const handlers = require('./handlers');
const RequestHandler = require('./RequestHandler');
const APIRequest = require('./APIRequest');
const routeBuilder = require('./APIRouter');
const { Error } = require('../errors');
@@ -12,6 +12,7 @@ class RESTManager {
this.globallyRateLimited = false;
this.tokenPrefix = tokenPrefix;
this.versioned = true;
this.globalTimeout = null;
if (client.options.restSweepInterval > 0) {
client.setInterval(() => {
this.handlers.sweep(handler => handler._inactive);
@@ -41,24 +42,16 @@ class RESTManager {
request: apiRequest,
resolve,
reject,
});
}).catch(reject);
});
}
getRequestHandler() {
const method = this.client.options.apiRequestMethod;
if (typeof method === 'function') return method;
const handler = handlers[method];
if (!handler) throw new Error('RATELIMIT_INVALID_METHOD');
return handler;
}
request(method, url, options = {}) {
const apiRequest = new APIRequest(this, method, url, options);
let handler = this.handlers.get(apiRequest.route);
if (!handler) {
handler = new handlers.RequestHandler(this, this.getRequestHandler());
handler = new RequestHandler(this);
this.handlers.set(apiRequest.route, handler);
}