Skip to content

Commit 4b53d8d

Browse files
committed
PEER-220 Add wait lock for currently matching requests
Signed-off-by: SeeuSim <[email protected]>
1 parent bfbe560 commit 4b53d8d

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

backend/matching/src/workers/matcher.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ async function processMatch(
3535
timestamp, // We use timestamp as the Stream ID
3636
socketPort: matchedSocketPort,
3737
} = decodePoolTicket(matched);
38+
39+
// To block cancellation
40+
io.sockets.in([matchedSocketPort]).emit('Matching');
41+
3842
const matchedStreamId = getStreamId(timestamp);
3943

4044
logger.info(`Found match: ${JSON.stringify(matched)}`);
@@ -88,6 +92,10 @@ async function match() {
8892
difficulty,
8993
topic,
9094
} = decodePoolTicket(matchRequest);
95+
96+
// To Block Cancellation
97+
io.sockets.in([requestorSocketPort]).emit('Matching');
98+
9199
const clause = [`-@userId:(${requestorUserId})`];
92100
if (difficulty) {
93101
clause.push(`@difficulty:{${difficulty}}`);
@@ -111,7 +119,7 @@ async function match() {
111119
);
112120
if (exactMatchFound || !topic || !difficulty) {
113121
// Match found, or Partial search completed
114-
return;
122+
continue;
115123
}
116124

117125
// Match on Topic
@@ -127,7 +135,7 @@ async function match() {
127135
'topic'
128136
);
129137
if (topicMatchFound) {
130-
return;
138+
continue;
131139
}
132140

133141
// Match on Difficulty
@@ -136,7 +144,17 @@ async function match() {
136144
`@difficulty:${difficulty} -@userId:(${requestorUserId})`,
137145
searchParams
138146
);
139-
await processMatch(redisClient, requestorParams, difficultyMatches, 'difficulty');
147+
const hasDifficultyMatch = await processMatch(
148+
redisClient,
149+
requestorParams,
150+
difficultyMatches,
151+
'difficulty'
152+
);
153+
154+
if (!hasDifficultyMatch) {
155+
// To allow cancellation
156+
io.sockets.in(requestorSocketPort).emit('Waiting');
157+
}
140158
}
141159
}
142160
}

0 commit comments

Comments
 (0)