feature: expired sessions can be deleted via a background goroutine.
- Call defer store.StopCleanup(store.Cleanup(time.Minute * 5)) after store creation. - Does not break the existing API (optional, but recommended) - Based on https://github.com/yosssi/boltstore/reaper - Deletes expired sessions (where expireson > now()) - Includes tests
This commit is contained in:
64
cleanup.go
Normal file
64
cleanup.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package pgstore
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
var defaultInterval = time.Minute * 5
|
||||
|
||||
// Cleanup runs a background goroutine every interval that deletes expired
|
||||
// sessions from the database.
|
||||
//
|
||||
// The design is based on https://github.com/yosssi/boltstore
|
||||
func (db *PGStore) Cleanup(interval time.Duration) (chan<- struct{}, <-chan struct{}) {
|
||||
if interval <= 0 {
|
||||
interval = defaultInterval
|
||||
}
|
||||
|
||||
quit, done := make(chan struct{}), make(chan struct{})
|
||||
go db.cleanup(interval, quit, done)
|
||||
return quit, done
|
||||
}
|
||||
|
||||
// StopCleanup stops the background cleanup from running.
|
||||
func (db *PGStore) StopCleanup(quit chan<- struct{}, done <-chan struct{}) {
|
||||
quit <- struct{}{}
|
||||
<-done
|
||||
}
|
||||
|
||||
// cleanup deletes expired sessions at set intervals.
|
||||
func (db *PGStore) cleanup(interval time.Duration, quit <-chan struct{}, done chan<- struct{}) {
|
||||
ticker := time.NewTicker(interval)
|
||||
|
||||
defer func() {
|
||||
ticker.Stop()
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-quit:
|
||||
// Handle the quit signal
|
||||
done <- struct{}{}
|
||||
return
|
||||
case <-ticker.C:
|
||||
// Delete expired sessions on each tick
|
||||
err := db.deleteExpired()
|
||||
if err != nil {
|
||||
log.Printf("pgstore: unable to delete expired sessions: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// deleteExpired deletes expired sessions from the database.
|
||||
func (db *PGStore) deleteExpired() error {
|
||||
_, err := db.DbMap.Exec("DELETE FROM http_sessions WHERE expireson < now()")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user