@@ -13,15 +13,15 @@ import (
1313)
1414
1515/* for creating a session for user - used by HTTP HANDLERS */
16- func (m * Manager ) CreateSession (username , ipAddress , userAgent string ) error {
16+ func (m * Manager ) CreateSession (username , ipAddress , userAgent string ) (uuid. UUID , error ) {
1717
1818 /* lock the ActiveSessions mutex till the function ends */
1919 m .mutex .Lock ()
2020 defer m .mutex .Unlock ()
2121
2222 /* check if session exists */
2323 if _ , exists := m .sessionsMap [username ]; exists {
24- return fmt .Errorf ("user already exists in active sessions" )
24+ return uuid . Nil , fmt .Errorf ("user already exists in active sessions" )
2525 }
2626
2727 /* Generate session metadata */
@@ -39,7 +39,12 @@ func (m *Manager) CreateSession(username, ipAddress, userAgent string) error {
3939 CreatedAt : now ,
4040 LastActiveAt : now ,
4141 Timer : time .AfterFunc (time .Duration (config .BackendConfig .AppInfo .SessionTimeout )* time .Hour ,
42- func () { m .ExpireSession (username ) },
42+ func () {
43+ err := m .ExpireSession (username )
44+ if err != nil {
45+ m .errCh <- err
46+ }
47+ },
4348 ),
4449 CompletedCount : 0 ,
4550 FailedCount : 0 ,
@@ -56,19 +61,19 @@ func (m *Manager) CreateSession(username, ipAddress, userAgent string) error {
5661 /* store session to Redis */
5762 m .saveSessionRedis (session )
5863
59- return nil
64+ return sessionID , nil
6065}
6166
6267/* for expiring a session */
63- func (m * Manager ) ExpireSession (username string ) {
68+ func (m * Manager ) ExpireSession (username string ) error {
6469 /* thread safety for the manager */
6570 m .mutex .Lock ()
6671 defer m .mutex .Unlock ()
6772
6873 /* check if user exists in active sessions */
6974 session , ok := m .sessionsMap [username ]
7075 if ! ok {
71- return
76+ return fmt . Errorf ( "active user session not found" )
7277 }
7378
7479 session .Mutex .Lock ()
@@ -86,18 +91,16 @@ func (m *Manager) ExpireSession(username string) {
8691 if session .TransactionQueue .Len () != 0 {
8792 /* transactions are pending, mark them pending */
8893 for node := session .TransactionQueue .Front (); node != nil ; node = node .Next () {
89- /* work on transaction structure for *list.List() */
9094 txResult , ok := node .Value .(* types.Transaction )
9195 if ! ok {
9296 continue
9397 }
9498 txResult .Status = types .StatusPending
9599
100+ /* convert transactions into PostgreSQL compatible parameters */
96101 txnPQ , err := ConvertTransactiontoStoreParams (* txResult )
97102 if err != nil {
98- /* error is conversion, continue the loop in good faith */
99- /* need to handle these errors later */
100- fmt .Printf ("Failed to convert transaction to archive format: %v\n " , err )
103+ m .errCh <- fmt .Errorf ("failed to convert transaction to archive format: %w" , err )
101104 continue
102105 }
103106
@@ -113,7 +116,7 @@ func (m *Manager) ExpireSession(username string) {
113116 break
114117 }
115118 if storeErr != nil {
116- fmt .Printf ( "Failed to archive transaction %s after retries: %v \n " , txResult .ID , storeErr )
119+ m . errCh <- fmt .Errorf ( "failed to archive transaction %s after retries: %w " , txResult .ID , storeErr )
117120 continue
118121 }
119122 }
@@ -130,52 +133,40 @@ func (m *Manager) ExpireSession(username string) {
130133 m .sessionOrder .Remove (session .listElem )
131134 }
132135
133- /* for debugging */
134- fmt .Printf ("Archiving session ID=%s with status=%q\n " , session .ID , session .Status )
135-
136136 /* convert all session parameters to PostgreSQL compatible parameters */
137137 archive , err := ConvertSessionToStoreParams (session )
138- if err != nil {
139- /* session conversion failed, leave it in good faith */
140- /* handle err later */
141- fmt .Printf ("Failed to convert session to archive format: %v\n " , err )
142- return
143- }
144-
145- /* debug print the archive parameters */
146- fmt .Printf ("Archive parameters - ID: %s, Status: %q, Username: %s\n " ,
147- archive .ID , archive .Status , archive .Username )
148-
149- /* store session to the archive with retries */
150- var storeErr error
151- for retries := 0 ; retries < 3 ; retries ++ {
152- if _ , err := m .archivalPQ .StoreSessionPQ (context .Background (), * archive ); err != nil {
153- storeErr = err
154- time .Sleep (time .Second * time .Duration (retries + 1 ))
155- continue
138+ if err == nil {
139+ /* store session to the archive with retries */
140+ var storeErr error
141+ for retries := 0 ; retries < 3 ; retries ++ {
142+ if _ , err := m .archivalPQ .StoreSessionPQ (context .Background (), * archive ); err != nil {
143+ storeErr = err
144+ time .Sleep (time .Second * time .Duration (retries + 1 ))
145+ continue
146+ }
147+ storeErr = nil
148+ break
156149 }
157- storeErr = nil
158- break
159- }
160- if storeErr != nil {
161- fmt . Printf ( "Failed to archive session after retries: %v \n " , storeErr )
162- return
150+ if storeErr ! = nil {
151+ m . errCh <- fmt . Errorf ( "failed to archive session after retries: %w" , storeErr )
152+ }
153+ } else {
154+ /* handle err */
155+ m . errCh <- fmt . Errorf ( "failed to convert session to archive format: %w" , err )
163156 }
164157
165158 /* delete both session and transaction results from Redis */
166159 sessionKey := fmt .Sprintf ("session:%s" , session .ID )
167160 txResultsKey := fmt .Sprintf ("session:%s:txresults" , session .ID )
168161 result := m .redis .Del (context .Background (), sessionKey , txResultsKey )
169162 if result .Err () != nil {
170- fmt .Printf ("Failed to delete session from Redis: %v\n " , result .Err ())
171- } else {
172- // Log the number of keys deleted
173- deleted , _ := result .Result ()
174- fmt .Printf ("Successfully deleted %d keys from Redis for session %s\n " , deleted , session .ID )
163+ m .errCh <- fmt .Errorf ("Failed to delete session from Redis: %w" , result .Err ())
175164 }
176165
177166 /* remove session from sessionsMap */
178167 delete (m .sessionsMap , username )
168+
169+ return nil
179170}
180171
181172/* add transaction to a session - assumes caller holds necessary locks */
0 commit comments