call saveToFile OnStop

This is better than waiting because while we wait, anything could happen
(crash, timeout of the code who's using addrbook, ...). If we save
immediately, we have much greater chances of success.
This commit is contained in:
Anton Kaliaev 2017-01-16 23:36:10 +04:00
parent 5eeaffd38e
commit 590efc1040
No known key found for this signature in database
GPG Key ID: 7B6881D965918214
1 changed files with 3 additions and 5 deletions

View File

@ -15,7 +15,6 @@ import (
"time"
. "github.com/tendermint/go-common"
"github.com/tendermint/go-crypto"
)
const (
@ -86,7 +85,6 @@ type AddrBook struct {
addrLookup map[string]*knownAddress // new & old
addrNew []map[string]*knownAddress
addrOld []map[string]*knownAddress
wg sync.WaitGroup
nOld int
nNew int
}
@ -128,7 +126,6 @@ func (a *AddrBook) init() {
func (a *AddrBook) OnStart() error {
a.BaseService.OnStart()
a.loadFromFile(a.filePath)
a.wg.Add(1)
go a.saveRoutine()
return nil
}
@ -139,6 +136,7 @@ func (a *AddrBook) OnStop() {
func (a *AddrBook) Wait() {
a.wg.Wait()
a.saveToFile(a.filePath)
}
func (a *AddrBook) AddOurAddress(addr *NetAddress) {
@ -309,6 +307,8 @@ type addrBookJSON struct {
}
func (a *AddrBook) saveToFile(filePath string) {
log.Info("Saving AddrBook to file", "size", a.Size())
// Compile Addrs
addrs := []*knownAddress{}
for _, ka := range a.addrLookup {
@ -386,7 +386,6 @@ out:
for {
select {
case <-dumpAddressTicker.C:
log.Info("Saving AddrBook to file", "size", a.Size())
a.saveToFile(a.filePath)
case <-a.Quit:
break out
@ -394,7 +393,6 @@ out:
}
dumpAddressTicker.Stop()
a.saveToFile(a.filePath)
a.wg.Done()
log.Notice("Address handler done")
}