update server with client queue and connection time
This commit is contained in:
+16
-16
@@ -38,7 +38,7 @@ class TranscriptionServer:
|
|||||||
self.clients = {}
|
self.clients = {}
|
||||||
self.websockets = {}
|
self.websockets = {}
|
||||||
self.clients_start_time = {}
|
self.clients_start_time = {}
|
||||||
self.max_clients = 4
|
self.max_clients = 1
|
||||||
self.max_connection_time = 600 # in seconds
|
self.max_connection_time = 600 # in seconds
|
||||||
|
|
||||||
def get_wait_time(self):
|
def get_wait_time(self):
|
||||||
@@ -58,21 +58,23 @@ class TranscriptionServer:
|
|||||||
Args:
|
Args:
|
||||||
websocket (WebSocket): The WebSocket connection for the client.
|
websocket (WebSocket): The WebSocket connection for the client.
|
||||||
"""
|
"""
|
||||||
# Check if the maximum number of clients is reached
|
logging.info("New client connected")
|
||||||
print("New client connected")
|
options = websocket.recv()
|
||||||
|
options = json.loads(options)
|
||||||
|
|
||||||
if len(self.clients) >= self.max_clients:
|
if len(self.clients) >= self.max_clients:
|
||||||
# Send response to the new client to come back later
|
logging.warning("Client Queue Full. Asking client to wait ...")
|
||||||
wait_time = self.get_wait_time()
|
wait_time = self.get_wait_time()
|
||||||
response = {
|
response = {
|
||||||
|
"uid" : options["uid"],
|
||||||
"status": "WAIT",
|
"status": "WAIT",
|
||||||
"message": wait_time,
|
"message": wait_time,
|
||||||
}
|
}
|
||||||
websocket.send(json.dumps(response))
|
websocket.send(json.dumps(response))
|
||||||
websocket.close()
|
websocket.close()
|
||||||
|
del websocket
|
||||||
return
|
return
|
||||||
|
|
||||||
options = websocket.recv()
|
|
||||||
options = json.loads(options)
|
|
||||||
client = ServeClient(
|
client = ServeClient(
|
||||||
websocket,
|
websocket,
|
||||||
multilingual=options["multilingual"],
|
multilingual=options["multilingual"],
|
||||||
@@ -82,7 +84,6 @@ class TranscriptionServer:
|
|||||||
)
|
)
|
||||||
|
|
||||||
self.clients[websocket] = client
|
self.clients[websocket] = client
|
||||||
# max 10 minutes for each client
|
|
||||||
self.clients_start_time[websocket] = time.time()
|
self.clients_start_time[websocket] = time.time()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
@@ -102,12 +103,11 @@ class TranscriptionServer:
|
|||||||
logging.error(e)
|
logging.error(e)
|
||||||
return
|
return
|
||||||
self.clients[websocket].add_frames(frame_np)
|
self.clients[websocket].add_frames(frame_np)
|
||||||
|
|
||||||
elapsed_time = time.time() - self.clients_start_time[websocket]
|
elapsed_time = time.time() - self.clients_start_time[websocket]
|
||||||
if elapsed_time >= 45: # 10 minutes in seconds
|
if elapsed_time >= self.max_connection_time:
|
||||||
# send a disconnection message
|
|
||||||
self.clients[websocket].disconnect()
|
self.clients[websocket].disconnect()
|
||||||
print(f"{self.clients[websocket]} Client disconnected due to overtime.")
|
logging.warning(f"{self.clients[websocket]} Client disconnected due to overtime.")
|
||||||
print()
|
|
||||||
self.clients[websocket].cleanup()
|
self.clients[websocket].cleanup()
|
||||||
self.clients.pop(websocket)
|
self.clients.pop(websocket)
|
||||||
self.clients_start_time.pop(websocket)
|
self.clients_start_time.pop(websocket)
|
||||||
@@ -120,8 +120,8 @@ class TranscriptionServer:
|
|||||||
self.clients[websocket].cleanup()
|
self.clients[websocket].cleanup()
|
||||||
self.clients.pop(websocket)
|
self.clients.pop(websocket)
|
||||||
self.clients_start_time.pop(websocket)
|
self.clients_start_time.pop(websocket)
|
||||||
print("Connection Closed.")
|
logging.info("Connection Closed.")
|
||||||
print(self.clients)
|
logging.info(self.clients)
|
||||||
|
|
||||||
del websocket
|
del websocket
|
||||||
break
|
break
|
||||||
@@ -294,7 +294,7 @@ class ServeClient:
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.info(f"[ERROR]: {e}")
|
logging.error(f"[ERROR]: {e}")
|
||||||
else:
|
else:
|
||||||
# show previous output if there is pause i.e. no output from whisper
|
# show previous output if there is pause i.e. no output from whisper
|
||||||
segments = []
|
segments = []
|
||||||
@@ -318,9 +318,9 @@ class ServeClient:
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.info(f"[INFO]: {e}")
|
logging.error(f"[ERROR]: {e}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.info(f"[INFO]: {e}")
|
logging.error(f"[ERROR]: {e}")
|
||||||
time.sleep(0.01)
|
time.sleep(0.01)
|
||||||
|
|
||||||
def update_segments(self, segments, duration):
|
def update_segments(self, segments, duration):
|
||||||
|
|||||||
Reference in New Issue
Block a user