From e2a4b3d45c8119b0921cb2f0e1ac7ec4eb08fe2a Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Tue, 14 Mar 2023 00:40:04 +0800 Subject: [PATCH] fix: request timeout errors --- app/requests.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/requests.ts b/app/requests.ts index 54c09e8d6..6429cf31f 100644 --- a/app/requests.ts +++ b/app/requests.ts @@ -52,7 +52,7 @@ export async function requestChatStream( }); const controller = new AbortController(); - setTimeout(() => controller.abort(), 10000); + const reqTimeoutId = setTimeout(() => controller.abort(), 10000); try { const res = await fetch("/api/chat-stream", { @@ -63,10 +63,14 @@ export async function requestChatStream( body: JSON.stringify(req), signal: controller.signal, }); + clearTimeout(reqTimeoutId); let responseText = ""; - const finish = () => options?.onMessage(responseText, true); + const finish = () => { + options?.onMessage(responseText, true); + controller.abort(); + }; if (res.ok) { const reader = res.body?.getReader(); @@ -74,7 +78,9 @@ export async function requestChatStream( while (true) { // handle time out, will stop if no response in 10 secs + const resTimeoutId = setTimeout(() => finish(), 10000); const content = await reader?.read(); + clearTimeout(resTimeoutId); const text = decoder.decode(content?.value); responseText += text;