From 544e456302a2098e264961277e97c63843b0d11e Mon Sep 17 00:00:00 2001 From: Amish Shah Date: Sat, 24 Dec 2016 12:13:42 +0000 Subject: [PATCH] Add ClientOptions.restTimeOffset for better performance for bots with a good network connection --- src/client/rest/RequestHandlers/Burst.js | 4 ++-- src/client/rest/RequestHandlers/Sequential.js | 4 ++-- src/util/Constants.js | 3 +++ test/random.js | 5 +++++ 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/client/rest/RequestHandlers/Burst.js b/src/client/rest/RequestHandlers/Burst.js index e43df3952..0135d0df2 100644 --- a/src/client/rest/RequestHandlers/Burst.js +++ b/src/client/rest/RequestHandlers/Burst.js @@ -29,7 +29,7 @@ class BurstRequestHandler extends RequestHandler { this.requestResetTime = Number(res.headers['x-ratelimit-reset']) * 1000; this.requestRemaining = Number(res.headers['x-ratelimit-remaining']); 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.status === 429) { @@ -38,7 +38,7 @@ class BurstRequestHandler extends RequestHandler { this.restManager.client.setTimeout(() => { this.globalLimit = false; this.handle(); - }, Number(res.headers['retry-after']) + 500); + }, Number(res.headers['retry-after']) + this.restManager.client.options.restTimeOffset); if (res.headers['x-ratelimit-global']) { this.globalLimit = true; } diff --git a/src/client/rest/RequestHandlers/Sequential.js b/src/client/rest/RequestHandlers/Sequential.js index c971c198f..f206424f7 100644 --- a/src/client/rest/RequestHandlers/Sequential.js +++ b/src/client/rest/RequestHandlers/Sequential.js @@ -60,7 +60,7 @@ class SequentialRequestHandler extends RequestHandler { this.waiting = false; this.globalLimit = false; resolve(); - }, Number(res.headers['retry-after']) + 500); + }, Number(res.headers['retry-after']) + this.restManager.client.options.restTimeOffset); if (res.headers['x-ratelimit-global']) { this.globalLimit = true; } @@ -79,7 +79,7 @@ class SequentialRequestHandler extends RequestHandler { this.restManager.client.setTimeout(() => { this.waiting = false; resolve(data); - }, (this.requestResetTime - Date.now()) + this.timeDifference + 1000); + }, (this.requestResetTime - Date.now()) + this.timeDifference + this.restManager.client.options.restTimeOffset); } else { this.waiting = false; resolve(data); diff --git a/src/util/Constants.js b/src/util/Constants.js index 08f804156..fddc6d14a 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -20,6 +20,8 @@ exports.Package = require('../../package.json'); * @property {boolean} [sync=false] Whether to periodically sync guilds (for userbots) * @property {number} [restWsBridgeTimeout=5000] Maximum time permitted between REST responses and their * 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 * 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 @@ -38,6 +40,7 @@ exports.DefaultOptions = { sync: false, restWsBridgeTimeout: 5000, disabledEvents: [], + restTimeOffset: 500, /** * Websocket options. These are left as snake_case to match the API. diff --git a/test/random.js b/test/random.js index d6c761ea5..0b22765ff 100644 --- a/test/random.js +++ b/test/random.js @@ -117,10 +117,15 @@ client.on('message', message => { if (message.content === 'ratelimittest') { let i = 1; + const start = Date.now(); while (i <= 20) { message.channel.sendMessage(`Testing my rates, item ${i} of 20`); 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') {