mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
feat: abort Requests that takes a lot of time to resolve (#3327)
* 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.
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
@@ -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 });
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
@@ -50,6 +51,7 @@ exports.DefaultOptions = {
|
||||
partials: [],
|
||||
restWsBridgeTimeout: 5000,
|
||||
disabledEvents: [],
|
||||
restRequestTimeout: 15000,
|
||||
retryLimit: 1,
|
||||
restTimeOffset: 500,
|
||||
restSweepInterval: 60,
|
||||
|
||||
1
typings/index.d.ts
vendored
1
typings/index.d.ts
vendored
@@ -2018,6 +2018,7 @@ declare module 'discord.js' {
|
||||
partials?: PartialTypes[];
|
||||
restWsBridgeTimeout?: number;
|
||||
restTimeOffset?: number;
|
||||
restRequestTimeout?: number;
|
||||
restSweepInterval?: number;
|
||||
retryLimit?: number;
|
||||
presence?: PresenceData;
|
||||
|
||||
Reference in New Issue
Block a user