Skip to content

Commit

Permalink
Added max peers setting
Browse files Browse the repository at this point in the history
  • Loading branch information
meyer9 committed May 14, 2019
1 parent 43d41a4 commit 2ca244b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
8 changes: 5 additions & 3 deletions beacon/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os/signal"
"time"

"github.com/libp2p/go-libp2p-crypto"
"github.com/mitchellh/go-homedir"
crypto "github.com/libp2p/go-libp2p-crypto"
homedir "github.com/mitchellh/go-homedir"
ma "github.com/multiformats/go-multiaddr"
"github.com/phoreproject/synapse/beacon"
"github.com/phoreproject/synapse/beacon/config"
Expand All @@ -33,6 +33,7 @@ type Config struct {
HeartBeatInterval time.Duration
TimeOutInterval time.Duration
DiscoveryOptions p2p.DiscoveryOptions
MaxPeers int
}

// NewConfig creates a default Config
Expand All @@ -50,6 +51,7 @@ func NewConfig() Config {
HeartBeatInterval: 8 * time.Second,
TimeOutInterval: 16 * time.Second,
DiscoveryOptions: p2p.NewDiscoveryOptions(),
MaxPeers: 16,
}
}

Expand Down Expand Up @@ -147,7 +149,7 @@ func (app *BeaconApp) loadP2P() error {
panic(err)
}

hostNode, err := p2p.NewHostNode(addr, pub, priv, app.config.DiscoveryOptions, app.config.TimeOutInterval)
hostNode, err := p2p.NewHostNode(addr, pub, priv, app.config.DiscoveryOptions, app.config.TimeOutInterval, app.config.MaxPeers)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion explorer/explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (ex *Explorer) loadP2P() error {
panic(err)
}

hostNode, err := p2p.NewHostNode(addr, pub, priv, ex.config.DiscoveryOptions, 16*time.Second)
hostNode, err := p2p.NewHostNode(addr, pub, priv, ex.config.DiscoveryOptions, 16*time.Second, 16)
if err != nil {
panic(err)
}
Expand Down
14 changes: 10 additions & 4 deletions p2p/hostnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/golang/protobuf/proto"
"github.com/libp2p/go-libp2p"
libp2p "github.com/libp2p/go-libp2p"
crypto "github.com/libp2p/go-libp2p-crypto"
host "github.com/libp2p/go-libp2p-host"
inet "github.com/libp2p/go-libp2p-net"
Expand All @@ -17,7 +17,7 @@ import (
ps "github.com/libp2p/go-libp2p-peerstore"
protocol "github.com/libp2p/go-libp2p-protocol"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/multiformats/go-multiaddr"
multiaddr "github.com/multiformats/go-multiaddr"
"github.com/phoreproject/synapse/pb"
logger "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -47,6 +47,7 @@ type HostNode struct {
gossipSub *pubsub.PubSub
ctx context.Context
cancel context.CancelFunc
maxPeers int

timeoutInterval time.Duration

Expand All @@ -69,7 +70,7 @@ type HostNode struct {
var protocolID = protocol.ID("/grpc/phore/0.0.1")

// NewHostNode creates a host node
func NewHostNode(listenAddress multiaddr.Multiaddr, publicKey crypto.PubKey, privateKey crypto.PrivKey, options DiscoveryOptions, timeoutInterval time.Duration) (*HostNode, error) {
func NewHostNode(listenAddress multiaddr.Multiaddr, publicKey crypto.PubKey, privateKey crypto.PrivKey, options DiscoveryOptions, timeoutInterval time.Duration, maxPeers int) (*HostNode, error) {
ctx, cancel := context.WithCancel(context.Background())
h, err := libp2p.New(
ctx,
Expand Down Expand Up @@ -111,6 +112,7 @@ func NewHostNode(listenAddress multiaddr.Multiaddr, publicKey crypto.PubKey, pri
currentID: 0,
timeoutInterval: timeoutInterval,
peerListLock: new(sync.Mutex),
maxPeers: maxPeers,
}

discovery := NewDiscovery(ctx, hostNode, options)
Expand Down Expand Up @@ -200,7 +202,7 @@ func (node *HostNode) Connect(peerInfo peerstore.PeerInfo) (*Peer, error) {
return nil, errors.New("cannot connect to self")
}

if(node.IsPeerConnected(peerInfo)) {
if node.IsPeerConnected(peerInfo) {
return nil, nil
}

Expand Down Expand Up @@ -379,6 +381,10 @@ func (node *HostNode) FindPeerByID(id peer.ID) (*Peer, bool) {

// PeerDiscovered is run when peers are discovered.
func (node *HostNode) PeerDiscovered(pi peerstore.PeerInfo) {
if node.maxPeers <= len(node.peerList) {
return
}

_, err := node.Connect(pi)
if err != nil {
logger.WithField("err", err).Debug("could not connect to peer")
Expand Down

0 comments on commit 2ca244b

Please sign in to comment.