|
1 | 1 | import { sleep } from "../utils/sleep.js"; |
| 2 | +import { collectErrorGraphCandidates, extractErrorCode } from "./errors.js"; |
| 3 | +import { getRetryAttemptErrors } from "./retry.js"; |
2 | 4 |
|
3 | 5 | const RECOVERY_BACKOFF_MS: readonly number[] = [5_000, 25_000, 120_000, 600_000]; |
4 | 6 | export const RECOVERY_REPLAY_SPACING_MS = 250; |
5 | 7 |
|
| 8 | +const PRE_CONNECT_ERROR_CODES = new Set([ |
| 9 | + "ECONNREFUSED", |
| 10 | + "ENOTFOUND", |
| 11 | + "EAI_AGAIN", |
| 12 | + "ENETDOWN", |
| 13 | + "ENETUNREACH", |
| 14 | + "EHOSTUNREACH", |
| 15 | +]); |
| 16 | +const TRANSPORT_ERROR_CODE_RE = |
| 17 | + /^(?:E(?:AI_|CONN|NET|HOST|ADDR|PIPE|TIMEDOUT|SOCKET)|UND_ERR_|ERR_(?:NETWORK|HTTP2|QUIC|TLS|SSL))/; |
| 18 | + |
| 19 | +function isProvenPreConnectCandidate(candidate: unknown): boolean { |
| 20 | + const code = extractErrorCode(candidate)?.trim().toUpperCase(); |
| 21 | + if (code === "UND_ERR_CONNECT_TIMEOUT" || code === "UND_ERR_DNS_RESOLVE_FAILED") { |
| 22 | + return true; |
| 23 | + } |
| 24 | + if (!code || !PRE_CONNECT_ERROR_CODES.has(code) || !candidate || typeof candidate !== "object") { |
| 25 | + return false; |
| 26 | + } |
| 27 | + const syscall = (candidate as { syscall?: unknown }).syscall; |
| 28 | + return syscall === "connect" || syscall === "getaddrinfo"; |
| 29 | +} |
| 30 | + |
| 31 | +function nestedErrorCandidates(current: Record<string, unknown>): unknown[] { |
| 32 | + const retryAttempts = getRetryAttemptErrors(current); |
| 33 | + if (isProvenPreConnectCandidate(current)) { |
| 34 | + return retryAttempts ? [...retryAttempts] : []; |
| 35 | + } |
| 36 | + const nested = [current.cause, current.original, current.error, current.reason]; |
| 37 | + if (Array.isArray(current.errors)) { |
| 38 | + nested.push(...current.errors); |
| 39 | + } |
| 40 | + const nestedObjects = nested.filter( |
| 41 | + (candidate) => candidate !== null && typeof candidate === "object", |
| 42 | + ); |
| 43 | + return retryAttempts ? [...retryAttempts, ...nestedObjects] : nestedObjects; |
| 44 | +} |
| 45 | + |
| 46 | +export function isPreConnectNetworkError(err: unknown): boolean { |
| 47 | + let foundPreConnectProof = false; |
| 48 | + for (const candidate of collectErrorGraphCandidates(err, nestedErrorCandidates)) { |
| 49 | + const code = extractErrorCode(candidate)?.trim().toUpperCase(); |
| 50 | + if (isProvenPreConnectCandidate(candidate)) { |
| 51 | + foundPreConnectProof = true; |
| 52 | + continue; |
| 53 | + } |
| 54 | + const nested = |
| 55 | + candidate && typeof candidate === "object" |
| 56 | + ? nestedErrorCandidates(candidate as Record<string, unknown>) |
| 57 | + : []; |
| 58 | + const isPreConnectAggregateSummary = |
| 59 | + candidate !== null && |
| 60 | + typeof candidate === "object" && |
| 61 | + Array.isArray((candidate as { errors?: unknown }).errors) && |
| 62 | + code !== undefined && |
| 63 | + PRE_CONNECT_ERROR_CODES.has(code); |
| 64 | + // Wrapper nodes may carry neutral SDK codes. Every transport leaf must still |
| 65 | + // prove pre-connect failure; Node AggregateError summary codes are accepted |
| 66 | + // only after their children are traversed and independently prove the same. |
| 67 | + if ( |
| 68 | + nested.length === 0 || |
| 69 | + (code && |
| 70 | + !isPreConnectAggregateSummary && |
| 71 | + (PRE_CONNECT_ERROR_CODES.has(code) || TRANSPORT_ERROR_CODE_RE.test(code))) |
| 72 | + ) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + } |
| 76 | + return foundPreConnectProof; |
| 77 | +} |
| 78 | + |
6 | 79 | export function computeBackoffMs(retryCount: number): number { |
7 | 80 | if (retryCount <= 0) { |
8 | 81 | return 0; |
|
0 commit comments