Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

implement a clean shutdown for the memory address book #192

Merged
merged 1 commit into from
Dec 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions pstoremem/addr_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func (segments *addrSegments) get(p peer.ID) *addrSegment {
type memoryAddrBook struct {
segments addrSegments

ctx context.Context
cancel func()
refCount sync.WaitGroup
cancel func()

subManager *AddrSubManager
}
Expand All @@ -77,32 +77,32 @@ func NewAddrBook() *memoryAddrBook {
return ret
}(),
subManager: NewAddrSubManager(),
ctx: ctx,
cancel: cancel,
}

go ab.background()
ab.refCount.Add(1)
go ab.background(ctx)
return ab
}

// background periodically schedules a gc
func (mab *memoryAddrBook) background() {
func (mab *memoryAddrBook) background(ctx context.Context) {
defer mab.refCount.Done()
ticker := time.NewTicker(1 * time.Hour)
defer ticker.Stop()

for {
select {
case <-ticker.C:
mab.gc()

case <-mab.ctx.Done():
case <-ctx.Done():
return
}
}
}

func (mab *memoryAddrBook) Close() error {
mab.cancel()
mab.refCount.Wait()
return nil
}

Expand Down