Skip to content

Commit

Permalink
Fixed gc
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricTian committed Jan 9, 2019
1 parent 2a177c4 commit 7332bfd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ func main() {
manager := manage.NewDefaultManager()

// use mysql token store
store := mysql.NewStore(
store := mysql.NewDefaultStore(
mysql.NewConfig("root:123456@tcp(127.0.0.1:3306)/myapp_test?charset=utf8"),
"",
0,
)

defer store.Close()
Expand Down
43 changes: 27 additions & 16 deletions mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ type Config struct {
MaxIdleConns int
}

// NewDefaultStore create mysql store instance
func NewDefaultStore(config *Config) *Store {
return NewStore(config, "", 0)
}

// NewStore create mysql store instance,
// config mysql configuration,
// tableName table name (default oauth2_token),
Expand Down Expand Up @@ -114,27 +119,33 @@ func (s *Store) Close() {
s.db.Db.Close()
}

func (s *Store) errorf(format string, args ...interface{}) {
if s.stdout != nil {
buf := fmt.Sprintf(format, args...)
s.stdout.Write([]byte(buf))
func (s *Store) gc() {
for range s.ticker.C {
s.clean()
}
}

func (s *Store) gc() {
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)
func (s *Store) clean() {
now := time.Now().Unix()
query := fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE expired_at<=? OR (code='' AND access='' AND refresh='')", s.tableName)
n, err := s.db.SelectInt(query, now)
if err != nil || n == 0 {
if err != nil {
s.errorf("[ERROR]:%s", err.Error())
return
} else if n > 0 {
_, err = s.db.Exec(fmt.Sprintf("DELETE FROM %s WHERE expired_at<=?", s.tableName), now)
if err != nil {
s.errorf("[ERROR]:%s", err.Error())
}
s.errorf(err.Error())
}
return
}

_, err = s.db.Exec(fmt.Sprintf("DELETE FROM %s WHERE expired_at<=? OR (code='' AND access='' AND refresh='')", s.tableName), now)
if err != nil {
s.errorf(err.Error())
}
}

func (s *Store) errorf(format string, args ...interface{}) {
if s.stdout != nil {
buf := fmt.Sprintf("[OAUTH2-MYSQL-ERROR]: "+format, args...)
s.stdout.Write([]byte(buf))
}
}

Expand Down
9 changes: 3 additions & 6 deletions mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@ import (

"gopkg.in/oauth2.v3/models"

. "github.com/smartystreets/goconvey/convey"

_ "github.com/go-sql-driver/mysql"
. "github.com/smartystreets/goconvey/convey"
)

const (
dsn = "root:@tcp(127.0.0.1:3306)/myapp_test?charset=utf8"
)

func TestTokenStore(t *testing.T) {
// wait gc
defer time.Sleep(time.Second * 2)

Convey("Test mysql token store", t, func() {
store := NewStore(NewConfig(dsn), "", 1)
store := NewDefaultStore(NewConfig(dsn))
defer store.clean()

Convey("Test authorization code store", func() {
info := &models.Token{
Expand Down

0 comments on commit 7332bfd

Please sign in to comment.