OSDN Git Service

try to fix seed handshak fail
authorpaladz <453256728@qq.com>
Thu, 26 Apr 2018 09:34:48 +0000 (17:34 +0800)
committerpaladz <453256728@qq.com>
Thu, 26 Apr 2018 09:34:48 +0000 (17:34 +0800)
p2p/pex_reactor.go
p2p/switch.go

index a76bd25..58fc941 100644 (file)
@@ -122,6 +122,12 @@ func (r *PEXReactor) AddPeer(p *Peer) error {
                }
                r.book.AddAddress(addr, addr)
        }
+       if r.sw.peers.Size() >= r.sw.config.MaxNumPeers {
+               if ok := r.SendAddrs(p, r.book.GetSelection()); ok {
+                       r.sw.StopPeerGracefully(p)
+               }
+               return errors.New("Error in AddPeer: reach the max peer, exchange then close")
+       }
        return nil
 }
 
index b53924f..9278e35 100644 (file)
@@ -500,15 +500,15 @@ func (sw *Switch) listenerRoutine(l Listener) {
                        break
                }
 
-               // ignore connection if we already have enough
-               maxPeers := sw.config.MaxNumPeers
-               if maxPeers <= sw.peers.Size() {
+               // disconnect if we alrady have 2 * MaxNumPeers, we do this because we wanna address book get exchanged even if
+               // the connect is full. The pex will disconnect the peer after address exchange, the max connected peer won't
+               // be double of MaxNumPeers
+               if sw.config.MaxNumPeers*2 <= sw.peers.Size() {
                        // close inConn
                        inConn.Close()
                        log.WithFields(log.Fields{
                                "address":  inConn.RemoteAddr().String(),
                                "numPeers": sw.peers.Size(),
-                               "max":      maxPeers,
                        }).Info("Ignoring inbound connection: already have enough peers")
                        continue
                }