yanic/database/all/internal.go

46 lines
773 B
Go
Raw Permalink Normal View History

package all
import (
"sync"
"time"
"github.com/FreifunkBremen/yanic/database"
)
2018-01-19 03:31:33 +00:00
var Conn database.Connection
var wg = sync.WaitGroup{}
2018-01-03 14:41:40 +00:00
var quit chan struct{}
2017-04-17 23:48:38 +00:00
func Start(config database.Config) (err error) {
2018-01-19 03:31:33 +00:00
Conn, err = Connect(config.Connection)
2018-01-03 14:41:40 +00:00
if err != nil {
return
}
2018-01-03 14:41:40 +00:00
quit = make(chan struct{})
wg.Add(1)
2018-01-03 14:41:40 +00:00
go deleteWorker(config.DeleteInterval.Duration, config.DeleteAfter.Duration)
return
}
2018-01-03 14:41:40 +00:00
func Close() {
close(quit)
wg.Wait()
2018-01-19 03:31:33 +00:00
Conn.Close()
2018-01-03 14:41:40 +00:00
quit = nil
}
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:
2018-01-19 03:31:33 +00:00
Conn.PruneNodes(deleteAfter)
2018-01-03 14:41:40 +00:00
case <-quit:
ticker.Stop()
wg.Done()
2018-01-03 14:41:40 +00:00
return
}
}
}