Skip to content

Commit fc92c7c

Browse files
committed
Add ability to store in redis
1 parent b3ea591 commit fc92c7c

File tree

9 files changed

+302
-54
lines changed

9 files changed

+302
-54
lines changed

matching-service/consumer/begin_consuming.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package consumer
22

33
import (
44
"matching-service/models"
5-
db "matching-service/mappings"
5+
db "matching-service/storage"
66
)
77

8-
func BeginConsuming(mq *models.MessageQueue, logger *models.Logger, mappings *db.Mappings) {
8+
func BeginConsuming(mq *models.MessageQueue, logger *models.Logger, clientMappings *db.ClientMappings, roomMappings *db.RoomMappings) {
99
logger.Log.Info("Begin processing requests")
1010

1111
msgs, err := mq.Channel.Consume(
@@ -26,7 +26,7 @@ func BeginConsuming(mq *models.MessageQueue, logger *models.Logger, mappings *db
2626

2727
go func() {
2828
for req := range msgs {
29-
if err := Process(req, mappings); err != nil {
29+
if err := Process(req, clientMappings); err != nil {
3030
logger.Log.Error("Unable to convert request from JSON:" + err.Error())
3131
}
3232
}

matching-service/consumer/process_request.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,31 @@ package consumer
77

88
import (
99
"encoding/json"
10-
db "matching-service/mappings"
10+
db "matching-service/storage"
1111
"matching-service/models"
1212

13-
rabbit "github.com/streadway/amqp"
1413
"fmt"
14+
15+
rabbit "github.com/streadway/amqp"
1516
)
1617

17-
func Process(msg rabbit.Delivery, mappings *db.Mappings) error {
18-
var request models.Requests
18+
func Process(msg rabbit.Delivery, mappings *db.ClientMappings) error {
19+
var request models.IncomingRequests
1920

2021
if err := json.Unmarshal(msg.Body, &request); err != nil {
2122
return err
2223
}
2324

24-
2525
room, err := mappings.HandleRequest(request)
2626

27-
fmt.Println("handled!")
2827
if err != nil {
2928
return err
3029
}
3130

3231
//deliver the response to the backend
3332
//TODO: to implement this
34-
if room != nil {}
33+
if room != nil {
34+
35+
}
3536
return nil
36-
}
37+
}

matching-service/main.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"log"
55
"os"
66

7-
models "matching-service/models"
8-
"matching-service/mappings"
97
"matching-service/consumer"
8+
models "matching-service/models"
9+
"matching-service/storage"
10+
1011
"github.com/joho/godotenv"
1112
rabbit "github.com/streadway/amqp"
1213

@@ -92,9 +93,21 @@ func main() {
9293
logger, logFile := initialiseLogger()
9394

9495
defer logFile.Close()
95-
storage := mappings.CreateMappings()
96+
//TODO: remove this
97+
//deprecatedStorage := mappings.CreateMappings()
98+
99+
100+
REDIS_URI := os.Getenv("REDIS_URI")
101+
102+
if REDIS_URI == "" {
103+
REDIS_URI = "localhost://9190"
104+
}
105+
106+
clientMappings := storage.InitialiseClientMappings(REDIS_URI, 0)
107+
roomMappings := storage.InitialiseRoomMappings(REDIS_URI, 1)
108+
96109

97110
logger.Log.Info("Beginning consumption from message queue")
98-
consumer.BeginConsuming(mq, logger, storage)
111+
consumer.BeginConsuming(mq, logger, clientMappings, roomMappings)
99112

100113
}
Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//this file is deprecated
12
package mappings
23

34
import (
@@ -7,25 +8,25 @@ import (
78
"fmt"
89
"io"
910
"matching-service/models"
10-
1111
)
1212

13-
//TODO: consider using redis to store this information instead
13+
// TODO: consider using redis to store this information instead
1414
type Mappings struct {
15-
Topics map[string][]string
15+
Topics map[string][]string
1616
Difficulty map[string]string
1717
}
1818

1919
func CreateMappings() *Mappings {
2020
return &Mappings{
21-
Topics: make(map[string][]string),
21+
Topics: make(map[string][]string),
2222
Difficulty: make(map[string]string),
2323
}
2424
}
25-
//TODO: implement logic to implement TTL for values
26-
//logic to find matching categories and generates a room id for the 2 users
27-
func (db *Mappings) HandleRequest(request models.Requests) (*models.Room, error){
28-
for user1, topics:= range db.Topics {
25+
26+
// TODO: implement logic to implement TTL for values
27+
// logic to find matching categories and generates a room id for the 2 users
28+
func (db *Mappings) HandleRequest(request models.IncomingRequests) (*models.Room, error) {
29+
for user1, topics := range db.Topics {
2930
if difficulty, exists := db.Difficulty[user1]; !exists {
3031
return nil, fmt.Errorf("user %s only exists in topics store and not in difficulty store", user1)
3132
} else if difficulty != request.Difficulty {
@@ -37,8 +38,8 @@ func (db *Mappings) HandleRequest(request models.Requests) (*models.Room, error)
3738
// user1 does not match with this user
3839
if len(overlapping) == 0 {
3940
continue
40-
}
41-
41+
}
42+
4243
//match found, generate room Id and return the room
4344
if roomId, err := generateRoomId(); err != nil {
4445
return nil, err
@@ -48,20 +49,20 @@ func (db *Mappings) HandleRequest(request models.Requests) (*models.Room, error)
4849
delete(db.Difficulty, user1)
4950

5051
return &models.Room{
51-
RoomId: roomId,
52-
User1: user1,
53-
User2: request.UserId,
54-
TopicTags: overlapping,
52+
RoomId: roomId,
53+
User1: user1,
54+
User2: request.UserId,
55+
TopicTags: overlapping,
5556
Difficulty: request.Difficulty,
5657
}, nil
57-
}
58+
}
5859
}
5960

6061
//no match found
6162
//add user2 to the mappings
6263
db.Topics[request.UserId] = request.TopicTags
6364
db.Difficulty[request.UserId] = request.Difficulty
64-
65+
6566
return nil, nil
6667
}
6768

@@ -76,22 +77,22 @@ func generateRoomId() (string, error) {
7677
}
7778

7879
func findOverLap(user1 []string, user2 []string) []string {
79-
80+
8081
stringMap := make(map[string]bool)
81-
var commonStrings []string
82-
83-
// Store each string from slice1 in the map
84-
for _, topic := range user1 {
85-
stringMap[topic] = true
86-
}
87-
88-
// Iterate over slice2 and check for common strings
89-
for _, topic := range user2 {
90-
if stringMap[topic] {
91-
commonStrings = append(commonStrings, topic)
92-
delete(stringMap, topic) // Remove to avoid duplicates in result
93-
}
94-
}
95-
96-
return commonStrings
97-
}
82+
var commonStrings []string
83+
84+
// Store each string from slice1 in the map
85+
for _, topic := range user1 {
86+
stringMap[topic] = true
87+
}
88+
89+
// Iterate over slice2 and check for common strings
90+
for _, topic := range user2 {
91+
if stringMap[topic] {
92+
commonStrings = append(commonStrings, topic)
93+
delete(stringMap, topic) // Remove to avoid duplicates in result
94+
}
95+
}
96+
97+
return commonStrings
98+
}
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package models
22

3-
type Requests struct {
4-
UserId string `json:"userId"`
5-
TopicTags []string `json:"topicTags"`
6-
Difficulty string `json:"difficulty"`
7-
RequestTime string `json:"requestTime"`
3+
type IncomingRequests struct {
4+
UserId string `json:"userId"`
5+
TopicTags []string `json:"topicTags"`
6+
Difficulty string `json:"difficulty"`
7+
RequestTime string `json:"requestTime"`
88
}
9+
10+
type OutGoingRequests struct {
11+
TopicTags []string `json:"topicTags"`
12+
Difficulty string `json:"difficulty"`
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//representation of the question returned as a response
2+
package models
3+
4+
5+
type Question struct {
6+
Title string `json:"title"`
7+
TitleSlug string `json:"titleSlug"`
8+
Difficulty string `json:"difficulty"`
9+
TopicTags []string `json:"topicTags"`
10+
Content string `json:"content"`
11+
Schemas []string `json:"schemas"`
12+
Id int `json:"id"`
13+
}

0 commit comments

Comments
 (0)