forked from TrueCloudLab/neoneo-go
network: fix MaxPeers, introduce AttemptConnPeers
Our node didn't respect the MaxPeers setting, fix it with a drop of random connection when this limit is reached (to give a chance for newcomers to communicate), but also introduce AttemptConnPeers setting to tune the number of attempted connections. This also raises the default MaxPeers for testnet/mainnet to 100, because neo-go nodes love making friends.
This commit is contained in:
parent
31954bb20c
commit
7cf9a40468
11 changed files with 57 additions and 15 deletions
|
@ -72,6 +72,7 @@ type (
|
||||||
DialTimeout time.Duration `yaml:"DialTimeout"`
|
DialTimeout time.Duration `yaml:"DialTimeout"`
|
||||||
ProtoTickInterval time.Duration `yaml:"ProtoTickInterval"`
|
ProtoTickInterval time.Duration `yaml:"ProtoTickInterval"`
|
||||||
MaxPeers int `yaml:"MaxPeers"`
|
MaxPeers int `yaml:"MaxPeers"`
|
||||||
|
AttemptConnPeers int `yaml:"AttemptConnPeers"`
|
||||||
MinPeers int `yaml:"MinPeers"`
|
MinPeers int `yaml:"MinPeers"`
|
||||||
Monitoring metrics.PrometheusConfig `yaml:"Monitoring"`
|
Monitoring metrics.PrometheusConfig `yaml:"Monitoring"`
|
||||||
RPC RPCConfig `yaml:"RPC"`
|
RPC RPCConfig `yaml:"RPC"`
|
||||||
|
|
|
@ -46,7 +46,8 @@ ApplicationConfiguration:
|
||||||
Relay: true
|
Relay: true
|
||||||
DialTimeout: 3
|
DialTimeout: 3
|
||||||
ProtoTickInterval: 2
|
ProtoTickInterval: 2
|
||||||
MaxPeers: 50
|
MaxPeers: 100
|
||||||
|
AttemptConnPeers: 20
|
||||||
MinPeers: 5
|
MinPeers: 5
|
||||||
RPC:
|
RPC:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
|
@ -34,7 +34,8 @@ ApplicationConfiguration:
|
||||||
Relay: true
|
Relay: true
|
||||||
DialTimeout: 3
|
DialTimeout: 3
|
||||||
ProtoTickInterval: 2
|
ProtoTickInterval: 2
|
||||||
MaxPeers: 50
|
MaxPeers: 10
|
||||||
|
AttemptConnPeers: 5
|
||||||
MinPeers: 3
|
MinPeers: 3
|
||||||
RPC:
|
RPC:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
|
@ -31,7 +31,8 @@ ApplicationConfiguration:
|
||||||
Relay: true
|
Relay: true
|
||||||
DialTimeout: 3
|
DialTimeout: 3
|
||||||
ProtoTickInterval: 2
|
ProtoTickInterval: 2
|
||||||
MaxPeers: 50
|
MaxPeers: 10
|
||||||
|
AttemptConnPeers: 5
|
||||||
MinPeers: 3
|
MinPeers: 3
|
||||||
RPC:
|
RPC:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
|
@ -31,7 +31,8 @@ ApplicationConfiguration:
|
||||||
Relay: true
|
Relay: true
|
||||||
DialTimeout: 3
|
DialTimeout: 3
|
||||||
ProtoTickInterval: 2
|
ProtoTickInterval: 2
|
||||||
MaxPeers: 50
|
MaxPeers: 10
|
||||||
|
AttemptConnPeers: 5
|
||||||
MinPeers: 3
|
MinPeers: 3
|
||||||
RPC:
|
RPC:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
|
@ -31,7 +31,8 @@ ApplicationConfiguration:
|
||||||
Relay: true
|
Relay: true
|
||||||
DialTimeout: 3
|
DialTimeout: 3
|
||||||
ProtoTickInterval: 2
|
ProtoTickInterval: 2
|
||||||
MaxPeers: 50
|
MaxPeers: 10
|
||||||
|
AttemptConnPeers: 5
|
||||||
MinPeers: 3
|
MinPeers: 3
|
||||||
RPC:
|
RPC:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
|
@ -37,7 +37,8 @@ ApplicationConfiguration:
|
||||||
Relay: true
|
Relay: true
|
||||||
DialTimeout: 3
|
DialTimeout: 3
|
||||||
ProtoTickInterval: 2
|
ProtoTickInterval: 2
|
||||||
MaxPeers: 50
|
MaxPeers: 10
|
||||||
|
AttemptConnPeers: 5
|
||||||
MinPeers: 3
|
MinPeers: 3
|
||||||
RPC:
|
RPC:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
|
@ -46,7 +46,8 @@ ApplicationConfiguration:
|
||||||
Relay: true
|
Relay: true
|
||||||
DialTimeout: 3
|
DialTimeout: 3
|
||||||
ProtoTickInterval: 2
|
ProtoTickInterval: 2
|
||||||
MaxPeers: 50
|
MaxPeers: 100
|
||||||
|
AttemptConnPeers: 20
|
||||||
MinPeers: 5
|
MinPeers: 5
|
||||||
RPC:
|
RPC:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
|
@ -37,6 +37,7 @@ ApplicationConfiguration:
|
||||||
DialTimeout: 3
|
DialTimeout: 3
|
||||||
ProtoTickInterval: 2
|
ProtoTickInterval: 2
|
||||||
MaxPeers: 50
|
MaxPeers: 50
|
||||||
|
AttemptConnPeers: 5
|
||||||
MinPeers: 1
|
MinPeers: 1
|
||||||
RPC:
|
RPC:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
|
@ -18,11 +18,12 @@ import (
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// peer numbers are arbitrary at the moment.
|
// peer numbers are arbitrary at the moment.
|
||||||
defaultMinPeers = 5
|
defaultMinPeers = 5
|
||||||
maxPeers = 20
|
defaultAttemptConnPeers = 20
|
||||||
maxBlockBatch = 200
|
defaultMaxPeers = 100
|
||||||
maxAddrsToSend = 200
|
maxBlockBatch = 200
|
||||||
minPoolCount = 30
|
maxAddrsToSend = 200
|
||||||
|
minPoolCount = 30
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -30,6 +31,7 @@ var (
|
||||||
errIdenticalID = errors.New("identical node id")
|
errIdenticalID = errors.New("identical node id")
|
||||||
errInvalidHandshake = errors.New("invalid handshake")
|
errInvalidHandshake = errors.New("invalid handshake")
|
||||||
errInvalidNetwork = errors.New("invalid network")
|
errInvalidNetwork = errors.New("invalid network")
|
||||||
|
errMaxPeers = errors.New("max peers reached")
|
||||||
errServerShutdown = errors.New("server shutdown")
|
errServerShutdown = errors.New("server shutdown")
|
||||||
errInvalidInvType = errors.New("invalid inventory type")
|
errInvalidInvType = errors.New("invalid inventory type")
|
||||||
)
|
)
|
||||||
|
@ -86,6 +88,22 @@ func NewServer(config ServerConfig, chain core.Blockchainer) *Server {
|
||||||
s.MinPeers = defaultMinPeers
|
s.MinPeers = defaultMinPeers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if s.MaxPeers <= 0 {
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"MaxPeers configured": s.MaxPeers,
|
||||||
|
"MaxPeers actual": defaultMaxPeers,
|
||||||
|
}).Info("bad MaxPeers configured, using the default value")
|
||||||
|
s.MaxPeers = defaultMaxPeers
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.AttemptConnPeers <= 0 {
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"AttemptConnPeers configured": s.AttemptConnPeers,
|
||||||
|
"AttemptConnPeers actual": defaultAttemptConnPeers,
|
||||||
|
}).Info("bad AttemptConnPeers configured, using the default value")
|
||||||
|
s.AttemptConnPeers = defaultAttemptConnPeers
|
||||||
|
}
|
||||||
|
|
||||||
s.transport = NewTCPTransport(s, fmt.Sprintf(":%d", config.ListenTCP))
|
s.transport = NewTCPTransport(s, fmt.Sprintf(":%d", config.ListenTCP))
|
||||||
s.discovery = NewDefaultDiscovery(
|
s.discovery = NewDefaultDiscovery(
|
||||||
s.DialTimeout,
|
s.DialTimeout,
|
||||||
|
@ -137,9 +155,8 @@ func (s *Server) BadPeers() []string {
|
||||||
|
|
||||||
func (s *Server) run() {
|
func (s *Server) run() {
|
||||||
for {
|
for {
|
||||||
c := s.PeerCount()
|
if s.PeerCount() < s.MinPeers {
|
||||||
if c < s.ServerConfig.MinPeers {
|
s.discovery.RequestRemote(s.AttemptConnPeers)
|
||||||
s.discovery.RequestRemote(maxPeers - c)
|
|
||||||
}
|
}
|
||||||
if s.discovery.PoolCount() < minPoolCount {
|
if s.discovery.PoolCount() < minPoolCount {
|
||||||
select {
|
select {
|
||||||
|
@ -170,6 +187,16 @@ func (s *Server) run() {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"addr": p.RemoteAddr(),
|
"addr": p.RemoteAddr(),
|
||||||
}).Info("new peer connected")
|
}).Info("new peer connected")
|
||||||
|
peerCount := s.PeerCount()
|
||||||
|
if peerCount > s.MaxPeers {
|
||||||
|
s.lock.RLock()
|
||||||
|
// Pick a random peer and drop connection to it.
|
||||||
|
for peer := range s.peers {
|
||||||
|
peer.Disconnect(errMaxPeers)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
s.lock.RUnlock()
|
||||||
|
}
|
||||||
updatePeersConnectedMetric(s.PeerCount())
|
updatePeersConnectedMetric(s.PeerCount())
|
||||||
|
|
||||||
case drop := <-s.unregister:
|
case drop := <-s.unregister:
|
||||||
|
|
|
@ -15,6 +15,11 @@ type (
|
||||||
// connect with some new ones.
|
// connect with some new ones.
|
||||||
MinPeers int
|
MinPeers int
|
||||||
|
|
||||||
|
// AttemptConnPeers it the number of connection to try to
|
||||||
|
// establish when the connection count drops below the MinPeers
|
||||||
|
// value.
|
||||||
|
AttemptConnPeers int
|
||||||
|
|
||||||
// MaxPeers it the maximum numbers of peers that can
|
// MaxPeers it the maximum numbers of peers that can
|
||||||
// be connected to the server.
|
// be connected to the server.
|
||||||
MaxPeers int
|
MaxPeers int
|
||||||
|
@ -64,6 +69,7 @@ func NewServerConfig(cfg config.Config) ServerConfig {
|
||||||
DialTimeout: appConfig.DialTimeout * time.Second,
|
DialTimeout: appConfig.DialTimeout * time.Second,
|
||||||
ProtoTickInterval: appConfig.ProtoTickInterval * time.Second,
|
ProtoTickInterval: appConfig.ProtoTickInterval * time.Second,
|
||||||
MaxPeers: appConfig.MaxPeers,
|
MaxPeers: appConfig.MaxPeers,
|
||||||
|
AttemptConnPeers: appConfig.AttemptConnPeers,
|
||||||
MinPeers: appConfig.MinPeers,
|
MinPeers: appConfig.MinPeers,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue