Skip to content

Commit

Permalink
Optimize GC
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricTian committed Sep 15, 2018
1 parent 5dc62a6 commit b9867a4
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,16 @@ func NewStoreWithDB(db *sql.DB, tableName string, gcInterval int) *Store {
db: &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{Encoding: "UTF8", Engine: "MyISAM"}},
tableName: "oauth2_token",
stdout: os.Stderr,
interval: 600,
}
if tableName != "" {
store.tableName = tableName
}

interval := 600
if gcInterval > 0 {
store.interval = gcInterval
interval = gcInterval
}
store.ticker = time.NewTicker(time.Second * time.Duration(interval))

table := store.db.AddTableWithName(StoreItem{}, store.tableName)
table.AddIndex("idx_code", "Btree", []string{"code"})
Expand All @@ -89,10 +90,10 @@ func NewStoreWithDB(db *sql.DB, tableName string, gcInterval int) *Store {

// Store mysql token store
type Store struct {
interval int
tableName string
db *gorp.DbMap
stdout io.Writer
ticker *time.Ticker
}

// SetStdout set error output
Expand All @@ -103,6 +104,7 @@ func (s *Store) SetStdout(stdout io.Writer) *Store {

// Close close the store
func (s *Store) Close() {
s.ticker.Stop()
s.db.Db.Close()
}

Expand All @@ -114,8 +116,7 @@ func (s *Store) errorf(format string, args ...interface{}) {
}

func (s *Store) gc() {
ticker := time.NewTicker(time.Second * time.Duration(s.interval))
for range ticker.C {
for range s.ticker.C {
now := time.Now().Unix()
query := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE expired_at<=?", s.tableName)
n, err := s.db.SelectInt(query, now)
Expand Down

0 comments on commit b9867a4

Please sign in to comment.