2017-04-10 16:54:12 +00:00
|
|
|
package all
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/FreifunkBremen/yanic/database"
|
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
|
|
|
)
|
|
|
|
|
2018-01-03 14:41:40 +00:00
|
|
|
var conn database.Connection
|
|
|
|
var quit chan struct{}
|
2017-04-17 23:48:38 +00:00
|
|
|
|
2018-01-03 14:41:40 +00:00
|
|
|
func Start(config runtime.DatabaseConfig) (err error) {
|
|
|
|
conn, err = Connect(config.Connection)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2017-04-10 16:54:12 +00:00
|
|
|
}
|
2018-01-03 14:41:40 +00:00
|
|
|
quit = make(chan struct{})
|
|
|
|
go deleteWorker(config.DeleteInterval.Duration, config.DeleteAfter.Duration)
|
|
|
|
return
|
2017-04-10 16:54:12 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 14:41:40 +00:00
|
|
|
func Close() {
|
|
|
|
close(quit)
|
|
|
|
conn.Close()
|
|
|
|
quit = nil
|
2017-04-10 16:54:12 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 14:41:40 +00:00
|
|
|
// prunes node-specific data periodically
|
|
|
|
func deleteWorker(deleteInterval time.Duration, deleteAfter time.Duration) {
|
|
|
|
ticker := time.NewTicker(deleteInterval)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
conn.PruneNodes(deleteAfter)
|
|
|
|
case <-quit:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
2017-04-10 16:54:12 +00:00
|
|
|
}
|
|
|
|
}
|