Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b610b8f

Browse files
committedOct 13, 2015
a little more cleanup of dialAddrs
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent 9538115 commit b610b8f

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed
 

‎p2p/net/swarm/swarm_dial.go

+10-16
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,6 @@ func (s *Swarm) dialAddrs(ctx context.Context, d *conn.Dialer, p peer.ID, remote
373373
ctx, cancel := context.WithCancel(ctx)
374374
defer cancel() // cancel work when we exit func
375375

376-
foundConn := make(chan struct{})
377376
conns := make(chan conn.Conn)
378377
errs := make(chan error, len(remoteAddrs))
379378

@@ -397,7 +396,7 @@ func (s *Swarm) dialAddrs(ctx context.Context, d *conn.Dialer, p peer.ID, remote
397396

398397
// check parent still wants our results
399398
select {
400-
case <-foundConn:
399+
case <-ctx.Done():
401400
if connC != nil {
402401
connC.Close()
403402
}
@@ -413,50 +412,45 @@ func (s *Swarm) dialAddrs(ctx context.Context, d *conn.Dialer, p peer.ID, remote
413412
// permute addrs so we try different sets first each time.
414413
for _, addr := range remoteAddrs {
415414
select {
416-
case <-foundConn: // if one of them succeeded already
417-
return
418415
case <-ctx.Done(): // our context was cancelled
419416
return
420417
case limiter <- struct{}{}:
421-
// continue
418+
// take the token, move on
422419
}
423420

424421
// returns whatever ratelimiting is acceptable for workerAddr.
425422
// may not rate limit at all.
426423
rl := s.addrDialRateLimit(addr)
427424
select {
428-
case <-foundConn: // if one of them succeeded already
429-
return
430425
case <-ctx.Done(): // our context was cancelled
431426
return
432427
case rl <- struct{}{}:
433-
// continue
428+
// take the token, move on
434429
}
435430

436431
// we have to do the waiting concurrently because there are addrs
437432
// that SHOULD NOT be rate limited (utp), nor blocked by other
438433
// rate limited addrs (tcp).
439434
go func(rlc <-chan struct{}, a ma.Multiaddr) {
440-
defer func() {
441-
<-limiter
442-
<-rlc
443-
}()
444435
dialSingleAddr(a)
436+
<-limiter
437+
<-rlc
445438
}(rl, addr)
446-
447439
}
448440
}()
449441

450-
// wair fot the results.
442+
// wair for the results.
451443
exitErr := fmt.Errorf("failed to dial %s", p)
452-
for i := 0; i < len(remoteAddrs); i++ {
444+
for range remoteAddrs {
453445
select {
454446
case exitErr = <-errs: //
455447
log.Debug("dial error: ", exitErr)
456448
case connC := <-conns:
457449
// take the first + return asap
458-
close(foundConn)
459450
return connC, nil
451+
case <-ctx.Done():
452+
// break out and return error
453+
break
460454
}
461455
}
462456
return nil, exitErr

0 commit comments

Comments
 (0)
Please sign in to comment.