Unverified Commit 8f17218a authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #96 from libp2p/fix/finish-dial-fast

dialer: handle dial cancel and/or completion before trying new addresses
parents 9b27a665 9ed362ba
......@@ -358,14 +358,34 @@ func (s *Swarm) dialAddrs(ctx context.Context, p peer.ID, remoteAddrs <-chan ma.
defer s.limiter.clearAllPeerDials(p)
var active int
for {
for remoteAddrs != nil || active > 0 {
// Check for context cancellations and/or responses first.
select {
case <-ctx.Done():
if exitErr == defaultDialFail {
exitErr = ctx.Err()
}
return nil, exitErr
case resp := <-respch:
active--
if resp.Err != nil {
log.Infof("got error on dial to %s: %s", resp.Addr, resp.Err)
// Errors are normal, lots of dials will fail
exitErr = resp.Err
} else if resp.Conn != nil {
return resp.Conn, nil
}
// We got a result, try again from the top.
continue
default:
}
// Now, attempt to dial.
select {
case addr, ok := <-remoteAddrs:
if !ok {
remoteAddrs = nil
if active == 0 {
return nil, exitErr
}
continue
}
......@@ -382,15 +402,12 @@ func (s *Swarm) dialAddrs(ctx context.Context, p peer.ID, remoteAddrs <-chan ma.
log.Infof("got error on dial to %s: %s", resp.Addr, resp.Err)
// Errors are normal, lots of dials will fail
exitErr = resp.Err
if remoteAddrs == nil && active == 0 {
return nil, exitErr
}
} else if resp.Conn != nil {
return resp.Conn, nil
}
}
}
return nil, exitErr
}
// limitedDial will start a dial to the given peer when
......
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