From a5437a41f31bf47e9a6f8dc4452086c614d71d1c Mon Sep 17 00:00:00 2001 From: DD Date: Tue, 2 Jul 2024 23:25:22 +0300 Subject: [PATCH] feat(WebSocketShard): explicit time out network error handling (#10375) * feat(WebSocketShard): explicit time out network error handling * refactor: use constant --- packages/ws/src/utils/constants.ts | 2 ++ packages/ws/src/ws/WebSocketShard.ts | 16 +++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/ws/src/utils/constants.ts b/packages/ws/src/utils/constants.ts index c901c23b2..99873fde4 100644 --- a/packages/ws/src/utils/constants.ts +++ b/packages/ws/src/utils/constants.ts @@ -75,3 +75,5 @@ export function getInitialSendRateLimitState(): SendRateLimitState { resetAt: Date.now() + 60_000, }; } + +export const KnownNetworkErrorCodes = new Set(['ECONNRESET', 'ECONNREFUSED', 'ETIMEDOUT']); diff --git a/packages/ws/src/ws/WebSocketShard.ts b/packages/ws/src/ws/WebSocketShard.ts index 300acb94c..8547c69f3 100644 --- a/packages/ws/src/ws/WebSocketShard.ts +++ b/packages/ws/src/ws/WebSocketShard.ts @@ -21,9 +21,15 @@ import { type GatewaySendPayload, } from 'discord-api-types/v10'; import { WebSocket, type Data } from 'ws'; -import type { Inflate } from 'zlib-sync'; -import type { IContextFetchingStrategy } from '../strategies/context/IContextFetchingStrategy.js'; -import { ImportantGatewayOpcodes, getInitialSendRateLimitState } from '../utils/constants.js'; +import type * as ZlibSync from 'zlib-sync'; +import type { IContextFetchingStrategy } from '../strategies/context/IContextFetchingStrategy'; +import { + CompressionMethod, + CompressionParameterMap, + ImportantGatewayOpcodes, + KnownNetworkErrorCodes, + getInitialSendRateLimitState, +} from '../utils/constants.js'; import type { SessionInfo } from './WebSocketManager.js'; // eslint-disable-next-line promise/prefer-await-to-then @@ -107,7 +113,7 @@ export class WebSocketShard extends AsyncEventEmitter { // Indicates whether the shard has already resolved its original connect() call 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 readonly sendQueue = new AsyncQueue(); @@ -710,7 +716,7 @@ export class WebSocketShard extends AsyncEventEmitter { } 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.failedToConnectDueToNetworkError = true; return;