p2p/pex: some addrbook fixes

* fix before/after in isBad()
* allow multiple IPs per ID even if ID isOld
This commit is contained in:
Ethan Buchman 2018-04-28 20:07:38 -04:00
parent 3a30ee75b9
commit fae94a44a2
3 changed files with 18 additions and 15 deletions

View File

@ -626,8 +626,8 @@ func (a *addrBook) addAddress(addr, src *p2p.NetAddress) error {
ka := a.addrLookup[addr.ID] ka := a.addrLookup[addr.ID]
if ka != nil { if ka != nil {
// Already old. // If its already old and the addr is the same, ignore it.
if ka.isOld() { if ka.isOld() && ka.Addr.Equals(addr) {
return nil return nil
} }
// Already in max new buckets. // Already in max new buckets.

View File

@ -106,7 +106,6 @@ func (ka *knownAddress) removeBucketRef(bucketIdx int) int {
All addresses that meet these criteria are assumed to be worthless and not All addresses that meet these criteria are assumed to be worthless and not
worth keeping hold of. worth keeping hold of.
XXX: so a good peer needs us to call MarkGood before the conditions above are reached!
*/ */
func (ka *knownAddress) isBad() bool { func (ka *knownAddress) isBad() bool {
// Is Old --> good // Is Old --> good
@ -115,14 +114,15 @@ func (ka *knownAddress) isBad() bool {
} }
// Has been attempted in the last minute --> good // Has been attempted in the last minute --> good
if ka.LastAttempt.Before(time.Now().Add(-1 * time.Minute)) { if ka.LastAttempt.After(time.Now().Add(-1 * time.Minute)) {
return false return false
} }
// TODO: From the future?
// Too old? // Too old?
// XXX: does this mean if we've kept a connection up for this long we'll disconnect?! // TODO: should be a timestamp of last seen, not just last attempt
// and shouldn't it be .Before ? if ka.LastAttempt.Before(time.Now().Add(-1 * numMissingDays * time.Hour * 24)) {
if ka.LastAttempt.After(time.Now().Add(-1 * numMissingDays * time.Hour * 24)) {
return true return true
} }
@ -132,7 +132,6 @@ func (ka *knownAddress) isBad() bool {
} }
// Hasn't succeeded in too long? // Hasn't succeeded in too long?
// XXX: does this mean if we've kept a connection up for this long we'll disconnect?!
if ka.LastSuccess.Before(time.Now().Add(-1*minBadDays*time.Hour*24)) && if ka.LastSuccess.Before(time.Now().Add(-1*minBadDays*time.Hour*24)) &&
ka.Attempts >= maxFailures { ka.Attempts >= maxFailures {
return true return true

View File

@ -290,6 +290,8 @@ func (sw *Switch) stopAndRemovePeer(peer Peer, reason interface{}) {
// to the PEX/Addrbook to find the peer with the addr again // to the PEX/Addrbook to find the peer with the addr again
// NOTE: this will keep trying even if the handshake or auth fails. // NOTE: this will keep trying even if the handshake or auth fails.
// TODO: be more explicit with error types so we only retry on certain failures // TODO: be more explicit with error types so we only retry on certain failures
// - ie. if we're getting ErrDuplicatePeer we can stop
// because the addrbook got us the peer back already
func (sw *Switch) reconnectToPeer(addr *NetAddress) { func (sw *Switch) reconnectToPeer(addr *NetAddress) {
if sw.reconnecting.Has(string(addr.ID)) { if sw.reconnecting.Has(string(addr.ID)) {
return return
@ -305,14 +307,14 @@ func (sw *Switch) reconnectToPeer(addr *NetAddress) {
} }
err := sw.DialPeerWithAddress(addr, true) err := sw.DialPeerWithAddress(addr, true)
if err != nil { if err == nil {
sw.Logger.Info("Error reconnecting to peer. Trying again", "tries", i, "err", err, "addr", addr) return // success
// sleep a set amount
sw.randomSleep(reconnectInterval)
continue
} else {
return
} }
sw.Logger.Info("Error reconnecting to peer. Trying again", "tries", i, "err", err, "addr", addr)
// sleep a set amount
sw.randomSleep(reconnectInterval)
continue
} }
sw.Logger.Error("Failed to reconnect to peer. Beginning exponential backoff", sw.Logger.Error("Failed to reconnect to peer. Beginning exponential backoff",
@ -501,6 +503,8 @@ func (sw *Switch) addInboundPeerWithConfig(conn net.Conn, config *PeerConfig) er
// dial the peer; make secret connection; authenticate against the dialed ID; // dial the peer; make secret connection; authenticate against the dialed ID;
// add the peer. // add the peer.
// if dialing fails, start the reconnect loop. If handhsake fails, its over.
// If peer is started succesffuly, reconnectLoop will start when StopPeerForError is called
func (sw *Switch) addOutboundPeerWithConfig(addr *NetAddress, config *PeerConfig, persistent bool) error { func (sw *Switch) addOutboundPeerWithConfig(addr *NetAddress, config *PeerConfig, persistent bool) error {
sw.Logger.Info("Dialing peer", "address", addr) sw.Logger.Info("Dialing peer", "address", addr)
peerConn, err := newOutboundPeerConn(addr, config, persistent, sw.nodeKey.PrivKey) peerConn, err := newOutboundPeerConn(addr, config, persistent, sw.nodeKey.PrivKey)