@@ -373,7 +373,6 @@ func (s *Swarm) dialAddrs(ctx context.Context, d *conn.Dialer, p peer.ID, remote
373
373
ctx , cancel := context .WithCancel (ctx )
374
374
defer cancel () // cancel work when we exit func
375
375
376
- foundConn := make (chan struct {})
377
376
conns := make (chan conn.Conn )
378
377
errs := make (chan error , len (remoteAddrs ))
379
378
@@ -397,7 +396,7 @@ func (s *Swarm) dialAddrs(ctx context.Context, d *conn.Dialer, p peer.ID, remote
397
396
398
397
// check parent still wants our results
399
398
select {
400
- case <- foundConn :
399
+ case <- ctx . Done () :
401
400
if connC != nil {
402
401
connC .Close ()
403
402
}
@@ -413,50 +412,45 @@ func (s *Swarm) dialAddrs(ctx context.Context, d *conn.Dialer, p peer.ID, remote
413
412
// permute addrs so we try different sets first each time.
414
413
for _ , addr := range remoteAddrs {
415
414
select {
416
- case <- foundConn : // if one of them succeeded already
417
- return
418
415
case <- ctx .Done (): // our context was cancelled
419
416
return
420
417
case limiter <- struct {}{}:
421
- // continue
418
+ // take the token, move on
422
419
}
423
420
424
421
// returns whatever ratelimiting is acceptable for workerAddr.
425
422
// may not rate limit at all.
426
423
rl := s .addrDialRateLimit (addr )
427
424
select {
428
- case <- foundConn : // if one of them succeeded already
429
- return
430
425
case <- ctx .Done (): // our context was cancelled
431
426
return
432
427
case rl <- struct {}{}:
433
- // continue
428
+ // take the token, move on
434
429
}
435
430
436
431
// we have to do the waiting concurrently because there are addrs
437
432
// that SHOULD NOT be rate limited (utp), nor blocked by other
438
433
// rate limited addrs (tcp).
439
434
go func (rlc <- chan struct {}, a ma.Multiaddr ) {
440
- defer func () {
441
- <- limiter
442
- <- rlc
443
- }()
444
435
dialSingleAddr (a )
436
+ <- limiter
437
+ <- rlc
445
438
}(rl , addr )
446
-
447
439
}
448
440
}()
449
441
450
- // wair fot the results.
442
+ // wair for the results.
451
443
exitErr := fmt .Errorf ("failed to dial %s" , p )
452
- for i := 0 ; i < len ( remoteAddrs ); i ++ {
444
+ for range remoteAddrs {
453
445
select {
454
446
case exitErr = <- errs : //
455
447
log .Debug ("dial error: " , exitErr )
456
448
case connC := <- conns :
457
449
// take the first + return asap
458
- close (foundConn )
459
450
return connC , nil
451
+ case <- ctx .Done ():
452
+ // break out and return error
453
+ break
460
454
}
461
455
}
462
456
return nil , exitErr
0 commit comments