respect don contexteone
Juan Batiz-Benet committed
Dec 24, 2014 at 03:24 UTC
ccf6f79aa09add2d924e431d37a13188050ab3b1
5 files changed
+47
-22
net/conn/dial.go
+30
-18
@@ -50,32 +50,44 @@ func (d *Dialer) Dial(ctx context.Context, raddr ma.Multiaddr, remote peer.ID) (
50
return nil, err
51
}
52
53
- select {
54
- case <-ctx.Done():
55
- maconn.Close()
56
- return nil, ctx.Err()
57
- default:
58
- }
53
+ var connOut Conn
54
+ var errOut error
55
+ done := make(chan struct{})
56
+
57
+ // do it async to ensure we respect don contexteone
58
+ go func() {
59
+ defer func() { done <- struct{}{} }()
60
+
61
+ c, err := newSingleConn(ctx, d.LocalPeer, remote, maconn)
62
+ if err != nil {
63
+ errOut = err
64
+ return
65
+ }
66
60
- c, err := newSingleConn(ctx, d.LocalPeer, remote, maconn)
61
- if err != nil {
62
- return nil, err
63
- }
67
+ if d.PrivateKey == nil {
68
+ log.Warning("dialer %s dialing INSECURELY %s at %s!", d, remote, raddr)
69
+ connOut = c
70
+ return
71
+ }
72
+ c2, err := newSecureConn(ctx, d.PrivateKey, c)
73
+ if err != nil {
74
+ errOut = err
75
+ c.Close()
76
+ return
77
+ }
78
65
- if d.PrivateKey == nil {
66
- log.Warning("dialer %s dialing INSECURELY %s at %s!", d, remote, raddr)
67
- return c, nil
68
- }
79
+ connOut = c2
80
+ }()
81
82
select {
83
case <-ctx.Done():
72
- c.Close()
84
+ maconn.Close()
85
return nil, ctx.Err()
74
- default:
86
+ case <-done:
87
+ // whew, finished.
88
}
89
77
- // return c, nil
78
- return newSecureConn(ctx, d.PrivateKey, c)
90
+ return connOut, errOut
91
}
92
93
// MultiaddrProtocolsMatch returns whether two multiaddrs match in protocol stacks.
net/net.go
+13
-1
@@ -148,7 +148,19 @@ func (n *network) DialPeer(ctx context.Context, p peer.ID) error {
148
}
149
150
// identify the connection before returning.
151
- n.ids.IdentifyConn((*conn_)(sc))
151
+ done := make(chan struct{})
152
+ go func() {
153
+ n.ids.IdentifyConn((*conn_)(sc))
154
+ close(done)
155
+ }()
156
+
157
+ // respect don contexteone
158
+ select {
159
+ case <-done:
160
+ case <-ctx.Done():
161
+ return ctx.Err()
162
+ }
163
+
164
log.Debugf("network for %s finished dialing %s", n.local, p)
165
return nil
166
}
routing/dht/dht_net.go
+1
-1
@@ -31,7 +31,7 @@ func (dht *IpfsDHT) handleNewMessage(s inet.Stream) {
31
// receive msg
32
pmes := new(pb.Message)
33
if err := r.ReadMsg(pmes); err != nil {
34
- log.Error("Error unmarshaling data")
34
+ log.Errorf("Error unmarshaling data: %s", err)
35
return
36
}
37
routing/dht/dht_test.go
+2
-1
@@ -265,7 +265,8 @@ func TestBootstrap(t *testing.T) {
265
}
266
267
t.Logf("bootstrapping them so they find each other", nDHTs)
268
- bootstrap(t, ctx, dhts)
268
+ ctxT, _ := context.WithTimeout(ctx, 5*time.Second)
269
+ bootstrap(t, ctxT, dhts)
270
271
// the routing tables should be full now. let's inspect them.
272
t.Logf("checking routing table of %d", nDHTs)
routing/dht/ext_test.go
+1
-1
@@ -73,7 +73,7 @@ func TestGetFailures(t *testing.T) {
73
})
74
75
// This one should fail with NotFound
76
- ctx2, _ := context.WithTimeout(context.Background(), time.Second)
76
+ ctx2, _ := context.WithTimeout(context.Background(), 3*time.Second)
77
_, err = d.GetValue(ctx2, u.Key("test"))
78
if err != nil {
79
if err != routing.ErrNotFound {