From 09f9717473c4f7f58e819da0becc5934206b6d96 Mon Sep 17 00:00:00 2001 From: Marco Munizaga Date: Wed, 16 Oct 2024 11:50:47 -0700 Subject: [PATCH] nits --- p2p/host/autonat/autonat.go | 15 +++++++++------ p2p/host/autonat/options.go | 2 ++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/p2p/host/autonat/autonat.go b/p2p/host/autonat/autonat.go index b675658c95..ddf13dc763 100644 --- a/p2p/host/autonat/autonat.go +++ b/p2p/host/autonat/autonat.go @@ -264,24 +264,27 @@ func (as *AmbientAutoNAT) checkAddrs() (hasNewAddr bool) { func (as *AmbientAutoNAT) scheduleProbe(forceProbe bool) time.Duration { now := time.Now() currentStatus := *as.status.Load() - nextProbeAfter := as.config.refreshInterval - if forceProbe && currentStatus == network.ReachabilityUnknown { + receivedInbound := as.lastInbound.After(as.lastProbe) + switch { + case forceProbe && currentStatus == network.ReachabilityUnknown: // retry very quicky if forceProbe is true *and* we don't know our reachability // limit all peers fetch from peerstore to 1 per second. nextProbeAfter = 2 * time.Second - } else if currentStatus == network.ReachabilityUnknown || - as.confidence < maxConfidence || - (currentStatus != network.ReachabilityPublic && as.lastInbound.After(as.lastProbe)) { + nextProbeAfter = 2 * time.Second + case currentStatus == network.ReachabilityUnknown, + as.confidence < maxConfidence, + currentStatus != network.ReachabilityPublic && receivedInbound: // Retry quickly in case: // 1. Our reachability is Unknown // 2. We don't have enough confidence in our reachability. // 3. We're private but we received an inbound connection. nextProbeAfter = as.config.retryInterval - } else if currentStatus == network.ReachabilityPublic && as.lastInbound.After(as.lastProbe) { + case currentStatus == network.ReachabilityPublic && receivedInbound: // We are public and we received an inbound connection recently, // wait a little longer nextProbeAfter *= 2 + nextProbeAfter = min(nextProbeAfter, maxRefreshInterval) } nextProbeTime := as.lastProbe.Add(nextProbeAfter) if nextProbeTime.Before(now) { diff --git a/p2p/host/autonat/options.go b/p2p/host/autonat/options.go index dec62c5f1d..b378da348d 100644 --- a/p2p/host/autonat/options.go +++ b/p2p/host/autonat/options.go @@ -51,6 +51,8 @@ var defaults = func(c *config) error { return nil } +const maxRefreshInterval = 24 * time.Hour + // EnableService specifies that AutoNAT should be allowed to run a NAT service to help // other peers determine their own NAT status. The provided Network should not be the // default network/dialer of the host passed to `New`, as the NAT system will need to