|
| 1 | +package processes |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/rand" |
| 6 | + "encoding/hex" |
| 7 | + "fmt" |
| 8 | + "matching-service/models" |
| 9 | + |
| 10 | + "github.com/redis/go-redis/v9" |
| 11 | +) |
| 12 | + |
| 13 | +var ctx = context.Background() |
| 14 | + |
| 15 | +func generateMatchID() (string, error) { |
| 16 | + // Create a byte slice to hold random data |
| 17 | + b := make([]byte, 16) // 16 bytes = 128 bits |
| 18 | + _, err := rand.Read(b) |
| 19 | + if err != nil { |
| 20 | + return "", err |
| 21 | + } |
| 22 | + |
| 23 | + // Encode the byte slice to a hexadecimal string |
| 24 | + matchID := hex.EncodeToString(b) |
| 25 | + return matchID, nil |
| 26 | +} |
| 27 | + |
| 28 | +func enqueueUser(client *redis.Client, request models.MatchRequest) error { |
| 29 | + // Generate the key for storing in redis |
| 30 | + key := fmt.Sprintf("queue:%s:%s", request.Topics, request.Difficulties) |
| 31 | + |
| 32 | + // Concatenate the username,email,port |
| 33 | + value := fmt.Sprintf("%s,%s,%s", request.Username, request.Email, request.Port) |
| 34 | + |
| 35 | + // Push the user into the matching queue |
| 36 | + return client.LPush(ctx, key, value).Err() |
| 37 | +} |
| 38 | + |
| 39 | +func dequeueUser(client *redis.Client, request models.MatchRequest) error { |
| 40 | + // Generate the key for storing in redis |
| 41 | + key := fmt.Sprintf("queue:%s:%s", request.Topics, request.Difficulties) |
| 42 | + |
| 43 | + value := fmt.Sprintf("%s,%s,%s", request.Username, request.Email, request.Port) |
| 44 | + |
| 45 | + // Remove user from the matching queue |
| 46 | + _, err := client.LRem(ctx, key, 1, value).Result() |
| 47 | + return err |
| 48 | +} |
| 49 | + |
| 50 | +func matchUser(client *redis.Client, request models.MatchRequest) (string, error) { |
| 51 | + key := fmt.Sprintf("queue:%s:%s", request.Topics, request.Difficulties) |
| 52 | + value := fmt.Sprintf("%s,%s,%s", request.Username, request.Email, request.Port) |
| 53 | + |
| 54 | + // Check if the user is already matched |
| 55 | + exists, err := client.HExists(ctx, value, "username").Result() |
| 56 | + if err != nil { |
| 57 | + return "", err |
| 58 | + } |
| 59 | + |
| 60 | + if exists { |
| 61 | + // User is already matched, retrieve their details |
| 62 | + matchedUser, err := client.HGetAll(ctx, value).Result() |
| 63 | + if err != nil { |
| 64 | + return "", err |
| 65 | + } |
| 66 | + |
| 67 | + // Remove from Redis once retrieved |
| 68 | + _, err = client.Del(ctx, value).Result() |
| 69 | + if err != nil { |
| 70 | + return "", err |
| 71 | + } |
| 72 | + |
| 73 | + return fmt.Sprintf("%s,%s,%s,%s", matchedUser["username"], matchedUser["email"], matchedUser["port"], matchedUser["matchid"]), nil |
| 74 | + } |
| 75 | + |
| 76 | + match, err := client.RPop(ctx, key).Result() |
| 77 | + |
| 78 | + if err == redis.Nil { |
| 79 | + // No match found, enqueue user and return nil |
| 80 | + enqueueUser(client, request) |
| 81 | + return "", nil |
| 82 | + } else if err != nil { |
| 83 | + return "", err |
| 84 | + } |
| 85 | + |
| 86 | + // Check if the matched user is the same as the requesting user |
| 87 | + if match == value { |
| 88 | + // If matched user is the same, you can push it back to the queue and try again |
| 89 | + client.LPush(ctx, key, match) // Re-add the matched user back to the queue |
| 90 | + return "", nil |
| 91 | + } |
| 92 | + |
| 93 | + // Randomly create a matchid |
| 94 | + matchID, err := generateMatchID() |
| 95 | + if err != nil { |
| 96 | + fmt.Println("Error generating match ID:", err) |
| 97 | + return "", err |
| 98 | + } |
| 99 | + |
| 100 | + // Store matched user details in a hash |
| 101 | + err = client.HSet(ctx, match, map[string]interface{}{ |
| 102 | + "username": request.Username, |
| 103 | + "email": request.Email, |
| 104 | + "port": request.Port, |
| 105 | + "matchid": matchID, |
| 106 | + }).Err() |
| 107 | + if err != nil { |
| 108 | + return "", err |
| 109 | + } |
| 110 | + |
| 111 | + return fmt.Sprintf("%s,%s", match, matchID), nil |
| 112 | +} |
0 commit comments