Add ClientOptions.restTimeOffset for better performance for bots with a good network connection

This commit is contained in:
Amish Shah
2016-12-24 12:13:42 +00:00
parent 3eca3ba95e
commit 544e456302
4 changed files with 12 additions and 4 deletions

View File

@@ -29,7 +29,7 @@ class BurstRequestHandler extends RequestHandler {
this.requestResetTime = Number(res.headers['x-ratelimit-reset']) * 1000; this.requestResetTime = Number(res.headers['x-ratelimit-reset']) * 1000;
this.requestRemaining = Number(res.headers['x-ratelimit-remaining']); this.requestRemaining = Number(res.headers['x-ratelimit-remaining']);
this.timeDifference = Date.now() - new Date(res.headers.date).getTime(); this.timeDifference = Date.now() - new Date(res.headers.date).getTime();
this.handleNext((this.requestResetTime - Date.now()) + this.timeDifference + 1000); this.handleNext((this.requestResetTime - Date.now()) + this.timeDifference + this.restManager.client.options.restTimeOffset);
} }
if (err) { if (err) {
if (err.status === 429) { if (err.status === 429) {
@@ -38,7 +38,7 @@ class BurstRequestHandler extends RequestHandler {
this.restManager.client.setTimeout(() => { this.restManager.client.setTimeout(() => {
this.globalLimit = false; this.globalLimit = false;
this.handle(); this.handle();
}, Number(res.headers['retry-after']) + 500); }, Number(res.headers['retry-after']) + this.restManager.client.options.restTimeOffset);
if (res.headers['x-ratelimit-global']) { if (res.headers['x-ratelimit-global']) {
this.globalLimit = true; this.globalLimit = true;
} }

View File

@@ -60,7 +60,7 @@ class SequentialRequestHandler extends RequestHandler {
this.waiting = false; this.waiting = false;
this.globalLimit = false; this.globalLimit = false;
resolve(); resolve();
}, Number(res.headers['retry-after']) + 500); }, Number(res.headers['retry-after']) + this.restManager.client.options.restTimeOffset);
if (res.headers['x-ratelimit-global']) { if (res.headers['x-ratelimit-global']) {
this.globalLimit = true; this.globalLimit = true;
} }
@@ -79,7 +79,7 @@ class SequentialRequestHandler extends RequestHandler {
this.restManager.client.setTimeout(() => { this.restManager.client.setTimeout(() => {
this.waiting = false; this.waiting = false;
resolve(data); resolve(data);
}, (this.requestResetTime - Date.now()) + this.timeDifference + 1000); }, (this.requestResetTime - Date.now()) + this.timeDifference + this.restManager.client.options.restTimeOffset);
} else { } else {
this.waiting = false; this.waiting = false;
resolve(data); resolve(data);

View File

@@ -20,6 +20,8 @@ exports.Package = require('../../package.json');
* @property {boolean} [sync=false] Whether to periodically sync guilds (for userbots) * @property {boolean} [sync=false] Whether to periodically sync guilds (for userbots)
* @property {number} [restWsBridgeTimeout=5000] Maximum time permitted between REST responses and their * @property {number} [restWsBridgeTimeout=5000] Maximum time permitted between REST responses and their
* corresponding websocket events * corresponding websocket events
* @property {number} [restTimeOffset=500] The extra time in millseconds to wait before continuing to make REST
* requests (higher values will reduce rate-limiting errors on bad connections)
* @property {WSEventType[]} [disabledEvents] An array of disabled websocket events. Events in this array will not be * @property {WSEventType[]} [disabledEvents] An array of disabled websocket events. Events in this array will not be
* processed, potentially resulting in performance improvements for larger bots. Only disable events you are * processed, potentially resulting in performance improvements for larger bots. Only disable events you are
* 100% certain you don't need, as many are important, but not obviously so. The safest one to disable with the * 100% certain you don't need, as many are important, but not obviously so. The safest one to disable with the
@@ -38,6 +40,7 @@ exports.DefaultOptions = {
sync: false, sync: false,
restWsBridgeTimeout: 5000, restWsBridgeTimeout: 5000,
disabledEvents: [], disabledEvents: [],
restTimeOffset: 500,
/** /**
* Websocket options. These are left as snake_case to match the API. * Websocket options. These are left as snake_case to match the API.

View File

@@ -117,10 +117,15 @@ client.on('message', message => {
if (message.content === 'ratelimittest') { if (message.content === 'ratelimittest') {
let i = 1; let i = 1;
const start = Date.now();
while (i <= 20) { while (i <= 20) {
message.channel.sendMessage(`Testing my rates, item ${i} of 20`); message.channel.sendMessage(`Testing my rates, item ${i} of 20`);
i++; i++;
} }
message.channel.sendMessage('last one...').then(m => {
const diff = Date.now() - start;
m.reply(`Each message took ${diff / 21}ms to send`);
});
} }
if (message.content === 'makerole') { if (message.content === 'makerole') {