Skip to content

Commit c56d6b4

Browse files
committed
Add 60s timeout
1 parent 04a860a commit c56d6b4

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

frontend/components/matching/find-match.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default function FindMatch() {
1515
const [isSearching, setIsSearching] = useState<boolean>(false);
1616
const [waitTime, setWaitTime] = useState<number>(0);
1717
const [websocket, setWebsocket] = useState<WebSocket>();
18+
const [queueTimeout, setQueueTimeout] = useState<NodeJS.Timeout>();
1819
const { toast } = useToast();
1920
const auth = useAuth();
2021

@@ -26,17 +27,14 @@ export default function FindMatch() {
2627
}, 1000);
2728
} else {
2829
setWaitTime(0);
30+
clearTimeout(queueTimeout);
31+
setQueueTimeout(undefined);
2932
}
30-
return () => clearInterval(interval);
31-
}, [isSearching]);
3233

33-
useEffect(() => {
3434
return () => {
35-
if (websocket) {
36-
websocket.close();
37-
}
35+
clearInterval(interval);
3836
};
39-
}, [websocket]);
37+
}, [isSearching]);
4038

4139
const handleSearch = async () => {
4240
if (!selectedDifficulty || !selectedTopic) {
@@ -105,6 +103,11 @@ export default function FindMatch() {
105103
variant: "destructive",
106104
});
107105
};
106+
setQueueTimeout(
107+
setTimeout(() => {
108+
handleCancel();
109+
}, 60000)
110+
);
108111
setWebsocket(ws);
109112
return;
110113
default:

matching-service/app/routers/match.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ async def subscribe(
3939
while True:
4040
await websocket.receive_text()
4141
except WebSocketDisconnect:
42-
manager.disconnect(user_id, topic, difficulty, websocket)
42+
await manager.disconnect(user_id, topic, difficulty, websocket)

matching-service/app/utils/socketmanager.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ async def disconnect_all(
4848
return
4949

5050
await asyncio.gather(
51-
*[websocket.close() for websocket in self.connection_map[key]]
51+
*[self._close_and_ignore(websocket) for websocket in self.connection_map[key]]
5252
)
53-
del self.connection_map[key]
53+
self.connection_map.pop(key, None)
5454

5555
'''
5656
Disconnects the single connection.
@@ -68,8 +68,14 @@ async def disconnect(
6868

6969
self.connection_map[key].remove(websocket)
7070
if len(self.connection_map[key]) == 0:
71-
del self.connections_map[key]
72-
websocket.close()
71+
self.connection_map.pop(key, None)
72+
self._close_and_ignore(websocket)
73+
74+
async def _close_and_ignore(self, websocket: WebSocket):
75+
try:
76+
await websocket.close()
77+
except Exception:
78+
pass
7379

7480
'''
7581
Data is sent to through all connections associated with

0 commit comments

Comments
 (0)