}
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
}
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
}