Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions matching-service-api/transport/request_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand Down Expand Up @@ -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",
})
}
}
61 changes: 30 additions & 31 deletions matching-service/storage/client_mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,23 @@ 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,
}

}

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
Expand All @@ -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 {
Expand All @@ -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,
}

Expand All @@ -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
}

Expand Down Expand Up @@ -172,4 +171,4 @@ func generateRoomId() (string, error) {
}

return hex.EncodeToString(bytes), nil
}
}
4 changes: 2 additions & 2 deletions matching-service/storage/room_mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
1 change: 1 addition & 0 deletions peerprep/components/questionpage/Matchmaking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down