mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-07 22:05:18 +00:00
limit number of allowed connections per ip
This commit is contained in:
@@ -35,8 +35,6 @@ func NewPeerSet() *PeerSet {
|
||||
}
|
||||
|
||||
// Returns false if peer with key (uuid) is already in set.
|
||||
// TODO: we may want to do other things like restrict how many peers
|
||||
// we're willing to connect to behind a single IP
|
||||
func (ps *PeerSet) Add(peer *Peer) bool {
|
||||
ps.mtx.Lock()
|
||||
defer ps.mtx.Unlock()
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"testing"
|
||||
|
||||
"github.com/tendermint/tendermint/Godeps/_workspace/src/code.google.com/p/go-uuid/uuid"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
)
|
||||
|
||||
// Returns an empty dummy peer
|
||||
|
||||
@@ -209,6 +209,9 @@ func (pexR *PEXReactor) ensurePeers() {
|
||||
}
|
||||
}(item.(*NetAddress))
|
||||
}
|
||||
|
||||
// TODO: if no addresses to dial, we should send a pexRequest to a random peer
|
||||
// so we can get more peers
|
||||
}
|
||||
|
||||
// implements events.Eventable
|
||||
|
||||
@@ -53,10 +53,12 @@ type Switch struct {
|
||||
|
||||
var (
|
||||
ErrSwitchDuplicatePeer = errors.New("Duplicate peer")
|
||||
ErrSwitchMaxPeersPerIP = errors.New("IP has too many peers")
|
||||
)
|
||||
|
||||
const (
|
||||
peerDialTimeoutSeconds = 3
|
||||
maxPeersPerIP = 3
|
||||
)
|
||||
|
||||
func NewSwitch() *Switch {
|
||||
@@ -186,10 +188,21 @@ func (sw *Switch) AddPeerWithConnection(conn net.Conn, outbound bool) (*Peer, er
|
||||
}
|
||||
peer := newPeer(conn, peerNodeInfo, outbound, sw.reactorsByCh, sw.chDescs, sw.StopPeerForError)
|
||||
|
||||
// restrict the number of peers we're willing to connect to behind a single IP
|
||||
var numPeersOnThisIP int
|
||||
peers := sw.Peers().List()
|
||||
for _, p := range peers {
|
||||
if p.Host == peerNodeInfo.Host {
|
||||
numPeersOnThisIP += 1
|
||||
}
|
||||
}
|
||||
if numPeersOnThisIP == maxPeersPerIP {
|
||||
log.Info("Ignoring peer as we have the max allowed for that IP", "IP", peerNodeInfo.Host, "peer", peer, "max", maxPeersPerIP)
|
||||
return nil, ErrSwitchMaxPeersPerIP
|
||||
}
|
||||
|
||||
// Add the peer to .peers
|
||||
if sw.peers.Add(peer) {
|
||||
log.Info("Added peer", "peer", peer)
|
||||
} else {
|
||||
if !sw.peers.Add(peer) {
|
||||
log.Info("Ignoring duplicate peer", "peer", peer)
|
||||
peer.stop() // will also close conn
|
||||
return nil, ErrSwitchDuplicatePeer
|
||||
@@ -198,6 +211,8 @@ func (sw *Switch) AddPeerWithConnection(conn net.Conn, outbound bool) (*Peer, er
|
||||
if atomic.LoadUint32(&sw.running) == 1 {
|
||||
sw.startInitPeer(peer)
|
||||
}
|
||||
|
||||
log.Info("Added peer", "peer", peer)
|
||||
return peer, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user