forked from ariefdarmawan/kiam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_pool.go
206 lines (168 loc) · 4 KB
/
session_pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package kiam
import (
"errors"
"fmt"
"sync"
"time"
"git.kanosolution.net/kano/appkit"
"github.com/eaciit/toolkit"
"github.com/google/uuid"
)
type SessionPool struct {
mtx *sync.RWMutex
sessions map[string]*Session
refs map[string]string
logger *toolkit.LogEngine
CleanUpDuration time.Duration
CleanUpCheck time.Duration
}
func NewSessionPool(log *toolkit.LogEngine) *SessionPool {
if log == nil {
log = appkit.Log()
}
sp := new(SessionPool)
sp.mtx = new(sync.RWMutex)
sp.logger = log
sp.sessions = map[string]*Session{}
sp.refs = map[string]string{}
if int(sp.CleanUpDuration) == 0 {
sp.CleanUpDuration = 30 * time.Minute
}
if int(sp.CleanUpCheck) == 0 {
sp.CleanUpCheck = 100 * time.Millisecond
}
go func() {
for {
<-time.After(sp.CleanUpCheck)
cleaned := sp.RemoveSessionByDuration(sp.CleanUpDuration)
if cleaned > 0 {
sp.logger.Infof("cleaning up %d sessions(s)", cleaned)
}
}
}()
return sp
}
func (sp *SessionPool) GetIDs() []string {
res := []string{}
if sp.sessions == nil {
sp.sessions = map[string]*Session{}
}
for k := range sp.sessions {
res = append(res, k)
}
return res
}
func (sp *SessionPool) GetBySessionID(id string) (*Session, bool) {
sp.mtx.RLock()
defer sp.mtx.RUnlock()
se, ok := sp.sessions[id]
return se, ok
}
func (sp *SessionPool) GetByReferenceID(id string) (*Session, bool) {
sp.mtx.RLock()
defer sp.mtx.RUnlock()
seid, ok := sp.refs[id]
if seid == "" || !ok {
return nil, false
}
se, ok := sp.sessions[seid]
return se, ok
}
func (sp *SessionPool) Create(referenceID string, data toolkit.M, second int) (*Session, error) {
_, ok := sp.GetByReferenceID(referenceID)
if ok {
return nil, errors.New("Session already exist")
}
if second == 0 {
second = int(sp.CleanUpDuration) / int(time.Second)
}
se := new(Session)
se.SessionID = uuid.New().String()
se.ReferenceID = referenceID
se.Data = data
se.Duration = second
se.LastUpdate = time.Now()
sp.mtx.Lock()
defer sp.mtx.Unlock()
sp.sessions[se.SessionID] = se
sp.refs[referenceID] = se.SessionID
return se, nil
}
func (sp *SessionPool) RegisterSession(sess *Session) error {
if sess.SessionID == "" || sess.ReferenceID == "" {
return fmt.Errorf("sessionID and referenceID are mandatory")
}
sp.mtx.Lock()
defer sp.mtx.Unlock()
sp.sessions[sess.SessionID] = sess
sp.refs[sess.ReferenceID] = sess.SessionID
return nil
}
func (sp *SessionPool) Update(sessionID string, second int) error {
se, ok := sp.GetBySessionID(sessionID)
if !ok {
return errors.New("Session for this ID is not exist")
}
if second != 0 {
se.Duration = second
}
se.LastUpdate = time.Now()
sp.mtx.Lock()
defer sp.mtx.Unlock()
sp.sessions[se.SessionID] = se
return nil
}
func (sp *SessionPool) Renew(sessionID string, second int) (string, error) {
se, ok := sp.GetBySessionID(sessionID)
if !ok {
return "", errors.New("Session for this ID is not exist")
}
if second != 0 {
se.Duration = second
}
newse := *se
newse.LastUpdate = time.Now()
newse.SessionID = uuid.New().String()
sp.mtx.Lock()
defer sp.mtx.Unlock()
// add new session
sp.sessions[newse.SessionID] = &newse
sp.refs[newse.ReferenceID] = newse.SessionID
// remove old one
delete(sp.sessions, sessionID)
return newse.SessionID, nil
}
func (sp *SessionPool) Remove(sessionID string) {
se, ok := sp.GetBySessionID(sessionID)
if !ok {
return
}
sp.mtx.Lock()
defer sp.mtx.Unlock()
delete(sp.sessions, se.SessionID)
delete(sp.refs, se.ReferenceID)
}
func (sp *SessionPool) RemoveSessionByDuration(d time.Duration) int {
ss := []*Session{}
sp.mtx.RLock()
now := time.Now()
for _, sess := range sp.sessions {
if now.After(sess.LastUpdate.Add(time.Duration(sess.Duration) * time.Second)) {
ss = append(ss, sess)
}
}
sp.mtx.RUnlock()
sp.mtx.Lock()
for _, s := range ss {
delete(sp.sessions, s.SessionID)
delete(sp.refs, s.ReferenceID)
s = nil
}
sp.mtx.Unlock()
return len(ss)
}
func (sp *SessionPool) Length() int {
sp.mtx.RLock()
defer sp.mtx.Unlock()
return len(sp.sessions)
}