Skip to content

Commit

Permalink
feat: abort Requests that takes a lot of time to resolve (#3327)
Browse files Browse the repository at this point in the history
* Add Request Timeout

* Add abort controller in packages

* Fix Lint Error.

* Fix Lint Errors

* Make Timeout Customizable & use finally

* Fixed a minor issue

* Fix eslint

* Update request timeout to use d.js client timeout methods.
  • Loading branch information
Deivu authored and SpaceEEC committed Aug 19, 2019
1 parent 3fcc862 commit e4309b2
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -35,6 +35,7 @@
"runkitExampleFilename": "./docs/examples/ping.js",
"unpkg": "./webpack/discord.min.js",
"dependencies": {
"abort-controller": "^3.0.0",
"form-data": "^2.3.3",
"node-fetch": "^2.3.0",
"pako": "^1.0.8",
Expand Down
3 changes: 3 additions & 0 deletions src/client/Client.js
Expand Up @@ -392,6 +392,9 @@ class Client extends BaseClient {
if (typeof options.restWsBridgeTimeout !== 'number' || isNaN(options.restWsBridgeTimeout)) {
throw new TypeError('CLIENT_INVALID_OPTION', 'restWsBridgeTimeout', 'a number');
}
if (typeof options.restRequestTimeout !== 'number' || isNaN(options.restRequestTimeout)) {
throw new TypeError('CLIENT_INVALID_OPTION', 'restRequestTimeout', 'a number');
}
if (typeof options.restSweepInterval !== 'number' || isNaN(options.restSweepInterval)) {
throw new TypeError('CLIENT_INVALID_OPTION', 'restSweepInterval', 'a number');
}
Expand Down
6 changes: 5 additions & 1 deletion src/rest/APIRequest.js
Expand Up @@ -4,6 +4,7 @@ const FormData = require('form-data');
const https = require('https');
const { browser, UserAgent } = require('../util/Constants');
const fetch = require('node-fetch');
const AbortController = require('abort-controller');

if (https.Agent) var agent = new https.Agent({ keepAlive: true });

Expand Down Expand Up @@ -46,12 +47,15 @@ class APIRequest {
headers['Content-Type'] = 'application/json';
}

const controller = new AbortController();
const timeout = this.client.setTimeout(() => controller.abort(), this.client.options.restRequestTimeout);
return fetch(url, {
method: this.method,
headers,
agent,
body,
});
signal: controller.signal,
}).finally(() => this.client.clearTimeout(timeout));
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/util/Constants.js
Expand Up @@ -28,6 +28,7 @@ const browser = exports.browser = typeof window !== 'undefined';
* corresponding websocket events
* @property {number} [restTimeOffset=500] Extra time in millseconds to wait before continuing to make REST
* requests (higher values will reduce rate-limiting errors on bad connections)
* @property {number} [restRequestTimeout=15000] Time to wait before cancelling a REST request
* @property {number} [restSweepInterval=60] How frequently to delete inactive request buckets, in seconds
* (or 0 for never)
* @property {number} [retryLimit=1] How many times to retry on 5XX errors (Infinity for indefinite amount of retries)
Expand All @@ -50,6 +51,7 @@ exports.DefaultOptions = {
partials: [],
restWsBridgeTimeout: 5000,
disabledEvents: [],
restRequestTimeout: 15000,
retryLimit: 1,
restTimeOffset: 500,
restSweepInterval: 60,
Expand Down
1 change: 1 addition & 0 deletions typings/index.d.ts
Expand Up @@ -2018,6 +2018,7 @@ declare module 'discord.js' {
partials?: PartialTypes[];
restWsBridgeTimeout?: number;
restTimeOffset?: number;
restRequestTimeout?: number;
restSweepInterval?: number;
retryLimit?: number;
presence?: PresenceData;
Expand Down

0 comments on commit e4309b2

Please sign in to comment.