Commit 8b419c5d authored by Juan Batiz-Benet's avatar Juan Batiz-Benet
Browse files

p2p/net/swarm: permute addrs in rate limited dials

so that we dont get stuck dialing bad addrs first always.
parent 51709847
...@@ -3,6 +3,7 @@ package swarm ...@@ -3,6 +3,7 @@ package swarm
import ( import (
"errors" "errors"
"fmt" "fmt"
"math/rand"
"net" "net"
"sync" "sync"
"time" "time"
...@@ -384,13 +385,16 @@ func (s *Swarm) dialAddrs(ctx context.Context, d *conn.Dialer, p peer.ID, remote ...@@ -384,13 +385,16 @@ func (s *Swarm) dialAddrs(ctx context.Context, d *conn.Dialer, p peer.ID, remote
go func() { go func() {
// rate limiting just in case. at most 10 addrs at once. // rate limiting just in case. at most 10 addrs at once.
limiter := ratelimit.NewRateLimiter(procctx.WithContext(ctx), 10) limiter := ratelimit.NewRateLimiter(procctx.WithContext(ctx), 10)
for _, addr := range remoteAddrs {
// permute addrs so we try different sets first each time.
for _, i := range rand.Perm(len(remoteAddrs)) {
select { select {
case <-foundConn: // if one of them succeeded already case <-foundConn: // if one of them succeeded already
break break
default: default:
} }
workerAddr := addr // shadow variable to avoid race
workerAddr := remoteAddrs[i] // shadow variable to avoid race
limiter.Go(func(worker process.Process) { limiter.Go(func(worker process.Process) {
dialSingleAddr(workerAddr) dialSingleAddr(workerAddr)
}) })
......
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