yanic/database/all/internal.go

46 lines
773 B
Go
Raw Normal View History

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