fix(connection): handle stale connection state

This commit is contained in:
t8y2
2026-05-15 14:39:03 +08:00
parent 2973bb1cd5
commit b06ab4f27f
2 changed files with 44 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
const CONNECTION_ERROR_PATTERNS = [
"connection",
"broken pipe",
"reset by peer",
"timed out",
"closed",
"eof",
"i/o error",
];
export function staleConnectionMessage(error: unknown): string {
if (error instanceof Error) return error.message;
return String(error);
}
export function shouldMarkDisconnected(error: unknown): boolean {
const message = staleConnectionMessage(error).toLowerCase();
return CONNECTION_ERROR_PATTERNS.some((pattern) => message.includes(pattern)) || hasConnectionOsError(message);
}
function hasConnectionOsError(message: string): boolean {
const osErrorCodes = new Set(["10053", "10054", "10057", "10058", "10060", "10061"]);
const match = message.match(/os error\s+(\d+)/);
return !!match && osErrorCodes.has(match[1]);
}