diff --git a/pkg/network/discovery.go b/pkg/network/discovery.go index f33e98980..5659ee6c0 100644 --- a/pkg/network/discovery.go +++ b/pkg/network/discovery.go @@ -2,6 +2,7 @@ package network import ( "math" + "math/rand" "sync" "sync/atomic" "time" @@ -14,6 +15,11 @@ const ( connRetries = 3 ) +var ( + // Maximum waiting time before connection attempt. + tryMaxWait = time.Second / 2 +) + // Discoverer is an interface that is responsible for maintaining // a healthy connection pool. type Discoverer interface { @@ -294,6 +300,8 @@ func (d *DefaultDiscovery) updateNetSize() { } func (d *DefaultDiscovery) tryAddress(addr string) { + var tout = rand.Int63n(int64(tryMaxWait)) + time.Sleep(time.Duration(tout)) // Have a sleep before working hard. p, err := d.transport.Dial(addr, d.dialTimeout) atomic.AddInt32(&d.outstanding, -1) d.lock.Lock() diff --git a/pkg/network/discovery_test.go b/pkg/network/discovery_test.go index f5df858a5..2c1d83fbf 100644 --- a/pkg/network/discovery_test.go +++ b/pkg/network/discovery_test.go @@ -82,6 +82,7 @@ func TestDefaultDiscoverer(t *testing.T) { ts.dialCh = make(chan string) d := NewDefaultDiscovery(nil, time.Second/16, ts) + tryMaxWait = 1 // Don't waste time. var set1 = []string{"1.1.1.1:10333", "2.2.2.2:10333"} sort.Strings(set1) @@ -211,6 +212,7 @@ func TestSeedDiscovery(t *testing.T) { sort.Strings(seeds) d := NewDefaultDiscovery(seeds, time.Second/10, ts) + tryMaxWait = 1 // Don't waste time. d.RequestRemote(len(seeds)) for i := 0; i < connRetries*2; i++ {