-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemorystore.go
164 lines (150 loc) · 3.32 KB
/
memorystore.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
package sessions
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"io"
"sync"
"time"
"github.com/go-http-utils/cookie"
)
// NewMemoryStore returns an MemoryStore instance
func NewMemoryStore(options ...*Options) (store *MemoryStore) {
opts := &cookie.Options{
Path: "/",
HTTPOnly: true,
Signed: false, // not necessary
MaxAge: 24 * 60 * 60,
}
if len(options) > 0 && options[0] != nil {
temp := options[0]
opts.Path = temp.Path
opts.Domain = temp.Domain
opts.MaxAge = temp.MaxAge
opts.Secure = temp.Secure
opts.HTTPOnly = temp.HTTPOnly
}
store = &MemoryStore{
opts: opts,
ticker: time.NewTicker(time.Second),
store: make(map[string]*sessionValue),
done: make(chan bool, 1),
}
go store.cleanCache()
return
}
type sessionValue struct {
expired time.Time
session string
}
// MemoryStore using memory to store sessions base on secure cookies.
type MemoryStore struct {
opts *cookie.Options
store map[string]*sessionValue
ticker *time.Ticker
lock sync.Mutex
done chan bool
}
// Load a session by name and any kind of stores
func (m *MemoryStore) Load(name string, session Sessions, cookie *cookie.Cookies) error {
sid, err := cookie.Get(name, m.opts.Signed)
var result string
if sid != "" {
m.lock.Lock()
if val, ok := m.store[sid]; ok {
result = val.session
}
m.lock.Unlock()
}
if result != "" {
err = Decode(result, &session)
}
session.Init(name, sid, cookie, m, result)
return err
}
// Save session to Response's cookie
func (m *MemoryStore) Save(session Sessions) (err error) {
val, err := Encode(session)
if err != nil || !session.IsChanged(val) {
return
}
sid := session.GetSID()
if sid == "" {
sid = NewSID(val)
}
m.lock.Lock()
defer m.lock.Unlock()
m.store[sid] = &sessionValue{
session: val,
expired: time.Now().Add(time.Duration(m.opts.MaxAge) * time.Second),
}
session.GetCookie().Set(session.GetName(), sid, m.opts)
return
}
// Destroy destroy the session
func (m *MemoryStore) Destroy(session Sessions) (err error) {
sid := session.GetSID()
if sid != "" {
m.lock.Lock()
defer m.lock.Unlock()
delete(m.store, sid)
}
session.GetCookie().Remove(session.GetName(), m.opts)
return
}
// Len ...
func (m *MemoryStore) Len() int {
m.lock.Lock()
defer m.lock.Unlock()
return len(m.store)
}
// Close goroutine cleanCache thread
func (m *MemoryStore) Close() {
close(m.done)
}
func (m *MemoryStore) cleanCache() {
defer m.ticker.Stop()
for {
select {
case <-m.ticker.C:
m.clean()
case <-m.done:
return
}
}
}
func (m *MemoryStore) clean() {
m.lock.Lock()
defer m.lock.Unlock()
start := time.Now()
expireTime := start.Add(time.Millisecond * 100)
frequency := 24
var expired int
for {
label:
for i := 0; i < frequency; i++ {
for key, value := range m.store {
if value.expired.Before(start) {
delete(m.store, key)
expired++
}
break
}
}
if expireTime.Before(time.Now()) {
return
}
if expired > frequency/4 {
expired = 0
goto label
}
return
}
}
// NewSID generates a random SID
func NewSID(val string) string {
h := sha256.New()
h.Write([]byte(val))
io.CopyN(h, rand.Reader, 8)
return hex.EncodeToString(h.Sum(nil))
}