-
Notifications
You must be signed in to change notification settings - Fork 2
/
session_file.go
174 lines (143 loc) · 3.71 KB
/
session_file.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
package cypress
import (
"encoding/gob"
"errors"
"os"
"path"
"time"
"go.uber.org/zap"
)
var (
// ErrDirectoryRequired a directory is expected but file is seen
ErrDirectoryRequired = errors.New("a directory is required")
// ErrFileRequired a file is expected but directory is seen
ErrFileRequired = errors.New("a file is required")
// ErrBadSessionFile failed to read session data from session file
ErrBadSessionFile = errors.New("bad session file")
)
type fileSessionStore struct {
path string
gcTicker *time.Ticker
exitChan chan bool
}
type fileSessionItem struct {
Data []byte
Expiration time.Time
}
// NewFileSessionStore creates a file session store
func NewFileSessionStore(directory string) (SessionStore, error) {
fileInfo, err := os.Stat(directory)
if err != nil {
return nil, err
}
if !fileInfo.IsDir() {
return nil, ErrDirectoryRequired
}
store := &fileSessionStore{
path: directory,
gcTicker: time.NewTicker(time.Minute * 5),
exitChan: make(chan bool),
}
gob.Register(fileSessionItem{})
go func() {
for {
select {
case <-store.gcTicker.C:
store.doGC()
case <-store.exitChan:
return
}
}
}()
return store, nil
}
// Close close the session store
func (store *fileSessionStore) Close() {
store.exitChan <- true
store.gcTicker.Stop()
close(store.exitChan)
}
// Save saves the session to the file system
func (store *fileSessionStore) Save(session *Session, timeout time.Duration) error {
filePath := path.Join(store.path, session.ID)
if !session.IsValid {
// remove the session as it's set to invalid
err := os.Remove(filePath)
if err != nil {
zap.L().Error("failed to remove session file", zap.Error(err))
}
return nil
}
// this acquires session's read lock, must be put before session.lock.Lock()
sessionData := session.Serialize()
// we need to lock the session to avoid concurrent writes
session.lock.Lock()
defer session.lock.Unlock()
// try to remove the old session file if the file exists
os.Remove(filePath)
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
sessionItem := &fileSessionItem{
Data: sessionData,
Expiration: time.Now().Add(timeout),
}
encoder := gob.NewEncoder(file)
err = encoder.Encode(sessionItem)
return err
}
// Get gets a session with the given id from the file store
func (store *fileSessionStore) Get(id string) (*Session, error) {
sessionItem, err := store.readSession(id)
if err != nil {
return nil, err
}
if sessionItem == nil {
return nil, ErrSessionNotFound
}
if sessionItem.Expiration.Before(time.Now()) {
return nil, ErrSessionNotFound
}
session := NewSession(id)
session.Deserialize(sessionItem.Data)
return session, nil
}
func (store *fileSessionStore) readSession(id string) (*fileSessionItem, error) {
filePath := path.Join(store.path, id)
fileInfo, err := os.Stat(filePath)
if err != nil {
return nil, ErrSessionNotFound
}
if fileInfo.IsDir() {
return nil, ErrFileRequired
}
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
decoder := gob.NewDecoder(file)
sessionItem := fileSessionItem{}
err = decoder.Decode(&sessionItem)
if err != nil {
return nil, err
}
return &sessionItem, nil
}
func (store *fileSessionStore) doGC() {
files, err := os.ReadDir(store.path)
if err != nil {
zap.L().Error("failed to run GC on file session store", zap.Error(err))
return
}
now := time.Now()
for _, file := range files {
item, _ := store.readSession(file.Name())
if item != nil && item.Expiration.Before(now) {
os.Remove(path.Join(store.path, file.Name()))
zap.L().Debug("session file expired, clean up by GC", zap.Error(err))
}
}
}