Skip to content

Commit

Permalink
Modify PerformMatching algorithm and NotifyUser function
Browse files Browse the repository at this point in the history
  • Loading branch information
bensohh committed Oct 19, 2024
1 parent fea8537 commit d1908ef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
17 changes: 5 additions & 12 deletions apps/matching-service/handlers/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ func waitForResult(ws *websocket.Conn, ctx, timeoutCtx, matchCtx context.Context
return
}
log.Println("Match found for user: " + username)
// Notify the users about the match
notifyMatches(result.User, result.MatchedUser, result)
// Notify the user about the match
notifyMatches(result.User, result)

// NOTE: user and other user are already cleaned up in a separate matching algorithm process
// so no clean up is required here.
Expand All @@ -187,22 +187,15 @@ func sendTimeoutResponse(ws *websocket.Conn) {
}
}

func notifyMatches(username, matchedUsername string, result models.MatchFound) {
// Notify matches
func notifyMatches(username string, result models.MatchFound) {
mu.Lock()
defer mu.Unlock()

// Send message to the first user
// Send message to matched user
if userConn, userExists := activeConnections[username]; userExists {
if err := userConn.WriteJSON(result); err != nil {
log.Printf("Error sending message to user %s: %v\n", username, err)
}
}

// Send message to the matched user
if matchedUserConn, matchedUserExists := activeConnections[matchedUsername]; matchedUserExists {
result.User, result.MatchedUser = result.MatchedUser, result.User // Swap User and MatchedUser values
if err := matchedUserConn.WriteJSON(result); err != nil {
log.Printf("Error sending message to user %s: %v\n", username, err)
}
}
}
12 changes: 11 additions & 1 deletion apps/matching-service/processes/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func PerformMatching(matchRequest models.MatchRequest, ctx context.Context, matc
log.Println("Unable to randomly generate matchID")
}

// Signal that a match has been found
// Signal that a match has been found for user
matchFoundChannels[username] <- models.MatchFound{
Type: "match_found",
MatchID: matchId,
Expand All @@ -80,6 +80,16 @@ func PerformMatching(matchRequest models.MatchRequest, ctx context.Context, matc
Difficulty: matchedDifficulty,
}

// Signal that a match has been found for matchedUser
matchFoundChannels[matchedUsername] <- models.MatchFound{
Type: "match_found",
MatchID: matchId,
User: matchedUsername,
MatchedUser: username,
Topic: matchedTopic,
Difficulty: matchedDifficulty,
}

} else {
// log.Printf("No match found for user: %s", username)

Expand Down

0 comments on commit d1908ef

Please sign in to comment.