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

ratelimiter: fixing rate limiter use

Use of the ratelimiter should be conscious of the ratelimiter's
potential closing. any loops that add work to ratelimiter
should (a) only do so if the rate limiter is not closed,
or (b) prevent limiter while work is added
(i.e. use limiter.Go(addWorkHere))
parent 381e99a3
...@@ -385,20 +385,23 @@ func (s *Swarm) dialAddrs(ctx context.Context, d *conn.Dialer, p peer.ID, remote ...@@ -385,20 +385,23 @@ 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)
limiter.Go(func(worker process.Process) {
// permute addrs so we try different sets first each time. // permute addrs so we try different sets first each time.
for _, i := range rand.Perm(len(remoteAddrs)) { 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: case <-worker.Closing(): // our context was cancelled
break
default:
}
workerAddr := remoteAddrs[i] // shadow variable to avoid race
limiter.LimitedGo(func(worker process.Process) {
dialSingleAddr(workerAddr)
})
} }
})
workerAddr := remoteAddrs[i] // shadow variable to avoid race
limiter.Go(func(worker process.Process) {
dialSingleAddr(workerAddr)
})
}
}() }()
// wair fot the results. // wair fot the results.
......
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