mirror of
https://github.com/alexedwards/scs.git
synced 2025-07-13 01:00:17 +02:00
Merge pull request #230 from earthboundkid/cj-race-fix
sqlite3store: Fix race in StopCleanup
This commit is contained in:
@ -25,6 +25,7 @@ func New(db *sql.DB) *SQLite3Store {
|
|||||||
func NewWithCleanupInterval(db *sql.DB, cleanupInterval time.Duration) *SQLite3Store {
|
func NewWithCleanupInterval(db *sql.DB, cleanupInterval time.Duration) *SQLite3Store {
|
||||||
p := &SQLite3Store{db: db}
|
p := &SQLite3Store{db: db}
|
||||||
if cleanupInterval > 0 {
|
if cleanupInterval > 0 {
|
||||||
|
p.stopCleanup = make(chan bool)
|
||||||
go p.startCleanup(cleanupInterval)
|
go p.startCleanup(cleanupInterval)
|
||||||
}
|
}
|
||||||
return p
|
return p
|
||||||
@ -96,7 +97,6 @@ func (p *SQLite3Store) All() (map[string][]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *SQLite3Store) startCleanup(interval time.Duration) {
|
func (p *SQLite3Store) startCleanup(interval time.Duration) {
|
||||||
p.stopCleanup = make(chan bool)
|
|
||||||
ticker := time.NewTicker(interval)
|
ticker := time.NewTicker(interval)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
@ -269,7 +269,6 @@ func TestCleanup(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
p := NewWithCleanupInterval(db, 200*time.Millisecond)
|
p := NewWithCleanupInterval(db, 200*time.Millisecond)
|
||||||
defer p.StopCleanup()
|
|
||||||
|
|
||||||
err = p.Commit("session_token", []byte("encoded_data"), time.Now().Add(100*time.Millisecond))
|
err = p.Commit("session_token", []byte("encoded_data"), time.Now().Add(100*time.Millisecond))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -287,6 +286,8 @@ func TestCleanup(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(300 * time.Millisecond)
|
time.Sleep(300 * time.Millisecond)
|
||||||
|
p.StopCleanup()
|
||||||
|
|
||||||
row = db.QueryRow("SELECT COUNT(*) FROM sessions WHERE token = 'session_token'")
|
row = db.QueryRow("SELECT COUNT(*) FROM sessions WHERE token = 'session_token'")
|
||||||
err = row.Scan(&count)
|
err = row.Scan(&count)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Reference in New Issue
Block a user