Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(WebSocketShard): explicit time out network error handling #10375

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/ws/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,5 @@ export function getInitialSendRateLimitState(): SendRateLimitState {
resetAt: Date.now() + 60_000,
};
}

export const KnownNetworkErrorCodes = new Set(['ECONNRESET', 'ECONNREFUSED', 'ETIMEDOUT']);
5 changes: 3 additions & 2 deletions packages/ws/src/ws/WebSocketShard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
CompressionMethod,
CompressionParameterMap,
ImportantGatewayOpcodes,
KnownNetworkErrorCodes,
getInitialSendRateLimitState,
} from '../utils/constants.js';
import type { SessionInfo } from './WebSocketManager.js';
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
Loading