@@ -19,15 +19,14 @@ const cancel = () => {
19
19
} ;
20
20
const shutdown = ( ) => {
21
21
cancel ( ) ;
22
- client
23
- . disconnect ( )
24
- . then ( ( ) => client . quit ( ) )
25
- . then ( process . exit ( 0 ) ) ;
22
+ client . disconnect ( ) . then ( ( ) => {
23
+ process . exit ( 0 ) ;
24
+ } ) ;
26
25
} ;
27
26
28
- process . on ( 'exit' , shutdown ) ;
29
27
process . on ( 'SIGINT' , shutdown ) ;
30
28
process . on ( 'SIGTERM' , shutdown ) ;
29
+ process . on ( 'exit' , shutdown ) ;
31
30
32
31
type RequestorParams = {
33
32
requestorUserId : string ;
@@ -42,32 +41,36 @@ async function processMatch(
42
41
searchIdentifier ?: string
43
42
) {
44
43
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
+ }
71
74
}
72
75
73
76
logger . info ( `Found no matches` + ( searchIdentifier ? ` for ${ searchIdentifier } ` : '' ) ) ;
0 commit comments