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:
DD 2024-07-02 23:25:22 +03:00 committed by GitHub
parent ab8bf0f4d2
commit 093ac924ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 5 additions and 2 deletions

View file

@ -82,3 +82,5 @@ export function getInitialSendRateLimitState(): SendRateLimitState {
resetAt: Date.now() + 60_000,
};
}
export const KnownNetworkErrorCodes = new Set(['ECONNRESET', 'ECONNREFUSED', 'ETIMEDOUT']);

View file

@ -26,6 +26,7 @@ import {
CompressionMethod,
CompressionParameterMap,
ImportantGatewayOpcodes,
KnownNetworkErrorCodes,
getInitialSendRateLimitState,
} from '../utils/constants.js';
import type { SessionInfo } from './WebSocketManager.js';
@ -113,7 +114,7 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
// 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();
@ -791,7 +792,7 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
}
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;