mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-14 13:51:21 +00:00
fixes from bucky's review
This commit is contained in:
@ -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)
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user