From b6c062c451689c2141d535d2491e67244a277e3f Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Mon, 30 Apr 2018 08:19:19 -0400 Subject: [PATCH] fixes from review --- .../new-spec/reactors/pex/pex.md | 8 +++---- p2p/pex/addrbook.go | 21 ++++++++++++------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/docs/specification/new-spec/reactors/pex/pex.md b/docs/specification/new-spec/reactors/pex/pex.md index c879c336a..44a0e3fd8 100644 --- a/docs/specification/new-spec/reactors/pex/pex.md +++ b/docs/specification/new-spec/reactors/pex/pex.md @@ -20,9 +20,9 @@ Peer discovery begins with a list of seeds. When we have no peers, or have been unable to find enough peers from existing ones, we dial a randomly selected seed to get a list of peers to dial. -On startup, we will also immediately dial the given list of `persisten_peers`, -and will attempt to maintain persistent connections with them. If the connections die, or we fail to dail, -we will redial every 5s for a a few minutes, then switch to an exponential backoff schedule, +On startup, we will also immediately dial the given list of `persistent_peers`, +and will attempt to maintain persistent connections with them. If the connections die, or we fail to dial, +we will redial every 5s for a few minutes, then switch to an exponential backoff schedule, and after about a day of trying, stop dialing the peer. So long as we have less than `MinNumOutboundPeers`, we periodically request additional peers @@ -84,7 +84,7 @@ Connection attempts are made with exponential backoff (plus jitter). Because the selection process happens every `ensurePeersPeriod`, we might not end up dialing a peer for much longer than the backoff duration. -If we fail to conenct to the peer after 16 tries (with exponential backoff), we remove from address book completely. +If we fail to connect to the peer after 16 tries (with exponential backoff), we remove from address book completely. ## Select Peers to Exchange diff --git a/p2p/pex/addrbook.go b/p2p/pex/addrbook.go index 5b4428f5e..4408c3b91 100644 --- a/p2p/pex/addrbook.go +++ b/p2p/pex/addrbook.go @@ -221,7 +221,11 @@ func (a *addrBook) PickAddress(biasTowardsNewAddrs int) *p2p.NetAddress { a.mtx.Lock() defer a.mtx.Unlock() - if a.size() == 0 { + bookSize := a.size() + if bookSize <= 0 { + if bookSize < 0 { + a.Logger.Error("Addrbook size less than 0", "nNew", a.nNew, "nOld", a.nOld) + } return nil } if biasTowardsNewAddrs > 100 { @@ -301,7 +305,10 @@ func (a *addrBook) GetSelection() []*p2p.NetAddress { defer a.mtx.Unlock() bookSize := a.size() - if bookSize == 0 { + if bookSize <= 0 { + if bookSize < 0 { + a.Logger.Error("Addrbook size less than 0", "nNew", a.nNew, "nOld", a.nOld) + } return nil } @@ -344,7 +351,10 @@ func (a *addrBook) GetSelectionWithBias(biasTowardsNewAddrs int) []*p2p.NetAddre defer a.mtx.Unlock() bookSize := a.size() - if bookSize == 0 { + if bookSize <= 0 { + if bookSize < 0 { + a.Logger.Error("Addrbook size less than 0", "nNew", a.nNew, "nOld", a.nOld) + } return nil } @@ -609,10 +619,7 @@ func (a *addrBook) pickOldest(bucketType byte, bucketIdx int) *knownAddress { // adds the address to a "new" bucket. if its already in one, // it only adds it probabilistically func (a *addrBook) addAddress(addr, src *p2p.NetAddress) error { - if addr == nil { - return ErrAddrBookNilAddr{addr, src} - } - if src == nil { + if addr == nil || src == nil { return ErrAddrBookNilAddr{addr, src} }