mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2025-02-27 09:30:07 +08:00
increment WaitGroups before spawning goroutines.
This commit is contained in:
parent
86f1626c51
commit
d7e9f78273
@ -127,7 +127,9 @@ type dsAddrBook struct {
|
||||
|
||||
flushJobCh chan *addrsRecord
|
||||
cancelFn func()
|
||||
done sync.WaitGroup
|
||||
|
||||
// controls children goroutine lifetime.
|
||||
childrenDone sync.WaitGroup
|
||||
}
|
||||
|
||||
var _ pstore.AddrBook = (*dsAddrBook)(nil)
|
||||
@ -174,6 +176,7 @@ func NewAddrBook(ctx context.Context, store ds.Batching, opts Options) (ab *dsAd
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ab.childrenDone.Add(1)
|
||||
go ab.flusher()
|
||||
|
||||
return ab, nil
|
||||
@ -181,7 +184,7 @@ func NewAddrBook(ctx context.Context, store ds.Batching, opts Options) (ab *dsAd
|
||||
|
||||
func (ab *dsAddrBook) Close() {
|
||||
ab.cancelFn()
|
||||
ab.done.Wait()
|
||||
ab.childrenDone.Wait()
|
||||
}
|
||||
|
||||
func (ab *dsAddrBook) asyncFlush(pr *addrsRecord) {
|
||||
@ -234,8 +237,7 @@ func (ab *dsAddrBook) loadRecord(id peer.ID, cache bool, update bool) (pr *addrs
|
||||
|
||||
// flusher is a goroutine that takes care of persisting asynchronous flushes to the datastore.
|
||||
func (ab *dsAddrBook) flusher() {
|
||||
ab.done.Add(1)
|
||||
defer ab.done.Done()
|
||||
defer ab.childrenDone.Done()
|
||||
|
||||
for {
|
||||
select {
|
||||
|
@ -82,6 +82,7 @@ func newAddressBookGc(ctx context.Context, ab *dsAddrBook) (*dsAddrBookGc, error
|
||||
|
||||
// do not start GC timers if purge is disabled; this GC can only be triggered manually.
|
||||
if ab.opts.GCPurgeInterval > 0 {
|
||||
gc.ab.childrenDone.Add(1)
|
||||
go gc.background()
|
||||
}
|
||||
|
||||
@ -90,8 +91,7 @@ func newAddressBookGc(ctx context.Context, ab *dsAddrBook) (*dsAddrBookGc, error
|
||||
|
||||
// gc prunes expired addresses from the datastore at regular intervals. It should be spawned as a goroutine.
|
||||
func (gc *dsAddrBookGc) background() {
|
||||
gc.ab.done.Add(1)
|
||||
defer gc.ab.done.Done()
|
||||
defer gc.ab.childrenDone.Done()
|
||||
|
||||
select {
|
||||
case <-time.After(gc.ab.opts.GCInitialDelay):
|
||||
|
Loading…
Reference in New Issue
Block a user