From 5171d3df6b04d70f2c587fa5c95327083e11de72 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Wed, 11 Feb 2015 09:10:42 -0800 Subject: [PATCH] p2p/net/conn: respect context on dialing We were half-way with this. there's no way for net.Dialers to respect contexts, so we have to let the dial finish in the background. --- p2p/net/conn/dial.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/p2p/net/conn/dial.go b/p2p/net/conn/dial.go index 7e8fe1841..073bd5c62 100644 --- a/p2p/net/conn/dial.go +++ b/p2p/net/conn/dial.go @@ -31,13 +31,6 @@ func (d *Dialer) Dial(ctx context.Context, raddr ma.Multiaddr, remote peer.ID) ( logdial["encrypted"] = (d.PrivateKey != nil) // log wether this will be an encrypted dial or not. defer log.EventBegin(ctx, "connDial", logdial).Done() - maconn, err := d.rawConnDial(ctx, raddr, remote) - if err != nil { - logdial["dial"] = "failure" - logdial["error"] = err - return nil, err - } - var connOut Conn var errOut error done := make(chan struct{}) @@ -51,8 +44,15 @@ func (d *Dialer) Dial(ctx context.Context, raddr ma.Multiaddr, remote peer.ID) ( } }() + maconn, err := d.rawConnDial(ctx, raddr, remote) + if err != nil { + errOut = err + return + } + c, err := newSingleConn(ctx, d.LocalPeer, remote, maconn) if err != nil { + maconn.Close() errOut = err return } @@ -75,8 +75,8 @@ func (d *Dialer) Dial(ctx context.Context, raddr ma.Multiaddr, remote peer.ID) ( select { case <-ctx.Done(): - maconn.Close() logdial["error"] = ctx.Err() + logdial["dial"] = "failure" return nil, ctx.Err() case <-done: // whew, finished.