Skip to content

Commit

Permalink
Merge pull request #1115 from snyh/nolistener
Browse files Browse the repository at this point in the history
perf/basic_host: Don't handle address change if we hasn't anyone
  • Loading branch information
Stebalien committed Jun 30, 2021
2 parents 8ec1eec + 219a23a commit e5d5d44
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions p2p/host/basic/basic_host.go
Expand Up @@ -520,7 +520,11 @@ func (h *BasicHost) background() {
defer ticker.Stop()

for {
h.updateLocalIpAddr()
if len(h.network.ListenAddresses()) > 0 {
h.updateLocalIpAddr()
}
// Request addresses anyways because, technically, address filters still apply.
// The underlying AllAddrs call is effectivley a no-op.
curr := h.Addrs()
emitAddrChange(curr, lastAddrs)
lastAddrs = curr
Expand Down Expand Up @@ -813,6 +817,11 @@ func dedupAddrs(addrs []ma.Multiaddr) (uniqueAddrs []ma.Multiaddr) {
// AllAddrs returns all the addresses of BasicHost at this moment in time.
// It's ok to not include addresses if they're not available to be used now.
func (h *BasicHost) AllAddrs() []ma.Multiaddr {
listenAddrs := h.Network().ListenAddresses()
if len(listenAddrs) == 0 {
return nil
}

h.addrMu.RLock()
filteredIfaceAddrs := h.filteredInterfaceAddrs
allIfaceAddrs := h.allInterfaceAddrs
Expand All @@ -821,7 +830,6 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {

// Iterate over all _unresolved_ listen addresses, resolving our primary
// interface only to avoid advertising too many addresses.
listenAddrs := h.Network().ListenAddresses()
var finalAddrs []ma.Multiaddr
if resolved, err := addrutil.ResolveUnspecifiedAddresses(listenAddrs, filteredIfaceAddrs); err != nil {
// This can happen if we're listening on no addrs, or listening
Expand Down

0 comments on commit e5d5d44

Please sign in to comment.