Browse Source

fixes from bucky's review

pull/1292/head
Anton Kaliaev 7 years ago
parent
commit
0f41570c80
No known key found for this signature in database GPG Key ID: 7B6881D965918214
2 changed files with 15 additions and 12 deletions
  1. +14
    -9
      p2p/pex/pex_reactor.go
  2. +1
    -3
      rpc/lib/client/ws_client.go

+ 14
- 9
p2p/pex/pex_reactor.go View File

@ -39,6 +39,8 @@ const (
defaultSeedDisconnectWaitPeriod = 2 * time.Minute // disconnect after this
defaultCrawlPeerInterval = 2 * time.Minute // dont redial for this. TODO: back-off
defaultCrawlPeersPeriod = 30 * time.Second // check some peers every this
maxAttemptsToDial = 16 // ~ 35h in total (last attempt - 18h sleep)
)
// PEXReactor handles PEX (peer exchange) and ensures that an
@ -61,7 +63,7 @@ type PEXReactor struct {
requestsSent *cmn.CMap // ID->struct{}: unanswered send requests
lastReceivedRequests *cmn.CMap // ID->time.Time: last time peer requested from us
attemptsToDial sync.Map // dial addr -> number of attempts to dial (for exponential backoff)
attemptsToDial sync.Map // dial address (string) -> number of attempts (int) to dial (for exponential backoff)
}
// PEXReactorConfig holds reactor specific configuration data.
@ -365,22 +367,25 @@ func (r *PEXReactor) ensurePeers() {
}
func (r *PEXReactor) dialPeer(addr *p2p.NetAddress) {
// 1s == (1e9 ns) == (1 Billion ns)
billionNs := float64(time.Second.Nanoseconds())
attempts := r.AttemptsToDial(addr)
if attempts > maxAttemptsToDial {
r.Logger.Error("Reached max attempts to dial", "addr", addr, "attempts", attempts)
r.book.MarkBad(addr)
return
}
// exponential backoff if it's not our first attempt to dial given address
var attempts int
if lAttempts, attempted := r.attemptsToDial.Load(addr.DialString()); attempted {
attempts = lAttempts.(int)
jitterSeconds := time.Duration(rand.Float64() * billionNs)
if attempts != 0 {
jitterSeconds := time.Duration(rand.Float64() * float64(time.Second)) // 1s == (1e9 ns)
backoffDuration := jitterSeconds + ((1 << uint(attempts)) * time.Second)
r.Logger.Debug(fmt.Sprintf("Dialing %v", addr), "attempts", attempts, "backoff_duration", backoffDuration)
r.Logger.Debug("Sleeping before dialing", "addr", addr, "dur", backoffDuration)
time.Sleep(backoffDuration)
}
err := r.Switch.DialPeerWithAddress(addr, false)
if err != nil {
r.Logger.Error("Dialing failed", "err", err)
r.Logger.Error("Dialing failed", "addr", addr, "err", err, "attempts", attempts)
// TODO: detect more "bad peer" scenarios
if _, ok := err.(p2p.ErrSwitchAuthenticationFailure); ok {
r.book.MarkBad(addr)


+ 1
- 3
rpc/lib/client/ws_client.go View File

@ -254,10 +254,8 @@ func (c *WSClient) reconnect() error {
c.mtx.Unlock()
}()
// 1s == (1e9 ns) == (1 Billion ns)
billionNs := float64(time.Second.Nanoseconds())
for {
jitterSeconds := time.Duration(rand.Float64() * billionNs)
jitterSeconds := time.Duration(rand.Float64() * float64(time.Second)) // 1s == (1e9 ns)
backoffDuration := jitterSeconds + ((1 << uint(attempt)) * time.Second)
c.Logger.Info("reconnecting", "attempt", attempt+1, "backoff_duration", backoffDuration)


Loading…
Cancel
Save