Skip to content

Commit a5437a4

Browse files
didinelevladfrangu
authored andcommitted
feat(WebSocketShard): explicit time out network error handling (#10375)
* feat(WebSocketShard): explicit time out network error handling * refactor: use constant
1 parent 11dd1c0 commit a5437a4

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

packages/ws/src/utils/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,5 @@ export function getInitialSendRateLimitState(): SendRateLimitState {
7575
resetAt: Date.now() + 60_000,
7676
};
7777
}
78+
79+
export const KnownNetworkErrorCodes = new Set(['ECONNRESET', 'ECONNREFUSED', 'ETIMEDOUT']);

packages/ws/src/ws/WebSocketShard.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@ import {
2121
type GatewaySendPayload,
2222
} from 'discord-api-types/v10';
2323
import { WebSocket, type Data } from 'ws';
24-
import type { Inflate } from 'zlib-sync';
25-
import type { IContextFetchingStrategy } from '../strategies/context/IContextFetchingStrategy.js';
26-
import { ImportantGatewayOpcodes, getInitialSendRateLimitState } from '../utils/constants.js';
24+
import type * as ZlibSync from 'zlib-sync';
25+
import type { IContextFetchingStrategy } from '../strategies/context/IContextFetchingStrategy';
26+
import {
27+
CompressionMethod,
28+
CompressionParameterMap,
29+
ImportantGatewayOpcodes,
30+
KnownNetworkErrorCodes,
31+
getInitialSendRateLimitState,
32+
} from '../utils/constants.js';
2733
import type { SessionInfo } from './WebSocketManager.js';
2834

2935
// eslint-disable-next-line promise/prefer-await-to-then
@@ -107,7 +113,7 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
107113
// Indicates whether the shard has already resolved its original connect() call
108114
private initialConnectResolved = false;
109115

110-
// Indicates if we failed to connect to the ws url (ECONNREFUSED/ECONNRESET)
116+
// Indicates if we failed to connect to the ws url
111117
private failedToConnectDueToNetworkError = false;
112118

113119
private readonly sendQueue = new AsyncQueue();
@@ -710,7 +716,7 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
710716
}
711717

712718
private onError(error: Error) {
713-
if ('code' in error && ['ECONNRESET', 'ECONNREFUSED'].includes(error.code as string)) {
719+
if ('code' in error && KnownNetworkErrorCodes.has(error.code as string)) {
714720
this.debug(['Failed to connect to the gateway URL specified due to a network error']);
715721
this.failedToConnectDueToNetworkError = true;
716722
return;

0 commit comments

Comments
 (0)