Skip to content

Commit f2c419e

Browse files
authored
Merge pull request #101 from DefangLabs/linda-http-timeout
2 parents 4af0f15 + 5520e88 commit f2c419e

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

app/templates/index.html

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,12 @@ <h2>Ask Defang</h2>
293293

294294
function ask(query, responseContainer) {
295295
const assistantResponse = responseContainer.querySelector('.assistant-response');
296+
297+
// Add a timeout for fetch
298+
const controller = new AbortController();
299+
const signal = controller.signal;
300+
const timeoutId = setTimeout(() => controller.abort(), 60000); // 60-second timeout
301+
296302
// Send query to server
297303
rateLimitingFetch('/ask', {
298304
method: 'POST',
@@ -301,8 +307,10 @@ <h2>Ask Defang</h2>
301307
'X-CSRFToken': '{{ csrf_token() }}'
302308
},
303309
body: JSON.stringify({ query: query }),
310+
signal: signal
304311
})
305312
.then(response => {
313+
clearTimeout(timeoutId); // Clear timeout if request succeeds
306314
const reader = response.body.getReader();
307315
const decoder = new TextDecoder();
308316
let responseText = '';
@@ -327,10 +335,16 @@ <h2>Ask Defang</h2>
327335
readStream();
328336
})
329337
.catch(error => {
330-
console.error('Error:', error);
338+
clearTimeout(timeoutId); // Clear timeout if request fails
339+
if (error.name === 'AbortError') {
340+
assistantResponse.textContent = 'Request timed out. Please try again.';
341+
console.error('Request timed out. Error: ', error);
342+
} else {
343+
assistantResponse.textContent = `Failed to fetch response. Error: ${error.message}`;
344+
console.error('Failed to fetch response. Error:', error);
345+
}
331346
loadingSpinner.style.display = 'none';
332347
sendButton.disabled = false;
333-
assistantResponse.textContent = 'Error: Failed to get response';
334348
chatBox.scrollTop = chatBox.scrollHeight;
335349
});
336350
}

0 commit comments

Comments
 (0)