@@ -19,15 +19,14 @@ const cancel = () => {
1919} ;
2020const shutdown = ( ) => {
2121 cancel ( ) ;
22- client
23- . disconnect ( )
24- . then ( ( ) => client . quit ( ) )
25- . then ( process . exit ( 0 ) ) ;
22+ client . disconnect ( ) . then ( ( ) => {
23+ process . exit ( 0 ) ;
24+ } ) ;
2625} ;
2726
28- process . on ( 'exit' , shutdown ) ;
2927process . on ( 'SIGINT' , shutdown ) ;
3028process . on ( 'SIGTERM' , shutdown ) ;
29+ process . on ( 'exit' , shutdown ) ;
3130
3231type RequestorParams = {
3332 requestorUserId : string ;
@@ -42,32 +41,36 @@ async function processMatch(
4241 searchIdentifier ?: string
4342) {
4443 if ( matches . total > 0 ) {
45- const matched = matches . documents [ 0 ] ;
46- const {
47- userId : matchedUserId ,
48- timestamp, // We use timestamp as the Stream ID
49- socketPort : matchedSocketPort ,
50- } = decodePoolTicket ( matched ) ;
51-
52- // To block cancellation
53- sendNotif ( [ matchedSocketPort ] , MATCH_SVC_EVENT . MATCHING ) ;
54-
55- const matchedStreamId = getStreamId ( timestamp ) ;
56-
57- logger . info ( `Found match: ${ JSON . stringify ( matched ) } ` ) ;
58-
59- await Promise . all ( [
60- // Remove other from pool
61- redisClient . del ( [ getPoolKey ( requestorUserId ) , getPoolKey ( matchedUserId ) ] ) ,
62- // Remove other from queue
63- redisClient . xDel ( STREAM_NAME , [ requestorStreamId , matchedStreamId ] ) ,
64- ] ) ;
65-
66- // Notify both sockets
67- const { ...matchItems } = getMatchItems ( ) ;
68- sendNotif ( [ requestorSocketPort , matchedSocketPort ] , MATCH_SVC_EVENT . SUCCESS , matchItems ) ;
69- sendNotif ( [ requestorSocketPort , matchedSocketPort ] , MATCH_SVC_EVENT . DISCONNECT ) ;
70- return true ;
44+ for ( const matched of matches . documents ) {
45+ const {
46+ userId : matchedUserId ,
47+ timestamp, // We use timestamp as the Stream ID
48+ socketPort : matchedSocketPort ,
49+ } = decodePoolTicket ( matched ) ;
50+ if ( matchedUserId === requestorUserId ) {
51+ continue ;
52+ }
53+
54+ // To block cancellation
55+ sendNotif ( [ matchedSocketPort ] , MATCH_SVC_EVENT . MATCHING ) ;
56+
57+ const matchedStreamId = getStreamId ( timestamp ) ;
58+
59+ logger . info ( `Found match: ${ JSON . stringify ( matched ) } ` ) ;
60+
61+ await Promise . all ( [
62+ // Remove other from pool
63+ redisClient . del ( [ getPoolKey ( requestorUserId ) , getPoolKey ( matchedUserId ) ] ) ,
64+ // Remove other from queue
65+ redisClient . xDel ( STREAM_NAME , [ requestorStreamId , matchedStreamId ] ) ,
66+ ] ) ;
67+
68+ // Notify both sockets
69+ const { ...matchItems } = getMatchItems ( ) ;
70+ sendNotif ( [ requestorSocketPort , matchedSocketPort ] , MATCH_SVC_EVENT . SUCCESS , matchItems ) ;
71+ sendNotif ( [ requestorSocketPort , matchedSocketPort ] , MATCH_SVC_EVENT . DISCONNECT ) ;
72+ return true ;
73+ }
7174 }
7275
7376 logger . info ( `Found no matches` + ( searchIdentifier ? ` for ${ searchIdentifier } ` : '' ) ) ;
0 commit comments