mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
feat(WebSocketShard): explicit time out network error handling (#10375)
* feat(WebSocketShard): explicit time out network error handling * refactor: use constant
This commit is contained in:
@@ -75,3 +75,5 @@ export function getInitialSendRateLimitState(): SendRateLimitState {
|
|||||||
resetAt: Date.now() + 60_000,
|
resetAt: Date.now() + 60_000,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const KnownNetworkErrorCodes = new Set(['ECONNRESET', 'ECONNREFUSED', 'ETIMEDOUT']);
|
||||||
|
|||||||
@@ -21,9 +21,15 @@ import {
|
|||||||
type GatewaySendPayload,
|
type GatewaySendPayload,
|
||||||
} from 'discord-api-types/v10';
|
} from 'discord-api-types/v10';
|
||||||
import { WebSocket, type Data } from 'ws';
|
import { WebSocket, type Data } from 'ws';
|
||||||
import type { Inflate } from 'zlib-sync';
|
import type * as ZlibSync from 'zlib-sync';
|
||||||
import type { IContextFetchingStrategy } from '../strategies/context/IContextFetchingStrategy.js';
|
import type { IContextFetchingStrategy } from '../strategies/context/IContextFetchingStrategy';
|
||||||
import { ImportantGatewayOpcodes, getInitialSendRateLimitState } from '../utils/constants.js';
|
import {
|
||||||
|
CompressionMethod,
|
||||||
|
CompressionParameterMap,
|
||||||
|
ImportantGatewayOpcodes,
|
||||||
|
KnownNetworkErrorCodes,
|
||||||
|
getInitialSendRateLimitState,
|
||||||
|
} from '../utils/constants.js';
|
||||||
import type { SessionInfo } from './WebSocketManager.js';
|
import type { SessionInfo } from './WebSocketManager.js';
|
||||||
|
|
||||||
// eslint-disable-next-line promise/prefer-await-to-then
|
// eslint-disable-next-line promise/prefer-await-to-then
|
||||||
@@ -107,7 +113,7 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
|
|||||||
// Indicates whether the shard has already resolved its original connect() call
|
// Indicates whether the shard has already resolved its original connect() call
|
||||||
private initialConnectResolved = false;
|
private initialConnectResolved = false;
|
||||||
|
|
||||||
// Indicates if we failed to connect to the ws url (ECONNREFUSED/ECONNRESET)
|
// Indicates if we failed to connect to the ws url
|
||||||
private failedToConnectDueToNetworkError = false;
|
private failedToConnectDueToNetworkError = false;
|
||||||
|
|
||||||
private readonly sendQueue = new AsyncQueue();
|
private readonly sendQueue = new AsyncQueue();
|
||||||
@@ -710,7 +716,7 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private onError(error: Error) {
|
private onError(error: Error) {
|
||||||
if ('code' in error && ['ECONNRESET', 'ECONNREFUSED'].includes(error.code as string)) {
|
if ('code' in error && KnownNetworkErrorCodes.has(error.code as string)) {
|
||||||
this.debug(['Failed to connect to the gateway URL specified due to a network error']);
|
this.debug(['Failed to connect to the gateway URL specified due to a network error']);
|
||||||
this.failedToConnectDueToNetworkError = true;
|
this.failedToConnectDueToNetworkError = true;
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user