Skip to content

Commit c040b36

Browse files
committed
feat: improve logs
1 parent 922f336 commit c040b36

File tree

6 files changed

+60
-45
lines changed

6 files changed

+60
-45
lines changed

apps/matching-service/handlers/websocket.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func readMatchRequest(ws *websocket.Conn) (models.MatchRequest, error) {
9494
// If user is already removed, then nothing happens.
9595
// This function is unaffected by the external context.
9696
func cleanUpUser(username string) {
97-
redisClient := servers.GetRedisClient()
97+
rdb := servers.GetRedisClient()
9898
ctx := context.Background()
9999

100100
// Obtain lock with retry
@@ -104,13 +104,8 @@ func cleanUpUser(username string) {
104104
}
105105
defer lock.Release(ctx)
106106

107-
if err := redisClient.Watch(ctx, func(tx *redis.Tx) error {
108-
// Cleanup Redis
109-
databases.CleanUpUser(tx, username, ctx)
110-
return nil
111-
}); err != nil {
112-
return
113-
}
107+
// Cleanup Redis
108+
databases.CleanUpUser((*redis.Tx)(rdb.Conn()), username, ctx)
114109
}
115110

116111
// waitForResult waits for a match result, timeout, or cancellation.

apps/matching-service/processes/performmatches.go

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
2019
func 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
10191
func 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+
}

apps/matching-service/servers/grpc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ func InitGrpcServer() *grpc.ClientConn {
1717
conn, err := grpc.NewClient("localhost:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
1818
if err != nil {
1919
log.Fatalf("Did not connect: %v", err)
20+
} else {
21+
log.Println("Connected to Grpc server at :50051")
2022
}
2123

2224
// Create a new client for the ExampleService

apps/question-service/handlers/findmatchingquestion.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package handlers
22

33
import (
44
"context"
5+
"log"
56
pb "proto/questionmatching"
67
)
78

89
func (s *GrpcServer) FindMatchingQuestion(ctx context.Context, req *pb.MatchQuestionRequest) (*pb.QuestionFound, error) {
9-
// STUB
10+
log.Printf("Received find matching question request: %v", req)
1011
return &pb.QuestionFound{
1112
QuestionId: 1,
1213
QuestionName: "abc",

apps/question-service/handlers/list.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package handlers
22

33
import (
4-
"cloud.google.com/go/firestore"
54
"encoding/json"
65
"net/http"
76
"question-service/models"
87
"strconv"
98
"strings"
9+
10+
"cloud.google.com/go/firestore"
1011
)
1112

1213
var isValidSortField = map[string]bool{

apps/question-service/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ func main() {
3333

3434
// Initialize Firestore client
3535
ctx := context.Background()
36-
firebaseCredentialPath := os.Getenv("FIREBASE_CREDENTIAL_PATH")
37-
client, err := initFirestore(ctx, firebaseCredentialPath)
36+
client, err := initFirestore(ctx)
3837
if err != nil {
3938
log.Fatalf("Failed to initialize Firestore client: %v", err)
4039
}
@@ -57,7 +56,8 @@ func main() {
5756
}
5857

5958
// initFirestore initializes the Firestore client
60-
func initFirestore(ctx context.Context, credentialsPath string) (*firestore.Client, error) {
59+
func initFirestore(ctx context.Context) (*firestore.Client, error) {
60+
credentialsPath := os.Getenv("FIREBASE_CREDENTIAL_PATH")
6161
opt := option.WithCredentialsFile(credentialsPath)
6262
app, err := firebase.NewApp(ctx, nil, opt)
6363
if err != nil {

0 commit comments

Comments
 (0)