1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-30 01:52:26 +08:00

respect don contexteone

This commit is contained in:
Juan Batiz-Benet
2014-12-24 03:24:28 -08:00
parent f8c523fc3b
commit ccf6f79aa0
5 changed files with 49 additions and 24 deletions

View File

@ -50,32 +50,44 @@ func (d *Dialer) Dial(ctx context.Context, raddr ma.Multiaddr, remote peer.ID) (
return nil, err
}
var connOut Conn
var errOut error
done := make(chan struct{})
// do it async to ensure we respect don contexteone
go func() {
defer func() { done <- struct{}{} }()
c, err := newSingleConn(ctx, d.LocalPeer, remote, maconn)
if err != nil {
errOut = err
return
}
if d.PrivateKey == nil {
log.Warning("dialer %s dialing INSECURELY %s at %s!", d, remote, raddr)
connOut = c
return
}
c2, err := newSecureConn(ctx, d.PrivateKey, c)
if err != nil {
errOut = err
c.Close()
return
}
connOut = c2
}()
select {
case <-ctx.Done():
maconn.Close()
return nil, ctx.Err()
default:
case <-done:
// whew, finished.
}
c, err := newSingleConn(ctx, d.LocalPeer, remote, maconn)
if err != nil {
return nil, err
}
if d.PrivateKey == nil {
log.Warning("dialer %s dialing INSECURELY %s at %s!", d, remote, raddr)
return c, nil
}
select {
case <-ctx.Done():
c.Close()
return nil, ctx.Err()
default:
}
// return c, nil
return newSecureConn(ctx, d.PrivateKey, c)
return connOut, errOut
}
// MultiaddrProtocolsMatch returns whether two multiaddrs match in protocol stacks.

View File

@ -148,7 +148,19 @@ func (n *network) DialPeer(ctx context.Context, p peer.ID) error {
}
// identify the connection before returning.
n.ids.IdentifyConn((*conn_)(sc))
done := make(chan struct{})
go func() {
n.ids.IdentifyConn((*conn_)(sc))
close(done)
}()
// respect don contexteone
select {
case <-done:
case <-ctx.Done():
return ctx.Err()
}
log.Debugf("network for %s finished dialing %s", n.local, p)
return nil
}

View File

@ -31,7 +31,7 @@ func (dht *IpfsDHT) handleNewMessage(s inet.Stream) {
// receive msg
pmes := new(pb.Message)
if err := r.ReadMsg(pmes); err != nil {
log.Error("Error unmarshaling data")
log.Errorf("Error unmarshaling data: %s", err)
return
}

View File

@ -265,7 +265,8 @@ func TestBootstrap(t *testing.T) {
}
t.Logf("bootstrapping them so they find each other", nDHTs)
bootstrap(t, ctx, dhts)
ctxT, _ := context.WithTimeout(ctx, 5*time.Second)
bootstrap(t, ctxT, dhts)
// the routing tables should be full now. let's inspect them.
t.Logf("checking routing table of %d", nDHTs)

View File

@ -73,7 +73,7 @@ func TestGetFailures(t *testing.T) {
})
// This one should fail with NotFound
ctx2, _ := context.WithTimeout(context.Background(), time.Second)
ctx2, _ := context.WithTimeout(context.Background(), 3*time.Second)
_, err = d.GetValue(ctx2, u.Key("test"))
if err != nil {
if err != routing.ErrNotFound {