-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
79 lines (70 loc) · 1.55 KB
/
session.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
// Session storage
package grouter
import (
"sync"
"time"
)
type UssdSession interface {
// Unique ID of this session
ID() string
Set(key string, value any)
Get(key string) (any, bool)
MustGet(key string) any
Del(key string)
CreatedAt() time.Time
}
type Storage interface {
Set(key string, session UssdSession)
Get(key string) UssdSession
Del(key string)
// Vacuum removes sessions that are as old as the given duration
Vacuum(duration time.Duration)
}
type inMemoryStore struct {
vacuumch chan bool
ticker *time.Ticker
//store cmap.ConcurrentMap
store sync.Map
}
func (mss *inMemoryStore) Set(key string, sess UssdSession) {
mss.store.Store(key, sess)
}
func (mss *inMemoryStore) Get(key string) UssdSession {
if sess, ok := mss.store.Load(key); ok && sess != nil {
return sess.(UssdSession)
}
return nil
}
func (mss *inMemoryStore) Del(key string) {
mss.store.Delete(key)
}
func (mss *inMemoryStore) Vacuum(duration time.Duration) {
var now = time.Now()
mss.store.Range(func(key, value any) bool {
session := value.(UssdSession)
lifespan := now.Sub(session.CreatedAt())
if lifespan >= duration {
mss.store.Delete(key)
}
return true
})
}
func NewInMemorySessionStorage(frequency, sessionTTL time.Duration) Storage {
s := &inMemoryStore{
store: sync.Map{},
vacuumch: make(chan bool),
ticker: time.NewTicker(frequency),
}
go func(store *inMemoryStore) {
for {
select {
case <-store.vacuumch:
store.ticker.Stop()
return
case <-store.ticker.C:
store.Vacuum(sessionTTL)
}
}
}(s)
return s
}