Commit 6c981a3a authored by Petar Radovic's avatar Petar Radovic Committed by GitHub

connect to max 3 bootnodes at startup (#636)

parent 531117ed
...@@ -238,34 +238,33 @@ func (k *Kad) Start(ctx context.Context) error { ...@@ -238,34 +238,33 @@ func (k *Kad) Start(ctx context.Context) error {
} }
func (k *Kad) connectBootnodes(ctx context.Context) { func (k *Kad) connectBootnodes(ctx context.Context) {
var wg sync.WaitGroup var count int
for _, addr := range k.bootnodes { for _, addr := range k.bootnodes {
wg.Add(1) if count == 3 {
go func(a ma.Multiaddr) { return
defer wg.Done() }
var count int
if _, err := p2p.Discover(ctx, a, func(addr ma.Multiaddr) (stop bool, err error) { if _, err := p2p.Discover(ctx, addr, func(addr ma.Multiaddr) (stop bool, err error) {
k.logger.Tracef("connecting to bootnode %s", addr) k.logger.Tracef("connecting to bootnode %s", addr)
_, err = k.p2p.ConnectNotify(ctx, addr) _, err = k.p2p.ConnectNotify(ctx, addr)
if err != nil { if err != nil {
if !errors.Is(err, p2p.ErrAlreadyConnected) { if !errors.Is(err, p2p.ErrAlreadyConnected) {
k.logger.Debugf("connect fail %s: %v", addr, err) k.logger.Debugf("connect fail %s: %v", addr, err)
k.logger.Warningf("connect to bootnode %s", addr) k.logger.Warningf("connect to bootnode %s", addr)
}
return false, nil
} }
k.logger.Tracef("connected to bootnode %s", addr) return false, nil
count++
// connect to max 3 bootnodes
return count > 3, nil
}); err != nil {
k.logger.Debugf("discover fail %s: %v", a, err)
k.logger.Warningf("discover to bootnode %s", a)
return
} }
}(addr) k.logger.Tracef("connected to bootnode %s", addr)
count++
// connect to max 3 bootnodes
return count == 3, nil
}); err != nil {
k.logger.Debugf("discover fail %s: %v", addr, err)
k.logger.Warningf("discover to bootnode %s", addr)
return
}
} }
wg.Wait()
} }
// binSaturated indicates whether a certain bin is saturated or not. // binSaturated indicates whether a certain bin is saturated or not.
......
...@@ -721,7 +721,7 @@ func TestStart(t *testing.T) { ...@@ -721,7 +721,7 @@ func TestStart(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
waitCounter(t, &conns, 5) waitCounter(t, &conns, 3)
waitCounter(t, &failedConns, 0) waitCounter(t, &failedConns, 0)
}) })
} }
......
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