修复:自动回收失效隧道占用并限制首次连接等待时间

This commit is contained in:
2026-10-23 17:47:29 +08:00
parent 3519dd0891
commit 8684ce1a4d
8 changed files with 216 additions and 13 deletions
+14
View File
@@ -0,0 +1,14 @@
export class RequestTimeoutError extends Error {
constructor() { super('请求超时'); this.name = 'RequestTimeoutError'; }
}
/** 限制整个请求(包括读取响应正文)的等待时间,并终止底层网络连接。 */
export async function withRequestDeadline<T>(operation: (signal: AbortSignal) => Promise<T>, timeoutMs: number): Promise<T> {
const controller = new AbortController();
let timer: ReturnType<typeof setTimeout> | undefined;
const expired = new Promise<never>((_, reject) => {
timer = setTimeout(() => { reject(new RequestTimeoutError()); controller.abort(); }, timeoutMs);
});
try { return await Promise.race([operation(controller.signal), expired]); }
finally { clearTimeout(timer); }
}