Skip to content

Commit 093ac92

Browse files
authored
feat(WebSocketShard): explicit time out network error handling (#10375)
* feat(WebSocketShard): explicit time out network error handling * refactor: use constant
1 parent ab8bf0f commit 093ac92

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

packages/ws/src/utils/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,5 @@ export function getInitialSendRateLimitState(): SendRateLimitState {
8282
resetAt: Date.now() + 60_000,
8383
};
8484
}
85+
86+
export const KnownNetworkErrorCodes = new Set(['ECONNRESET', 'ECONNREFUSED', 'ETIMEDOUT']);

packages/ws/src/ws/WebSocketShard.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
CompressionMethod,
2727
CompressionParameterMap,
2828
ImportantGatewayOpcodes,
29+
KnownNetworkErrorCodes,
2930
getInitialSendRateLimitState,
3031
} from '../utils/constants.js';
3132
import type { SessionInfo } from './WebSocketManager.js';
@@ -113,7 +114,7 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
113114
// Indicates whether the shard has already resolved its original connect() call
114115
private initialConnectResolved = false;
115116

116-
// Indicates if we failed to connect to the ws url (ECONNREFUSED/ECONNRESET)
117+
// Indicates if we failed to connect to the ws url
117118
private failedToConnectDueToNetworkError = false;
118119

119120
private readonly sendQueue = new AsyncQueue();
@@ -791,7 +792,7 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
791792
}
792793

793794
private onError(error: Error) {
794-
if ('code' in error && ['ECONNRESET', 'ECONNREFUSED'].includes(error.code as string)) {
795+
if ('code' in error && KnownNetworkErrorCodes.has(error.code as string)) {
795796
this.debug(['Failed to connect to the gateway URL specified due to a network error']);
796797
this.failedToConnectDueToNetworkError = true;
797798
return;

0 commit comments

Comments
 (0)