boltstore/reaper/reaper.go

136 lines
2.9 KiB
Go
Raw Normal View History

2014-06-14 20:15:10 +04:00
package reaper
import (
2014-12-18 04:54:30 +03:00
"fmt"
2014-06-14 20:15:10 +04:00
"log"
"time"
"github.com/boltdb/bolt"
"github.com/yosssi/boltstore/shared"
)
2014-12-18 04:54:30 +03:00
//##############//
//### Public ###//
//##############//
2014-06-14 20:15:10 +04:00
// Run invokes a reap function as a goroutine.
func Run(db *bolt.DB, options Options) (chan<- struct{}, <-chan struct{}) {
options.setDefault()
quitC, doneC := make(chan struct{}), make(chan struct{})
go reap(db, options, quitC, doneC)
return quitC, doneC
}
2014-06-14 20:17:36 +04:00
// Quit terminates the reap goroutine.
2014-06-14 20:15:10 +04:00
func Quit(quitC chan<- struct{}, doneC <-chan struct{}) {
quitC <- struct{}{}
<-doneC
}
2014-12-18 04:54:30 +03:00
//###############//
//### Private ###//
//###############//
2014-06-14 20:15:10 +04:00
func reap(db *bolt.DB, options Options, quitC <-chan struct{}, doneC chan<- struct{}) {
var prevKey []byte
for {
2014-12-18 04:54:30 +03:00
// This slice is a buffer to save all expired session keys.
expiredSessionKeys := make([][]byte, 0)
// Start a bolt read transaction.
2014-06-14 20:15:10 +04:00
err := db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(options.BucketName)
if bucket == nil {
return nil
}
c := bucket.Cursor()
var i int
2014-12-18 04:54:30 +03:00
var isExpired bool
2014-06-14 20:15:10 +04:00
for k, v := c.Seek(prevKey); ; k, v = c.Next() {
// If we hit the end of our sessions then
// exit and start over next time.
if k == nil {
prevKey = nil
return nil
}
i++
2014-12-18 04:54:30 +03:00
// The flag if the session is expired
isExpired = false
2014-06-14 20:15:10 +04:00
session, err := shared.Session(v)
if err != nil {
2014-12-18 04:54:30 +03:00
// Just remove the session with the invalid session data.
// Log the error first.
log.Printf("boltstore: removing session from database with invalid value: %v", err)
isExpired = true
} else if shared.Expired(session) {
isExpired = true
2014-06-14 20:15:10 +04:00
}
2014-12-18 04:54:30 +03:00
if isExpired {
// Copy the byte slice key, because this data is
// not safe outside of this transaction.
temp := make([]byte, len(k))
copy(temp, k)
// Add it to the expired sessios keys slice
expiredSessionKeys = append(expiredSessionKeys, temp)
2014-06-14 20:15:10 +04:00
}
if options.BatchSize == i {
2014-12-17 23:42:57 +03:00
// Store the current key to the previous key.
// Copy the byte slice key, because this data is
// not safe outside of this transaction.
prevKey = make([]byte, len(k))
2014-06-14 20:15:10 +04:00
copy(prevKey, k)
return nil
}
}
})
if err != nil {
2014-12-18 04:54:30 +03:00
log.Printf("boltstore: obtain expired sessions error: %v", err)
}
if len(expiredSessionKeys) > 0 {
// Remove the expired sessions from the database
err = db.Update(func(txu *bolt.Tx) error {
// Get the bucket
b := txu.Bucket(options.BucketName)
if b == nil {
return nil
}
// Remove all expired sessions in the slice
for _, key := range expiredSessionKeys {
err = b.Delete(key)
if err != nil {
return err
}
}
return nil
})
if err != nil {
log.Printf("boltstore: remove expired sessions error: %v", err)
}
2014-06-14 20:15:10 +04:00
}
// Check if a quit signal is sent.
select {
case <-quitC:
doneC <- struct{}{}
return
default:
}
time.Sleep(options.CheckInterval)
}
}