@cryptotaxi247 / kubo / commits / 5171d3df6

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.

Juan Batiz-Benet committed Feb 11, 2015 at 09:10 UTC 5171d3df6b04d70f2c587fa5c95327083e11de72
1 file changed +8 -8
p2p/net/conn/dial.go
+8 -8
@@ -31,13 +31,6 @@ func (d *Dialer) Dial(ctx context.Context, raddr ma.Multiaddr, remote peer.ID) (
31 logdial["encrypted"] = (d.PrivateKey != nil) // log wether this will be an encrypted dial or not.
32 defer log.EventBegin(ctx, "connDial", logdial).Done()
33
34 - maconn, err := d.rawConnDial(ctx, raddr, remote)
35 - if err != nil {
36 - logdial["dial"] = "failure"
37 - logdial["error"] = err
38 - return nil, err
39 - }
40 -
34 var connOut Conn
35 var errOut error
36 done := make(chan struct{})
@@ -51,8 +44,15 @@ func (d *Dialer) Dial(ctx context.Context, raddr ma.Multiaddr, remote peer.ID) (
44 }
45 }()
46
47 + maconn, err := d.rawConnDial(ctx, raddr, remote)
48 + if err != nil {
49 + errOut = err
50 + return
51 + }
52 +
53 c, err := newSingleConn(ctx, d.LocalPeer, remote, maconn)
54 if err != nil {
55 + maconn.Close()
56 errOut = err
57 return
58 }
@@ -75,8 +75,8 @@ func (d *Dialer) Dial(ctx context.Context, raddr ma.Multiaddr, remote peer.ID) (
75
76 select {
77 case <-ctx.Done():
78 - maconn.Close()
78 logdial["error"] = ctx.Err()
79 + logdial["dial"] = "failure"
80 return nil, ctx.Err()
81 case <-done:
82 // whew, finished.