Skip to content

Commit 936167b

Browse files
committed
Handle outdated match requests
1 parent 7695dd5 commit 936167b

File tree

4 files changed

+53
-21
lines changed

4 files changed

+53
-21
lines changed

backend/matching-service/src/handlers/matchHandler.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface MatchRequest {
2323
}
2424

2525
export interface MatchRequestItem {
26+
id: string;
2627
user: MatchUser;
2728
complexities: string[];
2829
categories: string[];
@@ -34,10 +35,13 @@ export interface MatchRequestItem {
3435
const matches = new Map<string, Match>();
3536

3637
export const sendMatchRequest = async (
37-
matchRequest: MatchRequest
38+
matchRequest: MatchRequest,
39+
requestId: string
3840
): Promise<boolean> => {
3941
const { user, complexities, categories, languages, timeout } = matchRequest;
42+
4043
const matchItem: MatchRequestItem = {
44+
id: requestId,
4145
user: user,
4246
complexities: complexities,
4347
categories: categories,

backend/matching-service/src/handlers/websocketHandler.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
getMatchByUid,
1010
} from "./matchHandler";
1111
import { io } from "../../server";
12+
import { v4 as uuidv4 } from "uuid";
1213

1314
enum MatchEvents {
1415
// Receive
@@ -38,6 +39,7 @@ interface UserConnection {
3839
socket: Socket;
3940
connectionTimeout?: NodeJS.Timeout;
4041
isDisconnecting: boolean;
42+
requestId?: string;
4143
}
4244

4345
const connectionDelay = 3000; // time window to allow for page navigation / refresh
@@ -47,15 +49,18 @@ export const handleWebsocketMatchEvents = (socket: Socket) => {
4749
socket.removeAllListeners();
4850

4951
socket.on(MatchEvents.USER_CONNECTED, (uid: string) => {
50-
console.log(`uid: ${uid} and socket id: ${socket.id}`);
5152
clearTimeout(userConnections.get(uid)?.connectionTimeout);
5253
if (userConnections.has(uid)) {
5354
const matchId = getMatchIdByUid(uid);
5455
if (matchId) {
5556
socket.join(matchId);
5657
}
5758
}
58-
userConnections.set(uid, { socket: socket, isDisconnecting: false });
59+
userConnections.set(uid, {
60+
socket: socket,
61+
isDisconnecting: false,
62+
requestId: userConnections.get(uid)?.requestId,
63+
});
5964
});
6065

6166
socket.on(MatchEvents.USER_DISCONNECTED, (uid: string) => {
@@ -74,6 +79,7 @@ export const handleWebsocketMatchEvents = (socket: Socket) => {
7479
socket: socket,
7580
connectionTimeout: connectionTimeout,
7681
isDisconnecting: true,
82+
requestId: userConnections.get(uid)?.requestId,
7783
});
7884
});
7985

@@ -90,9 +96,15 @@ export const handleWebsocketMatchEvents = (socket: Socket) => {
9096
return;
9197
}
9298

93-
userConnections.set(uid, { socket: socket, isDisconnecting: false });
99+
const requestId = uuidv4();
100+
userConnections.set(uid, {
101+
socket: socket,
102+
connectionTimeout: userConnections.get(uid)?.connectionTimeout,
103+
isDisconnecting: false,
104+
requestId: requestId,
105+
});
94106

95-
const sent = await sendMatchRequest(matchRequest);
107+
const sent = await sendMatchRequest(matchRequest, requestId);
96108
if (!sent) {
97109
socket.emit(MatchEvents.MATCH_REQUEST_ERROR);
98110
userConnections.delete(uid);
@@ -136,7 +148,15 @@ export const handleWebsocketMatchEvents = (socket: Socket) => {
136148
socket.to(matchId).emit(MatchEvents.MATCH_UNSUCCESSFUL);
137149
}
138150

139-
const sent = await sendMatchRequest(rematchRequest);
151+
const uid = rematchRequest.user.id;
152+
const requestId = uuidv4();
153+
userConnections.set(uid, {
154+
socket: socket,
155+
isDisconnecting: false,
156+
requestId: requestId,
157+
});
158+
159+
const sent = await sendMatchRequest(rematchRequest, requestId);
140160
if (!sent) {
141161
socket.emit(MatchEvents.MATCH_REQUEST_ERROR);
142162
}
@@ -175,6 +195,10 @@ export const handleWebsocketMatchEvents = (socket: Socket) => {
175195
});
176196
};
177197

198+
export const isActiveRequest = (uid: string, requestId: string): boolean => {
199+
return userConnections.get(uid)?.requestId === requestId;
200+
};
201+
178202
export const sendMatchFound = (
179203
matchId: string,
180204
matchUser1: MatchUser,

backend/matching-service/src/utils/mq_utils.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createMatch, MatchRequestItem } from "../handlers/matchHandler";
2-
import { isUserConnected } from "../handlers/websocketHandler";
2+
import { isActiveRequest, isUserConnected } from "../handlers/websocketHandler";
33

44
const matchingRequests = new Map<string, MatchRequestItem>();
55

@@ -10,12 +10,17 @@ export const matchUsers = (newRequest: string) => {
1010
if (
1111
isExpired(pendingRequest) ||
1212
!isUserConnected(uid) ||
13+
!isActiveRequest(uid, pendingRequest.id) ||
1314
uid === newRequestUid
1415
) {
1516
matchingRequests.delete(uid);
1617
continue;
1718
}
18-
if (isExpired(newRequestJson) || !isUserConnected(uid)) {
19+
if (
20+
isExpired(newRequestJson) ||
21+
!isUserConnected(newRequestUid) ||
22+
!isActiveRequest(newRequestUid, newRequestJson.id)
23+
) {
1924
return;
2025
}
2126

@@ -33,16 +38,15 @@ const isExpired = (data: MatchRequestItem): boolean => {
3338
};
3439

3540
const isMatch = (req1: MatchRequestItem, req2: MatchRequestItem): boolean => {
36-
const hasCommonCategory = req1.categories.some((elem) =>
37-
req1.categories.includes(elem)
38-
);
3941
const hasCommonComplexity = req1.complexities.some((elem) =>
4042
req2.complexities.includes(elem)
4143
);
44+
const hasCommonCategory = req1.categories.some((elem) =>
45+
req2.categories.includes(elem)
46+
);
4247
const hasCommonLanguage = req1.languages.some((elem) =>
4348
req2.languages.includes(elem)
4449
);
4550

46-
// return hasCommonCategory && hasCommonComplexity && hasCommonLanguage;
47-
return true;
51+
return hasCommonComplexity && hasCommonCategory && hasCommonLanguage;
4852
};

frontend/src/pages/Home/index.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,14 @@ const Home: React.FC = () => {
265265
color="primary"
266266
fullWidth
267267
sx={{ marginTop: 2 }}
268-
// disabled={
269-
// !timeout ||
270-
// timeout < minMatchTimeout ||
271-
// timeout > maxMatchTimeout ||
272-
// complexities.length == 0 ||
273-
// categories.length == 0 ||
274-
// languages.length == 0
275-
// }
268+
disabled={
269+
!timeout ||
270+
timeout < minMatchTimeout ||
271+
timeout > maxMatchTimeout ||
272+
complexities.length == 0 ||
273+
categories.length == 0 ||
274+
languages.length == 0
275+
}
276276
onClick={() =>
277277
findMatch(complexities, categories, languages, timeout!)
278278
}

0 commit comments

Comments
 (0)