diff --git a/matching-service-api/transport/request_handler.go b/matching-service-api/transport/request_handler.go index ca6424be5b..5568bb207c 100644 --- a/matching-service-api/transport/request_handler.go +++ b/matching-service-api/transport/request_handler.go @@ -3,8 +3,8 @@ package transport import ( "encoding/json" "fmt" - "matching-service-api/models" "matching-service-api/mappings" + "matching-service-api/models" "net/http" "time" @@ -22,26 +22,29 @@ func HandleRequest(channel *models.ProducerQueue, logger *models.Logger) gin.Han return } - parsedTime, err := time.Parse("2006-01-02 15-04-05", req.RequestTime) + parsedTime, err := time.ParseInLocation("2006-01-02 15-04-05", req.RequestTime, time.UTC) if err != nil { logger.Log.Error("error parsing the time: ", err.Error()) - ctx.JSON(http.StatusBadRequest, "error parsing time, ensure time is parsed in YYYY-MM-DD HH:mm:ss format") + ctx.JSON( + http.StatusBadRequest, + "error parsing time, ensure time is parsed in YYYY-MM-DD HH:mm:ss format", + ) return } // adding this matching hash to reserve user-id as a store for // persistence. - matchHash, err := mappings.GenerateMatchingHash(); + matchHash, err := mappings.GenerateMatchingHash() if err != nil { logger.Log.Error("Error: " + err.Error()) ctx.JSON(http.StatusInternalServerError, "failed to generate query hash") return } - req.MatchHash = matchHash; + req.MatchHash = matchHash //current time is more than 30 seconds after request time, timeout - if time.Now().After(parsedTime.Add(30 * time.Second).Add(-8 * time.Hour)) { + if time.Now().UTC().After(parsedTime.Add(30 * time.Second)) { logger.Log.Warn("request timeout") ctx.JSON(http.StatusRequestTimeout, "request time is too old") return @@ -72,7 +75,7 @@ func HandleRequest(channel *models.ProducerQueue, logger *models.Logger) gin.Han logger.Log.Info(fmt.Sprintf("request from user %s successfully published", req.UserId)) ctx.JSON(http.StatusOK, gin.H{ "match_code": matchHash, - "message": "processing request", + "message": "processing request", }) } } diff --git a/matching-service/storage/client_mappings.go b/matching-service/storage/client_mappings.go index 5622044e23..52beb40e35 100644 --- a/matching-service/storage/client_mappings.go +++ b/matching-service/storage/client_mappings.go @@ -14,16 +14,15 @@ import ( redis "github.com/go-redis/redis/v8" ) - type ClientMappings struct { Conn *redis.Client } func InitialiseClientMappings(addr string, db_num int) *ClientMappings { conn := redis.NewClient(&redis.Options{ - Addr:addr, - DB: db_num, - }) + Addr: addr, + DB: db_num, + }) return &ClientMappings{ Conn: conn, @@ -31,7 +30,7 @@ func InitialiseClientMappings(addr string, db_num int) *ClientMappings { } -func (db *ClientMappings) HandleRequest(request models.IncomingRequests) (*models.Room, error){ +func (db *ClientMappings) HandleRequest(request models.IncomingRequests) (*models.Room, error) { ctx := context.Background() user2, user2_difficulty, user2_topics := request.UserId, request.Difficulty, request.TopicTags user2_requestTime, user2_matchHash := request.RequestTime, request.MatchHash @@ -43,12 +42,12 @@ func (db *ClientMappings) HandleRequest(request models.IncomingRequests) (*model } for _, user1 := range currMappings { - + if user1 == user2 { //users cannot match with themselves continue } - + result, err := db.Conn.HGetAll(ctx, user1).Result() if err == redis.Nil { @@ -64,33 +63,33 @@ func (db *ClientMappings) HandleRequest(request models.IncomingRequests) (*model user1_difficulty := result["difficulty"] user1_requestTime := result["requestTime"] - + if user1_difficulty != user2_difficulty { continue } overlappingTopics := findOverlap(user1_topics, user2_topics) - + if len(overlappingTopics) == 0 { continue - } - + } + roomId, err := generateRoomId() - + if err != nil { return nil, err - } - + } + user1_matchHash := result["matchHash"] - + db.Conn.Del(ctx, user1) - + room := models.Room{ - MatchHash1: user1_matchHash, - MatchHash2: user2_matchHash, - RoomId: roomId, - User1: user1, - User2: user2, + MatchHash1: user1_matchHash, + MatchHash2: user2_matchHash, + RoomId: roomId, + User1: user1, + User2: user2, RequestTime: user1_requestTime, } @@ -110,33 +109,33 @@ func (db *ClientMappings) HandleRequest(request models.IncomingRequests) (*model //no match found user2_topics_json, err := json.Marshal(user2_topics) - + if err != nil { return nil, err } err = db.Conn.HSet(ctx, user2, map[string]interface{}{ - "matchHash": user2_matchHash, - "topicTags": user2_topics_json, - "difficulty": user2_difficulty, + "matchHash": user2_matchHash, + "topicTags": user2_topics_json, + "difficulty": user2_difficulty, "requestTime": user2_requestTime, - }).Err() + }).Err() if err != nil { return nil, err } - requestTime, err := time.Parse("2006-01-02 15-04-05", user2_requestTime) + requestTime, err := time.ParseInLocation("2006-01-02 15-04-05", user2_requestTime, time.UTC) if err != nil { return nil, err } - expiryTime := requestTime.Add(30 * time.Second).Add(-8 * time.Hour) + expiryTime := requestTime.Add(30 * time.Second) diff := int(time.Until(expiryTime).Seconds()) - err = db.Conn.Expire(ctx, user2, time.Duration(diff) * time.Second).Err() + err = db.Conn.Expire(ctx, user2, time.Duration(diff)*time.Second).Err() - if err != nil { + if err != nil { return nil, err } @@ -172,4 +171,4 @@ func generateRoomId() (string, error) { } return hex.EncodeToString(bytes), nil -} \ No newline at end of file +} diff --git a/matching-service/storage/room_mappings.go b/matching-service/storage/room_mappings.go index 47826e2d15..879571d1e7 100644 --- a/matching-service/storage/room_mappings.go +++ b/matching-service/storage/room_mappings.go @@ -80,13 +80,13 @@ func (db *RoomMappings) SendToStorageBlob(room *models.Room) error { return fmt.Errorf("error setting user2's room to storage: %s", err2.Error()) } - requestTime, err := time.Parse("2006-01-02 15-04-05", room.RequestTime) + requestTime, err := time.ParseInLocation("2006-01-02 15-04-05", room.RequestTime, time.UTC) if err != nil { return fmt.Errorf("error parsing the time: %s", err.Error()) } - expiryTime := requestTime.Add(30 * time.Second).Add(-8 * time.Hour) + expiryTime := requestTime.Add(30 * time.Second) diff := int(time.Until(expiryTime).Seconds()) diff --git a/peerprep/components/questionpage/Matchmaking.tsx b/peerprep/components/questionpage/Matchmaking.tsx index c168763088..9ac8bc723f 100644 --- a/peerprep/components/questionpage/Matchmaking.tsx +++ b/peerprep/components/questionpage/Matchmaking.tsx @@ -30,6 +30,7 @@ const getMatchRequestTime = (): string => { minute: "2-digit", second: "2-digit", hour12: false, + timeZone: "UTC", }; const formattedDate = now.toLocaleString("en-CA", options); // gives YYYY-MM-DD, HH:mm:ss