2017-04-10 16:54:12 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
var quit chan struct{}
|
|
|
|
|
|
|
|
// Start workers of database
|
|
|
|
// WARNING: Do not override this function
|
|
|
|
// you should use New()
|
|
|
|
func Start(conn Connection, config *runtime.Config) {
|
|
|
|
quit = make(chan struct{})
|
|
|
|
go deleteWorker(conn, config.Database.DeleteInterval.Duration, config.Database.DeleteAfter.Duration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Close(conn Connection) {
|
|
|
|
if quit != nil {
|
|
|
|
close(quit)
|
|
|
|
}
|
|
|
|
if conn != nil {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// prunes node-specific data periodically
|
|
|
|
func deleteWorker(conn Connection, deleteInterval time.Duration, deleteAfter time.Duration) {
|
|
|
|
ticker := time.NewTicker(deleteInterval)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2017-04-17 18:42:06 +00:00
|
|
|
conn.PruneNodes(deleteAfter)
|
2017-04-10 16:54:12 +00:00
|
|
|
case <-quit:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|