Browse Source

Merge pull request #608 from libp2p/feat/cache-relay-addrs

cache relayAddrs for a short period of time
pull/609/head
Steven Allen 6 years ago
committed by GitHub
parent
commit
48800a6aca
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      p2p/host/relay/autorelay.go

17
p2p/host/relay/autorelay.go

@ -43,6 +43,9 @@ type AutoRelay struct {
mx sync.Mutex
relays map[peer.ID]struct{}
status autonat.NATStatus
cachedAddrs []ma.Multiaddr
cachedAddrsExpiry time.Time
}
func NewAutoRelay(ctx context.Context, bhost *basic.BasicHost, discover discovery.Discoverer, router routing.PeerRouting) *AutoRelay {
@ -108,6 +111,9 @@ func (ar *AutoRelay) background(ctx context.Context) {
}
if push {
ar.mx.Lock()
ar.cachedAddrs = nil
ar.mx.Unlock()
push = false
ar.host.PushIdentify()
}
@ -250,16 +256,20 @@ func (ar *AutoRelay) selectRelays(ctx context.Context, pis []pstore.PeerInfo) []
// through which we can be dialed.
func (ar *AutoRelay) relayAddrs(addrs []ma.Multiaddr) []ma.Multiaddr {
ar.mx.Lock()
defer ar.mx.Unlock()
if ar.status != autonat.NATStatusPrivate {
ar.mx.Unlock()
return addrs
}
if ar.cachedAddrs != nil && time.Now().Before(ar.cachedAddrsExpiry) {
return ar.cachedAddrs
}
relays := make([]peer.ID, 0, len(ar.relays))
for p := range ar.relays {
relays = append(relays, p)
}
ar.mx.Unlock()
raddrs := make([]ma.Multiaddr, 0, 4*len(relays)+2)
@ -285,6 +295,9 @@ func (ar *AutoRelay) relayAddrs(addrs []ma.Multiaddr) []ma.Multiaddr {
}
}
ar.cachedAddrs = raddrs
ar.cachedAddrsExpiry = time.Now().Add(30 * time.Second)
return raddrs
}

Loading…
Cancel
Save