periodically schedule gc

This commit is contained in:
vyzo
2019-04-28 12:17:48 +03:00
parent d0a3cc17f4
commit e4cf50160f

View File

@@ -37,14 +37,41 @@ type memoryAddrBook struct {
addrs map[peer.ID]map[string]*expiringAddr
nextGC time.Time
ctx context.Context
cancel func()
subManager *AddrSubManager
}
func NewAddrBook() pstore.AddrBook {
return &memoryAddrBook{
ctx, cancel := context.WithCancel(context.Background())
ab := &memoryAddrBook{
addrs: make(map[peer.ID]map[string]*expiringAddr),
subManager: NewAddrSubManager(),
ctx: ctx,
cancel: cancel,
}
go ab.background()
return ab
}
// background periodically schedules a gc
func (mab *memoryAddrBook) background() {
ticker := time.NewTicker(1 * time.Hour)
defer ticker.Stop()
for {
select {
case <-ticker.C:
mab.addrmu.Lock()
mab.gc()
mab.addrmu.Unlock()
case <-mab.ctx.Done():
return
}
}
}