Skip to content

Commit a2db9d1

Browse files
Updated interact.go for sessions based on cache and archive
1 parent 410dfb0 commit a2db9d1

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

internal/session/interact.go

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package session
22

33
import (
44
"container/list"
5+
"context"
56
"fmt"
67
"time"
78

89
"github.com/google/uuid"
910

1011
"github.com/PythonHacker24/linux-acl-management-backend/config"
12+
"github.com/PythonHacker24/linux-acl-management-backend/internal/types"
1113
)
1214

1315
/* for creating a session for user - used by HTTP HANDLERS */
@@ -69,18 +71,44 @@ func (m *Manager) ExpireSession(username string) {
6971
return
7072
}
7173

72-
/* mark the session as expired */
73-
session.Status = StatusExpired
74+
/*
75+
delete the session from Redis
76+
check if any transactions are remaining in the queue
77+
if yes, label transactions and sessions pending
78+
if no, label session expired
79+
push session and transactions to archive
80+
*/
81+
82+
/* check if transactions are remaining in the session queue */
83+
if session.TransactionQueue.Len() != 0 {
84+
/* transactions are pending, mark them pending */
85+
for node := session.TransactionQueue.Front(); node != nil; node = node.Next() {
86+
/* work on transaction structure for *list.List() */
87+
txResult, ok := node.Value.(types.Transaction)
88+
if !ok {
89+
continue
90+
}
91+
txResult.Status = string(StatusPending)
92+
/* TODO: Push this all into PostgreSQL */
93+
}
94+
} else {
95+
/* no empty transactions; mark the session as expired */
96+
session.Status = StatusExpired
97+
}
7498

7599
/* remove session from sessionOrder Linked List */
76100
if session.listElem != nil {
77101
m.sessionOrder.Remove(session.listElem)
78102
}
103+
104+
/* convert all session parameters to PostgreSQL compatible parameters */
105+
archive, err := ConvertSessionToStoreParams(session)
106+
if err != nil {
107+
return
108+
}
79109

80-
/* set session status of Redis to Expired */
81-
m.updateSessionStatusRedis(username, StatusExpired)
82-
83-
/* store session to archive pending */
110+
/* store session to the archive */
111+
m.archivalPQ.StoreSessionPQ(context.Background(), *archive)
84112

85113
/* remove session from sessionsMap */
86114
delete(m.sessionsMap, username)
@@ -95,7 +123,7 @@ func (m *Manager) AddTransaction(username string, txn interface{}) error {
95123
/* get the session from sessions map with O(1) runtime */
96124
session, exists := m.sessionsMap[username]
97125
if !exists {
98-
return fmt.Errorf("Session not found")
126+
return fmt.Errorf("session not found")
99127
}
100128

101129
/* thread safety for the session */
@@ -105,8 +133,6 @@ func (m *Manager) AddTransaction(username string, txn interface{}) error {
105133
/* push transaction into the queue from back */
106134
session.TransactionQueue.PushBack(txn)
107135

108-
/* log adding of the transaction */
109-
110136
return nil
111137
}
112138

@@ -119,7 +145,7 @@ func (m *Manager) refreshTimer(username string) error {
119145
/* get session from sessionMap */
120146
session, exists := m.sessionsMap[username]
121147
if !exists {
122-
return fmt.Errorf("Session not found")
148+
return fmt.Errorf("session not found")
123149
}
124150

125151
/* thread safety for the session */
@@ -145,7 +171,7 @@ func (m *Manager) refreshTimer(username string) error {
145171
return nil
146172
}
147173

148-
/* TODO: toDashoardView must be completed changes to fetch data from Redis only */
174+
/* TODO: toDashoardView must be changed to fetch data from Redis only */
149175

150176
/* convert session information into frontend safe structure */
151177
func (m *Manager) toDashboardView(username string) (SessionView, error) {
@@ -156,7 +182,7 @@ func (m *Manager) toDashboardView(username string) (SessionView, error) {
156182
/* get session from sessionMap */
157183
session, exists := m.sessionsMap[username]
158184
if !exists {
159-
return SessionView{}, fmt.Errorf("Session not found")
185+
return SessionView{}, fmt.Errorf("session not found")
160186
}
161187

162188
/* thread safety for the session */

0 commit comments

Comments
 (0)