@@ -8,8 +8,6 @@ const refreshDuration = 3000; // 5 seconds
8
8
const waitingDuration = 3000 ;
9
9
const matchingDuration = 60000 - waitingDuration ;
10
10
const queueName = 'matchingQueue' ;
11
- const exchangeName = 'matchingExchange' ;
12
- const exchangeType = 'direct' ;
13
11
14
12
let isCancelled = new Set ( ) ;
15
13
let availabilityCache = new Set ( ) ;
@@ -95,12 +93,10 @@ async function findMatch(request) {
95
93
// Add new request into the queue, 'topic' exchange type is used to route the message
96
94
async function addRequestIntoQueue ( channel , criteria , request ) {
97
95
try {
98
- await channel . assertExchange ( exchangeName , exchangeType , { durable : false } ) ;
99
96
await channel . assertQueue ( queueName , { durable : false } ) ;
100
- await channel . bindQueue ( queueName , exchangeName , criteria ) ;
101
97
102
98
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 } ) ;
104
100
availabilityCache . add ( request . id ) ;
105
101
isCancelled . delete ( parseInt ( request . id ) ) ;
106
102
@@ -112,41 +108,25 @@ async function addRequestIntoQueue(channel, criteria, request) {
112
108
}
113
109
}
114
110
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
-
132
111
// Check if there exists a matched pair for the user, else, find a match from the queue
133
112
async function getMatchFromQueue ( channel , criteria , request ) {
134
113
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 } ;
136
119
137
- if ( existingMatch ) {
138
- return existingMatch ;
120
+ } else {
121
+ return listenToMatchingQueue ( channel , criteria , request ) ;
139
122
}
140
- return listenToMatchingQueue ( channel , criteria , request ) ;
141
123
}
142
124
143
125
// Listen to the queue for a matching pair
144
126
async function listenToMatchingQueue ( channel , criteria , request ) {
145
127
try {
146
128
console . log ( `Start matching user ${ request . id } ` ) ;
147
- await channel . assertExchange ( exchangeName , exchangeType , { durable : false } ) ;
148
129
await channel . assertQueue ( queueName , { durable : false } ) ;
149
- await channel . bindQueue ( queueName , exchangeName , criteria ) ;
150
130
151
131
let matched = false ;
152
132
return new Promise ( async ( resolve ) => {
0 commit comments