return wg to addrbook

This commit is contained in:
Anton Kaliaev 2017-04-17 13:03:26 +04:00
parent 0277e52bd5
commit 4c0d1d3ad2
No known key found for this signature in database
GPG Key ID: 7B6881D965918214
1 changed files with 16 additions and 10 deletions

View File

@ -73,7 +73,12 @@ const (
serializationVersion = 1 serializationVersion = 1
) )
/* AddrBook - concurrency safe peer address manager */ const (
bucketTypeNew = 0x01
bucketTypeOld = 0x02
)
// AddrBook - concurrency safe peer address manager.
type AddrBook struct { type AddrBook struct {
BaseService BaseService
@ -86,15 +91,12 @@ type AddrBook struct {
addrLookup map[string]*knownAddress // new & old addrLookup map[string]*knownAddress // new & old
addrNew []map[string]*knownAddress addrNew []map[string]*knownAddress
addrOld []map[string]*knownAddress addrOld []map[string]*knownAddress
wg sync.WaitGroup
nOld int nOld int
nNew int nNew int
} }
const ( // NewAddrBook creates a new address book.
bucketTypeNew = 0x01
bucketTypeOld = 0x02
)
// Use Start to begin processing asynchronous address updates. // Use Start to begin processing asynchronous address updates.
func NewAddrBook(filePath string, routabilityStrict bool) *AddrBook { func NewAddrBook(filePath string, routabilityStrict bool) *AddrBook {
am := &AddrBook{ am := &AddrBook{
@ -124,19 +126,22 @@ func (a *AddrBook) init() {
} }
} }
// OnStart implements Service.
func (a *AddrBook) OnStart() error { func (a *AddrBook) OnStart() error {
a.BaseService.OnStart() a.BaseService.OnStart()
a.loadFromFile(a.filePath) a.loadFromFile(a.filePath)
a.wg.Add(1)
go a.saveRoutine() go a.saveRoutine()
return nil return nil
} }
func (a *AddrBook) OnStop() { func (a *AddrBook) Wait() {
a.BaseService.OnStop() a.wg.Wait()
} }
func (a *AddrBook) Wait() { // OnStop implements Service.
a.saveToFile(a.filePath) func (a *AddrBook) OnStop() {
a.BaseService.OnStop()
} }
func (a *AddrBook) AddOurAddress(addr *NetAddress) { func (a *AddrBook) AddOurAddress(addr *NetAddress) {
@ -399,6 +404,7 @@ out:
} }
dumpAddressTicker.Stop() dumpAddressTicker.Stop()
a.saveToFile(a.filePath) a.saveToFile(a.filePath)
a.wg.Done()
log.Notice("Address handler done") log.Notice("Address handler done")
} }