Skip to content

Commit

Permalink
Cleanup after clients upon disconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
NeonDaniel committed Sep 23, 2024
1 parent c9254c5 commit e135119
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion neon_hana/app/routers/node_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async def node_v1_endpoint(websocket: WebSocket, token: str):
socket_api.handle_client_input(client_in, client_id)
except WebSocketDisconnect:
disconnect_event.set()
# TODO: Delete client from socket_api
socket_api.end_session(session_id=client_id)


@node_route.websocket("/v1/stream")
Expand Down
20 changes: 19 additions & 1 deletion neon_hana/mq_websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ def new_connection(self, ws: WebSocket, session_id: str):
"socket": ws,
"user": self.user_config}

def end_session(self, session_id: str):
"""
End a client connection upon WS disconnection
"""
session: Optional[dict] = self._sessions.pop(session_id, None)
if not session:
LOG.error(f"Ended session is not established {session_id}")
return
stream: RemoteStreamHandler = session.get('stream')
if stream:
stream.shutdown()
stream.join()
LOG.info(f"Ended stream handler for: {session_id}")

def get_session(self, session_id: str) -> dict:
"""
Get the latest session context for the given session_id.
Expand Down Expand Up @@ -245,7 +259,7 @@ def start(self):
pass

def stop(self):
pass
self.queue.put(None)

def read_chunk(self) -> Optional[bytes]:
return self.queue.get()
Expand Down Expand Up @@ -302,6 +316,10 @@ def on_audio(self, audio_bytes: bytes, context: dict):
def on_chunk(self, chunk: ChunkInfo):
LOG.debug(f"Chunk: {chunk}")

def shutdown(self):
self.mic.stop()
self.voice_loop.stop()


class MockTransformers(Mock):
def transform(self, chunk):
Expand Down

0 comments on commit e135119

Please sign in to comment.