Skip to content

Commit e7c229e

Browse files
committed
PEER-223: Add proper pending flag logic for matcher
Signed-off-by: SeeuSim <[email protected]>
1 parent 7cab281 commit e7c229e

File tree

4 files changed

+14
-25
lines changed

4 files changed

+14
-25
lines changed

backend/matching/src/lib/db/seed.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ const main = async () => {
4949
difficulty: {
5050
type: SchemaFieldTypes.TAG,
5151
},
52+
pending: {
53+
type: SchemaFieldTypes.TEXT,
54+
},
5255
timestamp: {
5356
type: SchemaFieldTypes.NUMERIC,
5457
SORTABLE: true,

backend/matching/src/lib/utils/get-redis-payload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ export const getRedisPayload = (payload: IPoolTicket) => {
1010
? { topic: topic.join(',') }
1111
: { topic }
1212
: {};
13-
return { ...rest, ...topicField, ...difficultyField };
13+
return { ...rest, ...topicField, ...difficultyField, pending: 'true' };
1414
};
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { MATCH_PREFIX, STREAM_NAME } from '@/lib/db/constants';
2-
import { getRedisPayload } from '@/lib/utils';
1+
import { STREAM_NAME } from '@/lib/db/constants';
2+
import { getPoolKey, getRedisPayload } from '@/lib/utils';
33
import type { IQueueRequest, IRedisClient } from '@/types';
44

55
export const queueingService = async (client: IRedisClient, payload: IQueueRequest) => {
66
const formattedPayload = getRedisPayload(payload);
77
// Add to queue
88
await client.xAdd(STREAM_NAME, formattedPayload.timestamp, formattedPayload);
99
// Add to matching pool
10-
await client.hSet(`${MATCH_PREFIX}${payload.userId}`, formattedPayload);
10+
await client.hSet(getPoolKey(payload.userId), formattedPayload);
1111
};

backend/matching/src/workers/matcher.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,11 @@ async function processMatch(
5858
continue;
5959
}
6060

61-
const setPendingToFalse = async (userId: string) => {
62-
await redisClient.hSet(`match:${userId}`, 'pending', 'false');
63-
console.log(`User ${userId} is now in MATCHING state, pending set to false.`);
64-
};
65-
66-
await Promise.all([
67-
setPendingToFalse(requestorUserId), // Set pending to false for requester
68-
setPendingToFalse(matchedUserId), // Set pending to false for matched user
69-
]);
70-
7161
// To block cancellation
7262
sendNotif([matchedSocketPort], MATCHING_EVENT.MATCHING);
63+
await redisClient.hSet(getPoolKey(matchedUserId), 'pending', 'false');
7364

7465
const matchedStreamId = getStreamId(timestamp);
75-
7666
logger.info(`Found match: ${JSON.stringify(matched)}`);
7767

7868
await Promise.all([
@@ -141,8 +131,9 @@ async function match() {
141131

142132
// To Block Cancellation
143133
sendNotif([requestorSocketPort], MATCHING_EVENT.MATCHING);
134+
await redisClient.hSet(getPoolKey(requestorUserId), 'pending', 'false');
144135

145-
const clause = [`-@userId:(${requestorUserId})`];
136+
const clause = [`-@userId:(${requestorUserId}) @pending:(true)`];
146137

147138
if (difficulty) {
148139
clause.push(`@difficulty:{${difficulty}}`);
@@ -176,7 +167,7 @@ async function match() {
176167
// Match on Topic
177168
const topicMatches = await redisClient.ft.search(
178169
POOL_INDEX,
179-
`@topic:{${topic}} -@userId:(${requestorUserId})`,
170+
clause.filter((v) => !v.startsWith('@difficulty')).join(' '),
180171
searchParams
181172
);
182173
const topicMatchFound = await processMatch(
@@ -195,7 +186,7 @@ async function match() {
195186
// Match on Difficulty
196187
const difficultyMatches = await redisClient.ft.search(
197188
POOL_INDEX,
198-
`@difficulty:${difficulty} -@userId:(${requestorUserId})`,
189+
clause.filter((v) => !v.startsWith('@topic')).join(' '),
199190
searchParams
200191
);
201192
const hasDifficultyMatch = await processMatch(
@@ -209,14 +200,9 @@ async function match() {
209200

210201
if (!hasDifficultyMatch) {
211202
// To allow cancellation
203+
await redisClient.hSet(getPoolKey(requestorUserId), 'pending', 'true');
212204
sendNotif([requestorSocketPort], MATCHING_EVENT.PENDING);
213-
214-
const setPendingToTrue = async (userId: string) => {
215-
await redisClient.hSet(`match:${userId}`, 'pending', 'true');
216-
console.log(`User ${userId} is now in PENDING state, pending set to true.`);
217-
};
218-
219-
await setPendingToTrue(requestorUserId);
205+
logger.info(`${requestorUserId} is now in mode ${MATCHING_EVENT.PENDING}`);
220206
}
221207
}
222208
}

0 commit comments

Comments
 (0)