Skip to content

Commit

Permalink
allow concurrent calls to Close and SetTTL in the ObservedAddrManager
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Sep 7, 2021
1 parent 2cf1c45 commit 9342776
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions p2p/protocol/identify/obsaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ type ObservedAddrManager struct {
// active connection -> most recent observation
activeConns map[network.Conn]ma.Multiaddr

mu sync.RWMutex
mu sync.RWMutex
closed bool
// local(internal) address -> list of observed(external) addresses
addrs map[string][]*observedAddr
ttl time.Duration
Expand Down Expand Up @@ -530,9 +531,14 @@ func (oas *ObservedAddrManager) emitSpecificNATType(addrs []*observedAddr, proto
func (oas *ObservedAddrManager) Close() error {
oas.closeOnce.Do(func() {
oas.ctxCancel()

oas.mu.Lock()
oas.closed = true
oas.refreshTimer.Stop()
oas.mu.Unlock()

oas.refCount.Wait()
oas.reachabilitySub.Close()
oas.refreshTimer.Stop()
oas.host.Network().StopNotify((*obsAddrNotifiee)(oas))
})
return nil
Expand All @@ -558,6 +564,9 @@ func observerGroup(m ma.Multiaddr) string {
func (oas *ObservedAddrManager) SetTTL(ttl time.Duration) {
oas.mu.Lock()
defer oas.mu.Unlock()
if oas.closed {
return
}
oas.ttl = ttl
// refresh every ttl/2 so we don't forget observations from connected peers
oas.refreshTimer.Reset(ttl / 2)
Expand Down

0 comments on commit 9342776

Please sign in to comment.