-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgorm.go
360 lines (307 loc) · 7.91 KB
/
gorm.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package gorm
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"sync"
"time"
"github.com/go-session/session"
"github.com/jinzhu/gorm"
)
var (
_ session.ManagerStore = &managerStore{}
_ session.Store = &store{}
jsonMarshal = json.Marshal
jsonUnmarshal = json.Unmarshal
)
// SessionItem Data items stored in mysql
type SessionItem struct {
ID string `gorm:"column:id;size:255;primary_key;"`
Value string `gorm:"column:value;size:2048;"`
CreatedAt time.Time `gorm:"column:created_at;"`
ExpiredAt time.Time `gorm:"column:expired_at;"`
}
// Config configuration parameter
type Config struct {
Debug bool // start debug mode
ConnMaxLifetime time.Duration // sets the maximum amount of time a connection may be reused
MaxOpenConns int // sets the maximum number of open connections to the database
MaxIdleConns int // sets the maximum number of connections in the idle connection pool
TableName string // Specify the stored table name (default session)
GCInterval int // Time interval for executing GC (in seconds, default 600)
}
// MustStore Create an instance of a gorm store(Throw a panic if an error occurs)
func MustStore(cfg Config, dialect string, args ...interface{}) session.ManagerStore {
store, err := NewStore(cfg, dialect, args...)
if err != nil {
panic(err)
}
return store
}
// NewStore Create an instance of a gorm store
func NewStore(cfg Config, dialect string, args ...interface{}) (session.ManagerStore, error) {
db, err := gorm.Open(dialect, args...)
if err != nil {
return nil, err
}
if cfg.Debug {
db = db.Debug()
}
err = db.DB().Ping()
if err != nil {
return nil, err
}
db.DB().SetMaxIdleConns(cfg.MaxIdleConns)
db.DB().SetMaxOpenConns(cfg.MaxOpenConns)
db.DB().SetConnMaxLifetime(cfg.ConnMaxLifetime)
return NewStoreWithDB(db, cfg.TableName, cfg.GCInterval)
}
// MustStoreWithDB Create an instance of a gorm store(Throw a panic if an error occurs)
func MustStoreWithDB(db *gorm.DB, tableName string, gcInterval int) session.ManagerStore {
store, err := NewStoreWithDB(db, tableName, gcInterval)
if err != nil {
panic(err)
}
return store
}
// NewStoreWithDB Create an instance of a gorm store,
// tableName Specify the stored table name (default session),
// gcInterval Time interval for executing GC (in seconds, default 600)
func NewStoreWithDB(db *gorm.DB, tableName string, gcInterval int) (session.ManagerStore, error) {
store := &managerStore{
tableName: "session",
stdout: os.Stderr,
}
if tableName != "" {
store.tableName = tableName
}
store.db = db.Table(store.tableName)
if !db.HasTable(store.tableName) {
err := store.db.CreateTable(&SessionItem{}).Error
if err != nil {
return nil, err
}
store.db.AddIndex("idx_expired_at", "expired_at")
}
interval := 600
if gcInterval > 0 {
interval = gcInterval
}
store.ticker = time.NewTicker(time.Second * time.Duration(interval))
go store.gc()
return store, nil
}
type managerStore struct {
ticker *time.Ticker
wg sync.WaitGroup
db *gorm.DB
tableName string
stdout io.Writer
}
func (s *managerStore) gc() {
for range s.ticker.C {
s.clean()
}
}
func (s *managerStore) clean() {
s.wg.Add(1)
defer s.wg.Done()
db := s.db.Where("expired_at<=?", time.Now())
var count int
err := db.Count(&count).Error
if err != nil || count == 0 {
if err != nil {
s.errorf(err.Error())
}
return
}
err = db.Delete(nil).Error
if err != nil {
s.errorf(err.Error())
}
}
func (s *managerStore) errorf(format string, args ...interface{}) {
if s.stdout != nil {
buf := fmt.Sprintf("[GORM-SESSION-ERROR]: "+format, args...)
s.stdout.Write([]byte(buf))
}
}
func (s *managerStore) getValue(sid string) (string, error) {
var item SessionItem
err := s.db.Where("id=?", sid).First(&item).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return "", nil
}
} else if item.ExpiredAt.Before(time.Now()) {
return "", nil
}
return item.Value, nil
}
func (s *managerStore) parseValue(value string) (map[string]interface{}, error) {
var values map[string]interface{}
if len(value) > 0 {
err := jsonUnmarshal([]byte(value), &values)
if err != nil {
return nil, err
}
}
return values, nil
}
func (s *managerStore) GetExpired(expired int64) time.Time {
return time.Now().Add(time.Duration(expired) * time.Second)
}
func (s *managerStore) Check(_ context.Context, sid string) (bool, error) {
var count int
result := s.db.Where("id=?", sid).Count(&count)
if err := result.Error; err != nil {
return false, err
}
return count > 0, nil
}
func (s *managerStore) Create(ctx context.Context, sid string, expired int64) (session.Store, error) {
return newStore(ctx, s, sid, expired, nil), nil
}
func (s *managerStore) Update(ctx context.Context, sid string, expired int64) (session.Store, error) {
value, err := s.getValue(sid)
if err != nil {
return nil, err
} else if value == "" {
return newStore(ctx, s, sid, expired, nil), nil
}
result := s.db.Where("id=?", sid).Update("expired_at", s.GetExpired(expired))
if err := result.Error; err != nil {
return nil, err
}
values, err := s.parseValue(value)
if err != nil {
return nil, err
}
return newStore(ctx, s, sid, expired, values), nil
}
func (s *managerStore) Delete(_ context.Context, sid string) error {
result := s.db.Where("id=?", sid).Delete(nil)
return result.Error
}
func (s *managerStore) Refresh(ctx context.Context, oldsid, sid string, expired int64) (session.Store, error) {
value, err := s.getValue(oldsid)
if err != nil {
return nil, err
} else if value == "" {
return newStore(ctx, s, sid, expired, nil), nil
}
item := &SessionItem{
ID: sid,
Value: value,
CreatedAt: time.Now(),
ExpiredAt: s.GetExpired(expired),
}
result := s.db.Create(item)
if err := result.Error; err != nil {
return nil, err
}
err = s.Delete(nil, oldsid)
if err != nil {
return nil, err
}
values, err := s.parseValue(value)
if err != nil {
return nil, err
}
return newStore(ctx, s, sid, expired, values), nil
}
func (s *managerStore) Close() error {
s.ticker.Stop()
s.wg.Wait()
s.db.Close()
return nil
}
func newStore(ctx context.Context, s *managerStore, sid string, expired int64, values map[string]interface{}) *store {
if values == nil {
values = make(map[string]interface{})
}
return &store{
ctx: ctx,
mstore: s,
sid: sid,
expired: expired,
values: values,
}
}
type store struct {
sync.RWMutex
ctx context.Context
mstore *managerStore
sid string
expired int64
values map[string]interface{}
}
func (s *store) Context() context.Context {
return s.ctx
}
func (s *store) SessionID() string {
return s.sid
}
func (s *store) Set(key string, value interface{}) {
s.Lock()
s.values[key] = value
s.Unlock()
}
func (s *store) Get(key string) (interface{}, bool) {
s.RLock()
val, ok := s.values[key]
s.RUnlock()
return val, ok
}
func (s *store) Delete(key string) interface{} {
s.RLock()
v, ok := s.values[key]
s.RUnlock()
if ok {
s.Lock()
delete(s.values, key)
s.Unlock()
}
return v
}
func (s *store) Flush() error {
s.Lock()
s.values = make(map[string]interface{})
s.Unlock()
return s.Save()
}
func (s *store) Save() error {
var value string
s.RLock()
if len(s.values) > 0 {
buf, err := jsonMarshal(s.values)
if err != nil {
s.RUnlock()
return err
}
value = string(buf)
}
s.RUnlock()
exists, err := s.mstore.Check(nil, s.sid)
if err != nil {
return err
} else if !exists {
item := &SessionItem{
ID: s.sid,
Value: value,
CreatedAt: time.Now(),
ExpiredAt: s.mstore.GetExpired(s.expired),
}
result := s.mstore.db.Create(item)
if err := result.Error; err != nil {
return err
}
}
result := s.mstore.db.Where("id=?", s.sid).Updates(map[string]interface{}{
"value": value,
"expired_at": s.mstore.GetExpired(s.expired),
})
return result.Error
}