Skip to content

Commit a4d07a6

Browse files
committed
Change blob to be queried by a random hash instead
This allows a better platform for the collab service to persist/reconnect users through.
1 parent 79276ed commit a4d07a6

File tree

9 files changed

+69
-19
lines changed

9 files changed

+69
-19
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package mappings
2+
3+
import (
4+
"crypto/rand"
5+
"encoding/hex"
6+
"errors"
7+
"io"
8+
)
9+
10+
func GenerateMatchingHash() (string, error) {
11+
bytes := make([]byte, 16)
12+
13+
if _, err := io.ReadFull(rand.Reader, bytes); err != nil {
14+
return "", errors.New("Failed to generate random matching hash" + err.Error())
15+
}
16+
17+
return hex.EncodeToString(bytes), nil
18+
}

matching-service-api/models/request.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package models
22

33
type Request struct {
4+
MatchHash string
5+
46
UserId string `json:"userId"`
57

68
TopicTags []string `json:"topicTags"`

matching-service-api/transport/request_handler.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"matching-service-api/models"
7+
"matching-service-api/mappings"
78
"net/http"
89
"time"
910

@@ -29,6 +30,16 @@ func HandleRequest(channel *models.ProducerQueue, logger *models.Logger) gin.Han
2930
return
3031
}
3132

33+
// adding this matching hash to reserve user-id as a store for
34+
// persistence.
35+
matchHash, err := mappings.GenerateMatchingHash();
36+
if err != nil {
37+
logger.Log.Error("Error: " + err.Error())
38+
ctx.JSON(http.StatusInternalServerError, "failed to generate query hash")
39+
return
40+
}
41+
req.MatchHash = matchHash;
42+
3243
//current time is more than 30 seconds after request time, timeout
3344
if time.Now().After(parsedTime.Add(30 * time.Second).Add(-8 * time.Hour)) {
3445
logger.Log.Warn("request timeout")
@@ -59,6 +70,9 @@ func HandleRequest(channel *models.ProducerQueue, logger *models.Logger) gin.Han
5970
}
6071

6172
logger.Log.Info(fmt.Sprintf("request from user %s successfully published", req.UserId))
62-
ctx.JSON(http.StatusOK, "processing request")
73+
ctx.JSON(http.StatusOK, gin.H{
74+
"match_code": matchHash,
75+
"message": "processing request",
76+
})
6377
}
6478
}

matching-service/models/matching_request.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package models
22

