Skip to content

Commit 666626c

Browse files
Fixed Deadlock code in complete Redis and tested
1 parent ed70708 commit 666626c

File tree

1 file changed

+6
-55
lines changed

1 file changed

+6
-55
lines changed

internal/session/redis.go

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,9 @@ import (
1414
/* TODO: make the operations below thread safe with mutexes*/
1515

1616
/* store session into Redis database */
17-
func (m *Manager) saveSessionRedis(username string) error {
17+
func (m *Manager) saveSessionRedis(session *Session) error {
1818
ctx := context.Background()
1919

20-
/* thread safety for the manager */
21-
m.mutex.Lock()
22-
defer m.mutex.Unlock()
23-
24-
/* find the session in session map */
25-
session, ok := m.sessionsMap[username]
26-
if !ok {
27-
return fmt.Errorf("username not found in session")
28-
}
29-
3020
/* thread safety for the session */
3121
session.Mutex.Lock()
3222
defer session.Mutex.Unlock()
@@ -37,6 +27,7 @@ func (m *Manager) saveSessionRedis(username string) error {
3727
/* serialize the session with relevant information */
3828
sessionSerialized := session.serializeSessionForRedis()
3929

30+
/* hset the session to redis */
4031
if err := m.redis.HSet(ctx, key, sessionSerialized).Err(); err != nil {
4132
return fmt.Errorf("failed to save session to Redis: %w", err)
4233
}
@@ -45,24 +36,14 @@ func (m *Manager) saveSessionRedis(username string) error {
4536
}
4637

4738
/* update expiry time in session */
48-
func (m *Manager) updateSessionExpiryRedis(username string) error {
39+
func (m *Manager) updateSessionExpiryRedis(session *Session) error {
4940

5041
/*
5142
function expects that new expiry time is already set in the session
5243
*/
5344

5445
ctx := context.Background()
5546

56-
/* thread safety for the manager */
57-
m.mutex.Lock()
58-
defer m.mutex.Unlock()
59-
60-
/* find the session in session map */
61-
session, ok := m.sessionsMap[username]
62-
if !ok {
63-
return fmt.Errorf("username not found in session")
64-
}
65-
6647
/* thread safety for the session */
6748
session.Mutex.Lock()
6849
defer session.Mutex.Unlock()
@@ -83,20 +64,10 @@ func (m *Manager) updateSessionExpiryRedis(username string) error {
8364
}
8465

8566
/* update status of the session - update and set expired operations will be done with this */
86-
func (m *Manager) updateSessionStatusRedis(username string, status Status) error {
67+
func (m *Manager) updateSessionStatusRedis(session *Session, status Status) error {
8768

8869
ctx := context.Background()
8970

90-
/* thread safety for the manager */
91-
m.mutex.Lock()
92-
defer m.mutex.Unlock()
93-
94-
/* find the session in session map */
95-
session, ok := m.sessionsMap[username]
96-
if !ok {
97-
return fmt.Errorf("username not found in session")
98-
}
99-
10071
/* thread safety for the session */
10172
session.Mutex.Lock()
10273
defer session.Mutex.Unlock()
@@ -114,20 +85,10 @@ func (m *Manager) updateSessionStatusRedis(username string, status Status) error
11485
}
11586

11687
/* save transaction results to redis */
117-
func (m *Manager) saveTransactionResultsRedis(username string, txResult types.Transaction) error {
88+
func (m *Manager) saveTransactionResultsRedis(session *Session, txResult types.Transaction) error {
11889

11990
ctx := context.Background()
12091

121-
/* thread safety for the manager */
122-
m.mutex.Lock()
123-
defer m.mutex.Unlock()
124-
125-
/* find the session in session map */
126-
session, ok := m.sessionsMap[username]
127-
if !ok {
128-
return fmt.Errorf("username not found in session")
129-
}
130-
13192
/* thread safety for the session */
13293
session.Mutex.Lock()
13394
defer session.Mutex.Unlock()
@@ -148,19 +109,9 @@ func (m *Manager) saveTransactionResultsRedis(username string, txResult types.Tr
148109
return m.redis.RPush(ctx, key, resultBytes).Err()
149110
}
150111

151-
func (m *Manager) getTransactionResultsRedis(username string, limit int) ([]types.Transaction, error) {
112+
func (m *Manager) getTransactionResultsRedis(session *Session, limit int) ([]types.Transaction, error) {
152113
ctx := context.Background()
153114

154-
/* thread safety for the manager */
155-
m.mutex.Lock()
156-
defer m.mutex.Unlock()
157-
158-
/* find the session in session map */
159-
session, ok := m.sessionsMap[username]
160-
if !ok {
161-
return nil, fmt.Errorf("username not found in session")
162-
}
163-
164115
/* thread safety for the session */
165116
session.Mutex.Lock()
166117
defer session.Mutex.Unlock()

0 commit comments

Comments
 (0)