Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Oct 16, 2024
1 parent c14fc97 commit 09f9717
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 9 additions & 6 deletions p2p/host/autonat/autonat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions p2p/host/autonat/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 09f9717

Please sign in to comment.