@@ -61,15 +61,16 @@ func NewStoreWithDB(db *sql.DB, tableName string, gcInterval int) *Store {
61
61
db : & gorp.DbMap {Db : db , Dialect : gorp.MySQLDialect {Encoding : "UTF8" , Engine : "MyISAM" }},
62
62
tableName : "oauth2_token" ,
63
63
stdout : os .Stderr ,
64
- interval : 600 ,
65
64
}
66
65
if tableName != "" {
67
66
store .tableName = tableName
68
67
}
69
68
69
+ interval := 600
70
70
if gcInterval > 0 {
71
- store . interval = gcInterval
71
+ interval = gcInterval
72
72
}
73
+ store .ticker = time .NewTicker (time .Second * time .Duration (interval ))
73
74
74
75
table := store .db .AddTableWithName (StoreItem {}, store .tableName )
75
76
table .AddIndex ("idx_code" , "Btree" , []string {"code" })
@@ -89,10 +90,10 @@ func NewStoreWithDB(db *sql.DB, tableName string, gcInterval int) *Store {
89
90
90
91
// Store mysql token store
91
92
type Store struct {
92
- interval int
93
93
tableName string
94
94
db * gorp.DbMap
95
95
stdout io.Writer
96
+ ticker * time.Ticker
96
97
}
97
98
98
99
// SetStdout set error output
@@ -103,6 +104,7 @@ func (s *Store) SetStdout(stdout io.Writer) *Store {
103
104
104
105
// Close close the store
105
106
func (s * Store ) Close () {
107
+ s .ticker .Stop ()
106
108
s .db .Db .Close ()
107
109
}
108
110
@@ -114,8 +116,7 @@ func (s *Store) errorf(format string, args ...interface{}) {
114
116
}
115
117
116
118
func (s * Store ) gc () {
117
- ticker := time .NewTicker (time .Second * time .Duration (s .interval ))
118
- for range ticker .C {
119
+ for range s .ticker .C {
119
120
now := time .Now ().Unix ()
120
121
query := fmt .Sprintf ("SELECT COUNT(*) FROM %s WHERE expired_at<=?" , s .tableName )
121
122
n , err := s .db .SelectInt (query , now )
0 commit comments