Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Abort Requests that takes a lot of time to resolve #3327

Merged
merged 10 commits into from Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
8 changes: 6 additions & 2 deletions 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 @@ -41,17 +42,20 @@ class APIRequest {
for (const file of this.options.files) if (file && file.file) body.append(file.name, file.file, file.name);
if (typeof this.options.data !== 'undefined') body.append('payload_json', JSON.stringify(this.options.data));
if (!browser) headers = Object.assign(headers, body.getHeaders());
} else if (this.options.data != null) { // eslint-disable-line eqeqeq
} else if (this.options.data != null) { // eslint-disable-line
Deivu marked this conversation as resolved.
Show resolved Hide resolved
body = JSON.stringify(this.options.data);
headers['Content-Type'] = 'application/json';
}

const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), this.client.options.restRequestTimeout);
Deivu marked this conversation as resolved.
Show resolved Hide resolved
return fetch(url, {
method: this.method,
headers,
agent,
body,
});
signal: controller.signal,
}).finally(() => 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 @@ -1707,6 +1707,7 @@ declare module 'discord.js' {
partials?: PartialTypes[];
restWsBridgeTimeout?: number;
restTimeOffset?: number;
restRequestTimeout?: number;
restSweepInterval?: number;
retryLimit?: number;
presence?: PresenceData;
Expand Down