Commit 24c9ebcb authored by acud's avatar acud Committed by GitHub

kademlia: limit bootnode connection attempts (#1068)

parent c7f8c08e
......@@ -25,6 +25,7 @@ import (
const (
nnLowWatermark = 2 // the number of peers in consecutive deepest bins that constitute as nearest neighbours
maxConnAttempts = 3 // when there is maxConnAttempts failed connect calls for a given peer it is considered non-connectable
maxBootnodeAttempts = 3 // how many attempts to dial to bootnodes before giving up
)
var (
......@@ -218,6 +219,7 @@ func (k *Kad) manage() {
}
if k.connectedPeers.Length() == 0 {
k.logger.Debug("kademlia has no connected peers, trying bootnodes")
k.connectBootnodes(ctx)
}
......@@ -238,21 +240,28 @@ func (k *Kad) Start(ctx context.Context) error {
}
func (k *Kad) connectBootnodes(ctx context.Context) {
var count int
var attempts, connected int
var totalAttempts = maxBootnodeAttempts * len(k.bootnodes)
for _, addr := range k.bootnodes {
if count >= 3 {
if attempts >= totalAttempts || connected >= 3 {
return
}
if _, err := p2p.Discover(ctx, addr, func(addr ma.Multiaddr) (stop bool, err error) {
k.logger.Tracef("connecting to bootnode %s", addr)
if attempts >= maxBootnodeAttempts {
return true, nil
}
bzzAddress, err := k.p2p.Connect(ctx, addr)
attempts++
if err != nil {
if !errors.Is(err, p2p.ErrAlreadyConnected) {
k.logger.Debugf("connect fail %s: %v", addr, err)
k.logger.Warningf("connect to bootnode %s", addr)
return false, err
}
k.logger.Debugf("connect to bootnode fail: %v", err)
return false, nil
}
......@@ -260,9 +269,9 @@ func (k *Kad) connectBootnodes(ctx context.Context) {
return false, err
}
k.logger.Tracef("connected to bootnode %s", addr)
count++
connected++
// connect to max 3 bootnodes
return count >= 3, nil
return connected >= 3, nil
}); err != nil {
k.logger.Debugf("discover fail %s: %v", addr, err)
k.logger.Warningf("discover to bootnode %s", addr)
......
......@@ -50,7 +50,7 @@ var (
// ErrInvalidSyn is returned if observable address in ack is not a valid..
ErrInvalidSyn = errors.New("invalid syn")
// ErrWelcomeMessageLength is return if the welcome message is longer than the maximum length
// ErrWelcomeMessageLength is returned if the welcome message is longer than the maximum length
ErrWelcomeMessageLength = fmt.Errorf("handshake welcome message longer than maximum of %d characters", MaxWelcomeMessageLength)
)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment