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

connect to max 3 bootnodes at startup (#636)

parent 531117ed
...@@ -238,13 +238,14 @@ func (k *Kad) Start(ctx context.Context) error { ...@@ -238,13 +238,14 @@ 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
for _, addr := range k.bootnodes {
wg.Add(1)
go func(a ma.Multiaddr) {
defer wg.Done()
var count int var count int
if _, err := p2p.Discover(ctx, a, func(addr ma.Multiaddr) (stop bool, err error) {
for _, addr := range k.bootnodes {
if count == 3 {
return
}
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 {
...@@ -257,15 +258,13 @@ func (k *Kad) connectBootnodes(ctx context.Context) { ...@@ -257,15 +258,13 @@ func (k *Kad) connectBootnodes(ctx context.Context) {
k.logger.Tracef("connected to bootnode %s", addr) k.logger.Tracef("connected to bootnode %s", addr)
count++ count++
// connect to max 3 bootnodes // connect to max 3 bootnodes
return count > 3, nil return count == 3, nil
}); err != nil { }); err != nil {
k.logger.Debugf("discover fail %s: %v", a, err) k.logger.Debugf("discover fail %s: %v", addr, err)
k.logger.Warningf("discover to bootnode %s", a) k.logger.Warningf("discover to bootnode %s", addr)
return return
} }
}(addr)
} }
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