Skip to content

Commit

Permalink
Retry when DiscoverNAT fails for 5 times
Browse files Browse the repository at this point in the history
  • Loading branch information
hottestchilipepper committed Oct 21, 2023
1 parent fde9c41 commit e62bee7
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions p2p/host/basic/natmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import (
ma "github.com/multiformats/go-multiaddr"
)

// discoveryNATPeriod is the period at which we try to discover NATs.
var discoveryNATPeriod = 3 * time.Second

// discoveryTry is the number of times we try to discover NATs.
const discoveryTry = 5

// NATManager is a simple interface to manage NAT devices.
type NATManager interface {
// NAT gets the NAT device managed by the NAT manager.
Expand Down Expand Up @@ -88,11 +94,20 @@ func (nmgr *natManager) background(ctx context.Context) {

discoverCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
tryCount := 0
natInstance, err := inat.DiscoverNAT(discoverCtx)
if err != nil {
tryCount++
for err != nil {
log.Info("DiscoverNAT error:", err)
close(nmgr.ready)
return
if tryCount > discoveryTry {
log.Info("DiscoverNAT failed after ", tryCount, " tries")
return
}
time.Sleep(discoveryNATPeriod)
discoverCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
natInstance, err = inat.DiscoverNAT(discoverCtx)
tryCount++
}

nmgr.natMx.Lock()
Expand Down

0 comments on commit e62bee7

Please sign in to comment.