Skip to content

Commit 65f6580

Browse files
committed
Added log to record client ID registered; added more type hinting
1 parent f00c482 commit 65f6580

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/murfey/server/websocket.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import Any, Dict, TypeVar, Union
88

99
from fastapi import APIRouter, WebSocket, WebSocketDisconnect
10-
from sqlmodel import select
10+
from sqlmodel import Session, select
1111

1212
import murfey.server.prometheus as prom
1313
from murfey.server.murfey_db import get_murfey_db_session
@@ -38,16 +38,17 @@ async def connect(
3838

3939
@staticmethod
4040
def _register_new_client(client_id: int):
41+
log.debug(f"Registering new client with ID {client_id}")
4142
new_client = ClientEnvironment(client_id=client_id, connected=True)
42-
murfey_db = next(get_murfey_db_session())
43+
murfey_db: Session = next(get_murfey_db_session())
4344
murfey_db.add(new_client)
4445
murfey_db.commit()
4546
murfey_db.close()
4647

4748
def disconnect(self, client_id: int | str, unregister_client: bool = True):
4849
self.active_connections.pop(client_id)
4950
if unregister_client:
50-
murfey_db = next(get_murfey_db_session())
51+
murfey_db: Session = next(get_murfey_db_session())
5152
client_env = murfey_db.exec(
5253
select(ClientEnvironment).where(
5354
ClientEnvironment.client_id == client_id
@@ -73,7 +74,7 @@ async def websocket_endpoint(websocket: WebSocket, client_id: int):
7374
while True:
7475
data = await websocket.receive_text()
7576
try:
76-
json_data = json.loads(data)
77+
json_data: dict = json.loads(data)
7778
if json_data["type"] == "log": # and isinstance(json_data, dict)
7879
json_data.pop("type")
7980
await forward_log(json_data, websocket)
@@ -100,7 +101,7 @@ async def websocket_connection_endpoint(
100101
while True:
101102
data = await websocket.receive_text()
102103
try:
103-
json_data = json.loads(data)
104+
json_data: dict = json.loads(data)
104105
if json_data.get("type") == "log": # and isinstance(json_data, dict)
105106
json_data.pop("type")
106107
await forward_log(json_data, websocket)
@@ -115,12 +116,12 @@ async def websocket_connection_endpoint(
115116
await manager.broadcast(f"Client #{client_id} disconnected")
116117

117118

118-
async def check_connections(active_connections):
119+
async def check_connections(active_connections: list[WebSocket]):
119120
log.info("Checking connections")
120121
for connection in active_connections:
121122
log.info("Checking response")
122123
try:
123-
await asyncio.wait_for(connection.receive(), timeout=6)
124+
await asyncio.wait_for(connection.receive(), timeout=10)
124125
except asyncio.TimeoutError:
125126
log.info(f"Disconnecting Client {connection[0]}")
126127
manager.disconnect(connection[0], connection[1])
@@ -139,7 +140,7 @@ async def forward_log(logrecord: dict[str, Any], websocket: WebSocket):
139140

140141
@ws.delete("/test/{client_id}")
141142
async def close_ws_connection(client_id: int):
142-
murfey_db = next(get_murfey_db_session())
143+
murfey_db: Session = next(get_murfey_db_session())
143144
client_env = murfey_db.exec(
144145
select(ClientEnvironment).where(ClientEnvironment.client_id == client_id)
145146
).one()

0 commit comments

Comments
 (0)