Browse Source

p2p: retry failed connections slightly more aggressively (#8010)

* p2p: retry failed connections slightly more aggressively

* fix dial interval test
pull/8015/head
Sam Kleinman 3 years ago
committed by GitHub
parent
commit
89dbebd1c5
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 13 deletions
  1. +6
    -4
      internal/p2p/peermanager.go
  2. +6
    -6
      internal/p2p/peermanager_test.go
  3. +3
    -3
      node/setup.go

+ 6
- 4
internal/p2p/peermanager.go View File

@ -1004,13 +1004,15 @@ func (m *PeerManager) retryDelay(failures uint32, persistent bool) time.Duration
maxDelay = m.options.MaxRetryTimePersistent maxDelay = m.options.MaxRetryTimePersistent
} }
delay := m.options.MinRetryTime * time.Duration(math.Pow(2, float64(failures-1)))
if maxDelay > 0 && delay > maxDelay {
delay = maxDelay
}
delay := m.options.MinRetryTime * time.Duration(failures)
if m.options.RetryTimeJitter > 0 { if m.options.RetryTimeJitter > 0 {
delay += time.Duration(m.rand.Int63n(int64(m.options.RetryTimeJitter))) delay += time.Duration(m.rand.Int63n(int64(m.options.RetryTimeJitter)))
} }
if maxDelay > 0 && delay > maxDelay {
delay = maxDelay
}
return delay return delay
} }


+ 6
- 6
internal/p2p/peermanager_test.go View File

@ -3,6 +3,7 @@ package p2p_test
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -315,13 +316,14 @@ func TestPeerManager_DialNext_Retry(t *testing.T) {
ctx, cancel = context.WithTimeout(ctx, 5*time.Second) ctx, cancel = context.WithTimeout(ctx, 5*time.Second)
defer cancel() defer cancel()
for i := 0; i <= 5; i++ {
for i := 0; i <= 6; i++ {
start := time.Now() start := time.Now()
dial, err := peerManager.DialNext(ctx) dial, err := peerManager.DialNext(ctx)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, a, dial) require.Equal(t, a, dial)
elapsed := time.Since(start).Round(time.Millisecond) elapsed := time.Since(start).Round(time.Millisecond)
fmt.Println(elapsed, options.MinRetryTime)
switch i { switch i {
case 0: case 0:
require.LessOrEqual(t, elapsed, options.MinRetryTime) require.LessOrEqual(t, elapsed, options.MinRetryTime)
@ -330,14 +332,12 @@ func TestPeerManager_DialNext_Retry(t *testing.T) {
case 2: case 2:
require.GreaterOrEqual(t, elapsed, 2*options.MinRetryTime) require.GreaterOrEqual(t, elapsed, 2*options.MinRetryTime)
case 3: case 3:
require.GreaterOrEqual(t, elapsed, 3*options.MinRetryTime)
case 4, 5, 6:
require.GreaterOrEqual(t, elapsed, 4*options.MinRetryTime) require.GreaterOrEqual(t, elapsed, 4*options.MinRetryTime)
case 4, 5:
require.GreaterOrEqual(t, elapsed, options.MaxRetryTime)
require.LessOrEqual(t, elapsed, 8*options.MinRetryTime)
default: default:
require.Fail(t, "unexpected retry")
t.Fatal("unexpected retry")
} }
require.NoError(t, peerManager.DialFailed(ctx, a)) require.NoError(t, peerManager.DialFailed(ctx, a))
} }
} }


+ 3
- 3
node/setup.go View File

@ -332,10 +332,10 @@ func createPeerManager(
MaxConnected: maxConns, MaxConnected: maxConns,
MaxConnectedUpgrade: 4, MaxConnectedUpgrade: 4,
MaxPeers: 1000, MaxPeers: 1000,
MinRetryTime: 100 * time.Millisecond,
MaxRetryTime: 8 * time.Hour,
MinRetryTime: 250 * time.Millisecond,
MaxRetryTime: 30 * time.Minute,
MaxRetryTimePersistent: 5 * time.Minute, MaxRetryTimePersistent: 5 * time.Minute,
RetryTimeJitter: 3 * time.Second,
RetryTimeJitter: 5 * time.Second,
PrivatePeers: privatePeerIDs, PrivatePeers: privatePeerIDs,
} }


Loading…
Cancel
Save