33
type IncomingRequests struct {
4+
MatchHash string `json:"matchHash"`
45
UserId string `json:"userId"`
56
TopicTags []string `json:"topicTags"`
67
Difficulty string `json:"difficulty"`

matching-service/models/room.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package models
22

33
type Room struct {
4+
// stores what key to ship the resulting blob to
5+
MatchHash1 string `json:"matchHash1"`
6+
MatchHash2 string `json:"matchHash2"`
47

8+
// user information
59
RoomId string `json:"roomId"`
610
User1 string `json:"user1"`
711
User2 string `json:"user2"`

matching-service/storage/client_mappings.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ func InitialiseClientMappings(addr string, db_num int) *ClientMappings {
3333

3434
func (db *ClientMappings) HandleRequest(request models.IncomingRequests) (*models.Room, error){
3535
ctx := context.Background()
36-
user2, user2_difficulty, user2_topics, user2_requestTime := request.UserId, request.Difficulty, request.TopicTags, request.RequestTime
36+
user2, user2_difficulty, user2_topics := request.UserId, request.Difficulty, request.TopicTags
37+
user2_requestTime, user2_matchHash := request.RequestTime, request.MatchHash
3738

3839
currMappings, err := db.Conn.Keys(ctx, "*").Result()
3940

@@ -67,17 +68,20 @@ func (db *ClientMappings) HandleRequest(request models.IncomingRequests) (*model
6768
if len(overlappingTopics) == 0 {
6869
continue
6970
}
70-
7171

7272
roomId, err := generateRoomId()
7373

7474
if err != nil {
7575
return nil, err
7676
}
7777

78+
user1_matchHash := result["matchHash"]
79+
7880
db.Conn.Del(ctx, user1)
7981

8082
room := models.Room{
83+
MatchHash1: user1_matchHash,
84+
MatchHash2: user2_matchHash,
8185
RoomId: roomId,
8286
User1: user1,
8387
User2: user2,
@@ -106,6 +110,7 @@ func (db *ClientMappings) HandleRequest(request models.IncomingRequests) (*model
106110
}
107111

108112
err = db.Conn.HSet(ctx, user2, map[string]interface{}{
113+
"matchHash": user2_matchHash,
109114
"topicTags": user2_topics_json,
110115
"difficulty": user2_difficulty,
111116
"requestTime": user2_requestTime,

matching-service/storage/room_mappings.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ func (db *RoomMappings) SendToStorageBlob(room *models.Room) error {
4040
return fmt.Errorf("error marshling topics: %s", err.Error())
4141
}
4242

43+
// this is where the value is being set
4344
user1_info := map[string]interface{}{
4445
"roomId": room.RoomId,
46+
"thisUser": room.User1,
4547
"otherUser": room.User2,
4648
"requestTime": room.RequestTime,
4749

@@ -56,6 +58,7 @@ func (db *RoomMappings) SendToStorageBlob(room *models.Room) error {
5658

5759
user2_info := map[string]interface{}{
5860
"roomId": room.RoomId,
61+
"thisUser": room.User2,
5962
"otherUser": room.User1,
6063
"requestTime": room.RequestTime,
6164

@@ -68,11 +71,12 @@ func (db *RoomMappings) SendToStorageBlob(room *models.Room) error {
6871
"id": room.QuestionId,
6972
}
7073

71-
if err1 := db.Conn.HSet(ctx, room.User1, user1_info).Err(); err1 != nil {
74+
// TODO: Modify this - this is where the key-value is being set
75+
if err1 := db.Conn.HSet(ctx, room.MatchHash1, user1_info).Err(); err1 != nil {
7276
return fmt.Errorf("error setting user1's room to storage: %s", err1.Error())
7377
}
7478

75-
if err2 := db.Conn.HSet(ctx, room.User2, user2_info).Err(); err2 != nil {
79+
if err2 := db.Conn.HSet(ctx, room.MatchHash2, user2_info).Err(); err2 != nil {
7680
return fmt.Errorf("error setting user2's room to storage: %s", err2.Error())
7781
}
7882

@@ -86,11 +90,11 @@ func (db *RoomMappings) SendToStorageBlob(room *models.Room) error {
8690

8791
diff := int(time.Until(expiryTime).Seconds())
8892

89-
if err1 := db.Conn.Expire(ctx, room.User1, time.Duration(diff)*time.Second).Err(); err1 != nil {
93+
if err1 := db.Conn.Expire(ctx, room.MatchHash1, time.Duration(diff)*time.Second).Err(); err1 != nil {
9094
return fmt.Errorf("error setting expiry time on room data: %s", err1.Error())
9195
}
9296

93-
if err2 := db.Conn.Expire(ctx, room.User2, time.Duration(diff)*time.Second).Err(); err2 != nil {
97+
if err2 := db.Conn.Expire(ctx, room.MatchHash2, time.Duration(diff)*time.Second).Err(); err2 != nil {
9498
return fmt.Errorf("error setting expiry time on room data: %s", err2.Error())
9599
}
96100

storage-blob-api/transport/endpoint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func SetAllEndpoints(router *gin.Engine, db *storage.RoomMappings, logger *models.Logger) {
13-
router.GET("/request/:userId", HandleRequest(db, logger))
13+
router.GET("/request/:matchHash", HandleRequest(db, logger))
1414
}
1515

1616
func SetCors(router *gin.Engine, origin string) {

storage-blob-api/transport/request_handler.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ import (
1515

1616
func HandleRequest(db *storage.RoomMappings, logger *models.Logger) (gin.HandlerFunc){
1717
return func(ctx *gin.Context) {
18-
userId := ctx.Param("userId")
18+
matchHash := ctx.Param("matchHash")
1919

20-
result, err := db.Conn.HGetAll(context.Background(), userId).Result()
20+
result, err := db.Conn.HGetAll(context.Background(), matchHash).Result()
2121

2222
if err == redis.Nil {
23-
logger.Log.Warn(fmt.Sprintf("userId %s expired: ", userId))
24-
ctx.JSON(http.StatusGone, "userId has expired")
23+
logger.Log.Warn(fmt.Sprintf("matchHash %s expired: ", matchHash))
24+
ctx.JSON(http.StatusGone, "matchHash has expired")
2525
return
2626
} else if err != nil {
27-
logger.Log.Error(fmt.Errorf("error retrieving userId from database: %s", err.Error()))
28-
ctx.JSON(http.StatusBadGateway, "error retriving userId from database")
27+
logger.Log.Error(fmt.Errorf("error retrieving matchHash from database: %s", err.Error()))
28+
ctx.JSON(http.StatusBadGateway, "error retriving matchHash from database")
2929
return
3030
}
3131

@@ -49,10 +49,12 @@ func HandleRequest(db *storage.RoomMappings, logger *models.Logger) (gin.Handler
4949
return
5050
}
5151

52-
roomId, user2, requestTime, title, titleSlug, difficulty, content, questionId_string :=
53-
result["roomId"], result["otherUser"], result["requestTime"],
54-
result["title"], result["titleSlug"], result["difficulty"], result["content"], result["id"]
55-
52+
roomId, user1, user2, requestTime :=
53+
result["roomId"], result["thisUser"], result["otherUser"], result["requestTime"]
54+
55+
56+
title, titleSlug, difficulty, content, questionId_string :=
57+
result["title"], result["titleSlug"], result["difficulty"], result["content"], result["id"]
5658
questionId, err := strconv.Atoi(questionId_string)
5759

5860
if err != nil {
@@ -63,7 +65,7 @@ func HandleRequest(db *storage.RoomMappings, logger *models.Logger) (gin.Handler
6365

6466
room := models.Room{
6567
RoomId: roomId,
66-
User1: userId,
68+
User1: user1,
6769
User2: user2,
6870
RequestTime: requestTime,
6971

0 commit comments

Comments
 (0)