@@ -10,7 +10,6 @@ import (
1010 "matching-service/models"
1111 pb "matching-service/proto"
1212 "matching-service/servers"
13- "time"
1413
1514 "github.com/redis/go-redis/v9"
1615)
@@ -19,6 +18,7 @@ import (
1918// until a match is found or no match and the user is enqueued to the queue.
2019func PerformMatching (rdb * redis.Client , matchRequest models.MatchRequest , ctx context.Context , errorChan chan error ) {
2120 currentUsername := matchRequest .Username
21+ var matchFound * models.MatchFound
2222
2323 // Obtain lock with retry
2424 lock , err := servers .ObtainRedisLock (ctx )
@@ -39,7 +39,7 @@ func PerformMatching(rdb *redis.Client, matchRequest models.MatchRequest, ctx co
3939 defer databases .PrintMatchingQueue (tx , "After Matchmaking" , ctx )
4040
4141 // Find a matching user if any
42- matchFound , err : = findMatchingUsers (tx , currentUsername , ctx )
42+ matchFound , err = findMatchingUsers (tx , currentUsername , ctx )
4343 if err != nil {
4444 if errors .Is (err , models .NoMatchFound ) {
4545 return nil
@@ -48,42 +48,15 @@ func PerformMatching(rdb *redis.Client, matchRequest models.MatchRequest, ctx co
4848 return err
4949 } else if matchFound != nil && err != models .NoMatchFound {
5050 matchedUsername := matchFound .MatchedUser
51- matchedTopics := matchFound .MatchedTopics
52- matchedDifficulties := matchFound .MatchedDifficulties
5351
5452 // Log down which users got matched
55- log .Printf ("Users %s and %s matched on the topics: %v; with difficulties: %v" , currentUsername , matchedUsername , matchedTopics , matchedDifficulties )
53+ log .Printf ("Match %v: Users %s and %s matched on the topics: %v; with difficulties: %v" ,
54+ matchFound .MatchID , currentUsername , matchedUsername ,
55+ matchFound .MatchedTopics , matchFound .MatchedDifficulties )
5656
5757 // Clean up redis for this match
5858 databases .CleanUpUser (tx , currentUsername , ctx )
5959 databases .CleanUpUser (tx , matchedUsername , ctx )
60-
61- // Query question service to find a question for the match
62- ctx2 , cancel := context .WithTimeout (ctx , time .Second )
63- defer cancel ()
64-
65- question , err := servers .GetGrpcClient ().FindMatchingQuestion (ctx2 , & pb.MatchQuestionRequest {
66- MatchedTopics : matchedTopics ,
67- MatchedDifficulties : matchedDifficulties ,
68- })
69- if err != nil {
70- log .Fatalf ("Could not retrieve question from question-service: %v" , err )
71- }
72-
73- matchQuestionFound := models.MatchQuestionFound {
74- Type : "match_found" ,
75- MatchID : matchFound .MatchID ,
76- User : matchFound .User ,
77- MatchedUser : matchFound .MatchedUser ,
78- MatchedTopics : matchedTopics ,
79- QuestionID : question .QuestionId ,
80- QuestionName : question .QuestionName ,
81- QuestionDifficulty : question .QuestionDifficulty ,
82- QuestionTopics : question .QuestionTopics ,
83- }
84-
85- publishMatch (tx , ctx , currentUsername , matchedUsername , & matchQuestionFound )
86- publishMatch (tx , ctx , matchedUsername , currentUsername , & matchQuestionFound )
8760 }
8861
8962 return nil
@@ -94,18 +67,61 @@ func PerformMatching(rdb *redis.Client, matchRequest models.MatchRequest, ctx co
9467 // transaction failed, no retry
9568 println (fmt .Errorf ("Transaction execution failed: %v" , err ))
9669 }
70+ } else if matchFound != nil {
71+ completeMatch ((* redis .Tx )(rdb .Conn ()), ctx , matchFound )
9772 }
9873}
9974
75+ // Finds the question and publishes it to complete the matching process.
76+ func completeMatch (tx * redis.Tx , ctx context.Context , matchFound * models.MatchFound ) {
77+ matchQuestionFound := queryQuestionService (ctx , matchFound )
78+
79+ log .Printf ("Match %v: Question %v found with topics: %v and difficulty %v" ,
80+ matchFound .MatchID , matchQuestionFound .QuestionID ,
81+ matchQuestionFound .QuestionTopics , matchQuestionFound .QuestionDifficulty )
82+
83+ currentUsername := matchFound .User
84+ matchedUsername := matchFound .MatchedUser
85+
86+ publishMatch (tx , ctx , currentUsername , matchedUsername , matchQuestionFound )
87+ publishMatch (tx , ctx , matchedUsername , currentUsername , matchQuestionFound )
88+ }
89+
10090// Publish a match to the target user's pub/sub channel
10191func publishMatch (tx * redis.Tx , ctx context.Context , targetUser string , otherMatchedUser string , matchFound * models.MatchQuestionFound ) error {
10292 matchFound .User = targetUser
10393 matchFound .MatchedUser = otherMatchedUser
94+
10495 msg , err := json .Marshal (matchFound )
10596 if err != nil {
10697 log .Fatalf ("Could not marshal message: %v" , err )
10798 return err
10899 }
100+
109101 tx .Publish (ctx , targetUser , msg )
102+
110103 return nil
111104}
105+
106+ // Query question service to find a question for the match
107+ func queryQuestionService (ctx context.Context , matchFound * models.MatchFound ) * models.MatchQuestionFound {
108+ question , err := servers .GetGrpcClient ().FindMatchingQuestion (ctx , & pb.MatchQuestionRequest {
109+ MatchedTopics : matchFound .MatchedTopics ,
110+ MatchedDifficulties : matchFound .MatchedDifficulties ,
111+ })
112+ if err != nil {
113+ log .Fatalf ("Could not retrieve question from question-service: %v" , err )
114+ }
115+
116+ return & models.MatchQuestionFound {
117+ Type : "match_question_found" ,
118+ MatchID : matchFound .MatchID ,
119+ User : matchFound .User ,
120+ MatchedUser : matchFound .MatchedUser ,
121+ MatchedTopics : matchFound .MatchedTopics ,
122+ QuestionID : question .QuestionId ,
123+ QuestionName : question .QuestionName ,
124+ QuestionDifficulty : question .QuestionDifficulty ,
125+ QuestionTopics : question .QuestionTopics ,
126+ }
127+ }
0 commit comments