Skip to content

Commit 895d37a

Browse files
committed
Simplify the matching algorithm.
1 parent 8e9778b commit 895d37a

File tree

1 file changed

+8
-28
lines changed

1 file changed

+8
-28
lines changed

peer-prep/src/matching-service/services/matchingService.js

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ const refreshDuration = 3000; // 5 seconds
88
const waitingDuration = 3000;
99
const matchingDuration = 60000 - waitingDuration;
1010
const queueName = 'matchingQueue';
11-
const exchangeName = 'matchingExchange';
12-
const exchangeType = 'direct';
1311

1412
let isCancelled = new Set();
1513
let availabilityCache = new Set();
@@ -95,12 +93,10 @@ async function findMatch(request) {
9593
// Add new request into the queue, 'topic' exchange type is used to route the message
9694
async function addRequestIntoQueue(channel, criteria, request) {
9795
try {
98-
await channel.assertExchange(exchangeName, exchangeType, { durable: false });
9996
await channel.assertQueue(queueName, { durable: false });
100-
await channel.bindQueue(queueName, exchangeName, criteria);
10197

10298
const message = JSON.stringify({ criteria: criteria, request: request });
103-
channel.publish(exchangeName, criteria, Buffer.from(message), { expiration: matchingDuration });
99+
channel.sendToQueue(queueName, Buffer.from(message), { expiration: matchingDuration });
104100
availabilityCache.add(request.id);
105101
isCancelled.delete(parseInt(request.id));
106102

@@ -112,41 +108,25 @@ async function addRequestIntoQueue(channel, criteria, request) {
112108
}
113109
}
114110

115-
// Check if there exists a matched pair for the user
116-
async function getExistingMatchedPair(request) {
117-
console.log(`Checking if user ${request.id} has an ongoing matched pair...`);
118-
119-
return new Promise(async(resolve) => {
120-
const currentPair = await getCurrentMatchedPair(request.id);
121-
122-
if (currentPair) {
123-
const collaboratorId = String(currentPair.id1) === String(request.id) ? currentPair.id2 : currentPair.id1;
124-
return resolve({ stored: true, isMatched: true, id: request.id, collaboratorId: collaboratorId });
125-
126-
} else {
127-
return resolve(null);
128-
}
129-
});
130-
}
131-
132111
// Check if there exists a matched pair for the user, else, find a match from the queue
133112
async function getMatchFromQueue(channel, criteria, request) {
134113
console.log(`Checking if there is a match for user ${request.id} and find match from queue...`);
135-
const existingMatch = await getExistingMatchedPair(request);
114+
const currentPair = await getCurrentMatchedPair(request.id);
115+
116+
if (currentPair) {
117+
const collaboratorId = String(currentPair.id1) === String(request.id) ? currentPair.id2 : currentPair.id1;
118+
return { stored: true, isMatched: true, id: request.id, collaboratorId: collaboratorId };
136119

137-
if (existingMatch) {
138-
return existingMatch;
120+
} else {
121+
return listenToMatchingQueue(channel, criteria, request);
139122
}
140-
return listenToMatchingQueue(channel, criteria, request);
141123
}
142124

143125
// Listen to the queue for a matching pair
144126
async function listenToMatchingQueue(channel, criteria, request) {
145127
try {
146128
console.log(`Start matching user ${request.id}`);
147-
await channel.assertExchange(exchangeName, exchangeType, { durable: false });
148129
await channel.assertQueue(queueName, { durable: false });
149-
await channel.bindQueue(queueName, exchangeName, criteria);
150130

151131
let matched = false;
152132
return new Promise(async(resolve) => {

0 commit comments

Comments
 (0)