mirror of
https://github.com/danbulant/discord.js
synced 2026-07-06 11:40:41 +00: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:
parent
3fcc862c5f
commit
e4309b23d5
5 changed files with 12 additions and 1 deletions
|
|
@ -35,6 +35,7 @@
|
||||||
"runkitExampleFilename": "./docs/examples/ping.js",
|
"runkitExampleFilename": "./docs/examples/ping.js",
|
||||||
"unpkg": "./webpack/discord.min.js",
|
"unpkg": "./webpack/discord.min.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"abort-controller": "^3.0.0",
|
||||||
"form-data": "^2.3.3",
|
"form-data": "^2.3.3",
|
||||||
"node-fetch": "^2.3.0",
|
"node-fetch": "^2.3.0",
|
||||||
"pako": "^1.0.8",
|
"pako": "^1.0.8",
|
||||||
|
|
|
||||||
|
|
@ -392,6 +392,9 @@ class Client extends BaseClient {
|
||||||
if (typeof options.restWsBridgeTimeout !== 'number' || isNaN(options.restWsBridgeTimeout)) {
|
if (typeof options.restWsBridgeTimeout !== 'number' || isNaN(options.restWsBridgeTimeout)) {
|
||||||
throw new TypeError('CLIENT_INVALID_OPTION', 'restWsBridgeTimeout', 'a number');
|
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)) {
|
if (typeof options.restSweepInterval !== 'number' || isNaN(options.restSweepInterval)) {
|
||||||
throw new TypeError('CLIENT_INVALID_OPTION', 'restSweepInterval', 'a number');
|
throw new TypeError('CLIENT_INVALID_OPTION', 'restSweepInterval', 'a number');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ const FormData = require('form-data');
|
||||||
const https = require('https');
|
const https = require('https');
|
||||||
const { browser, UserAgent } = require('../util/Constants');
|
const { browser, UserAgent } = require('../util/Constants');
|
||||||
const fetch = require('node-fetch');
|
const fetch = require('node-fetch');
|
||||||
|
const AbortController = require('abort-controller');
|
||||||
|
|
||||||
if (https.Agent) var agent = new https.Agent({ keepAlive: true });
|
if (https.Agent) var agent = new https.Agent({ keepAlive: true });
|
||||||
|
|
||||||
|
|
@ -46,12 +47,15 @@ class APIRequest {
|
||||||
headers['Content-Type'] = 'application/json';
|
headers['Content-Type'] = 'application/json';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeout = this.client.setTimeout(() => controller.abort(), this.client.options.restRequestTimeout);
|
||||||
return fetch(url, {
|
return fetch(url, {
|
||||||
method: this.method,
|
method: this.method,
|
||||||
headers,
|
headers,
|
||||||
agent,
|
agent,
|
||||||
body,
|
body,
|
||||||
});
|
signal: controller.signal,
|
||||||
|
}).finally(() => this.client.clearTimeout(timeout));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ const browser = exports.browser = typeof window !== 'undefined';
|
||||||
* corresponding websocket events
|
* corresponding websocket events
|
||||||
* @property {number} [restTimeOffset=500] Extra time in millseconds to wait before continuing to make REST
|
* @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)
|
* 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
|
* @property {number} [restSweepInterval=60] How frequently to delete inactive request buckets, in seconds
|
||||||
* (or 0 for never)
|
* (or 0 for never)
|
||||||
* @property {number} [retryLimit=1] How many times to retry on 5XX errors (Infinity for indefinite amount of retries)
|
* @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: [],
|
partials: [],
|
||||||
restWsBridgeTimeout: 5000,
|
restWsBridgeTimeout: 5000,
|
||||||
disabledEvents: [],
|
disabledEvents: [],
|
||||||
|
restRequestTimeout: 15000,
|
||||||
retryLimit: 1,
|
retryLimit: 1,
|
||||||
restTimeOffset: 500,
|
restTimeOffset: 500,
|
||||||
restSweepInterval: 60,
|
restSweepInterval: 60,
|
||||||
|
|
|
||||||
1
typings/index.d.ts
vendored
1
typings/index.d.ts
vendored
|
|
@ -2018,6 +2018,7 @@ declare module 'discord.js' {
|
||||||
partials?: PartialTypes[];
|
partials?: PartialTypes[];
|
||||||
restWsBridgeTimeout?: number;
|
restWsBridgeTimeout?: number;
|
||||||
restTimeOffset?: number;
|
restTimeOffset?: number;
|
||||||
|
restRequestTimeout?: number;
|
||||||
restSweepInterval?: number;
|
restSweepInterval?: number;
|
||||||
retryLimit?: number;
|
retryLimit?: number;
|
||||||
presence?: PresenceData;
|
presence?: PresenceData;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue