mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 02:53:31 +01:00
Comply to the new Rate Limit Headers hammerandchisel/discord-api-docs#108
This commit is contained in:
@@ -12,6 +12,7 @@ class RESTManager {
|
|||||||
this.userAgentManager = new UserAgentManager(this);
|
this.userAgentManager = new UserAgentManager(this);
|
||||||
this.methods = new RESTMethods(this);
|
this.methods = new RESTMethods(this);
|
||||||
this.rateLimitedEndpoints = {};
|
this.rateLimitedEndpoints = {};
|
||||||
|
this.globallyRateLimited = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
push(handler, apiRequest) {
|
push(handler, apiRequest) {
|
||||||
|
|||||||
@@ -17,6 +17,18 @@ module.exports = class RequestHandler {
|
|||||||
this.queue = [];
|
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
|
* Push a new API request into this bucket
|
||||||
* @param {APIRequest} request the new request to push into the queue
|
* @param {APIRequest} request the new request to push into the queue
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ module.exports = class SequentialRequestHandler extends RequestHandler {
|
|||||||
* @returns {Promise<Object, Error>}
|
* @returns {Promise<Object, Error>}
|
||||||
*/
|
*/
|
||||||
execute(item) {
|
execute(item) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise(resolve => {
|
||||||
item.request.gen().end((err, res) => {
|
item.request.gen().end((err, res) => {
|
||||||
if (res && res.headers) {
|
if (res && res.headers) {
|
||||||
this.requestLimit = res.headers['x-ratelimit-limit'];
|
this.requestLimit = res.headers['x-ratelimit-limit'];
|
||||||
@@ -48,8 +48,12 @@ module.exports = class SequentialRequestHandler extends RequestHandler {
|
|||||||
if (err.status === 429) {
|
if (err.status === 429) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.waiting = false;
|
this.waiting = false;
|
||||||
|
this.globalLimit = false;
|
||||||
resolve();
|
resolve();
|
||||||
}, res.headers['retry-after']);
|
}, res.headers['retry-after'] + 500);
|
||||||
|
if (res.headers['x-ratelimit-global']) {
|
||||||
|
this.globalLimit = true;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
this.queue.shift();
|
this.queue.shift();
|
||||||
this.waiting = false;
|
this.waiting = false;
|
||||||
@@ -58,6 +62,7 @@ module.exports = class SequentialRequestHandler extends RequestHandler {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.queue.shift();
|
this.queue.shift();
|
||||||
|
this.globalLimit = false;
|
||||||
const data = res && res.body ? res.body : {};
|
const data = res && res.body ? res.body : {};
|
||||||
item.resolve(data);
|
item.resolve(data);
|
||||||
if (this.requestRemaining === 0) {
|
if (this.requestRemaining === 0) {
|
||||||
@@ -76,7 +81,7 @@ module.exports = class SequentialRequestHandler extends RequestHandler {
|
|||||||
|
|
||||||
handle() {
|
handle() {
|
||||||
super.handle();
|
super.handle();
|
||||||
if (this.waiting || this.queue.length === 0) {
|
if (this.waiting || this.queue.length === 0 || this.globalLimit) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.waiting = true;
|
this.waiting = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user