From 0ef02dea99dbf50a61cab09e4147864a14c276b8 Mon Sep 17 00:00:00 2001 From: mdr223 Date: Mon, 6 Nov 2023 21:58:47 -0500 Subject: [PATCH] add timeout handling on server-side --- A2rchi/interfaces/chat_app/app.py | 9 +++++++-- A2rchi/interfaces/chat_app/static/script.js-template | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/A2rchi/interfaces/chat_app/app.py b/A2rchi/interfaces/chat_app/app.py index 2c669fe..99361ce 100644 --- a/A2rchi/interfaces/chat_app/app.py +++ b/A2rchi/interfaces/chat_app/app.py @@ -367,8 +367,6 @@ def get_chat_response(self): import time time.sleep(130) - # TODO: also need to add timeout to server - # compute timestamp at which message was received by server msg_ts = datetime.now() @@ -376,6 +374,13 @@ def get_chat_response(self): message = request.json.get('last_message') conversation_id = request.json.get('conversation_id') is_refresh = request.json.get('is_refresh') + client_msg_ts = request.json.get('client_msg_ts') / 1000 + client_timeout = request.json.get('timeout') / 1000 + + # if timestamp from message is more than TIMEOUT_SECS in the past; + # do not generate response as the client will have timed out + if msg_ts - client_msg_ts > client_timeout: + return jsonify({'error': 'client timeout'}), 408 # query the chat and return the results. print(" INFO - Calling the ChatWrapper()") diff --git a/A2rchi/interfaces/chat_app/static/script.js-template b/A2rchi/interfaces/chat_app/static/script.js-template index 7b30464..1d66b95 100644 --- a/A2rchi/interfaces/chat_app/static/script.js-template +++ b/A2rchi/interfaces/chat_app/static/script.js-template @@ -101,6 +101,7 @@ const getChatResponse = async (incomingChatDiv, isRefresh=false) => { conversation_id: conversation_id, is_refresh: isRefresh, }), + client_msg_ts: Date.now(), timeout: DEFAULT_TIMEOUT_SECS * 1000 } @@ -115,7 +116,7 @@ const getChatResponse = async (incomingChatDiv, isRefresh=false) => { last_response_is_feedback_request = false; } catch (error) { pElement.classList.add("error"); - pElement.textContent = "Oops! Something went wrong while retrieving the response. Please try again."; + pElement.textContent = "

Oops! Something went wrong while retrieving the response. Please try again.

"; } // Remove the typing animation, append the paragraph element and save the chats to local storage