From c64338a816d52b737b3a79f804a078eda8ad7635 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Sat, 17 Jan 2015 14:54:36 -0800 Subject: [PATCH] diag/net: io must respect timeout ctx MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See the discussion below. A future commit will implement the closer change below, and rebase this one on top. <•jbenet> `n.Diagnostics.GetDiagnostic(time.Second * 20)` is not being respected. should it use a context instead? or is it a timeout because the timeout is sent to other nodes? <•jbenet> oh it's that the io doesnt respect the context so we're stuck waiting for responses. <•jbenet> this is that complex interface point between the world of contexts, and the world of io. ctxutil.Reader/Writer is made for this, but you have to make sure to defer close the stream. (see how dht_net uses it). i'd love to find a safer interface. not sure what it is, but we have to a) respect contexts, and b) allow using standard io.Reader/Writers. Maybe TRTTD <•jbenet> is have ctxutil.Reader/Writer take ReadCloser and WriteClosers and always close them. the user _must_ pass an ioutil. NopCloser to avoid ctxutil closing on you when you dont want it to. <•jbenet> this seems safer to me in the general case. --- diagnostics/diag.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/diagnostics/diag.go b/diagnostics/diag.go index a14f7d239..f745e2c28 100644 --- a/diagnostics/diag.go +++ b/diagnostics/diag.go @@ -16,6 +16,7 @@ import ( "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" ggio "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/gogoprotobuf/io" "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto" + ctxutil "github.com/jbenet/go-ipfs/util/ctx" host "github.com/jbenet/go-ipfs/p2p/host" inet "github.com/jbenet/go-ipfs/p2p/net" @@ -220,8 +221,10 @@ func (d *Diagnostics) sendRequest(ctx context.Context, p peer.ID, pmes *pb.Messa } defer s.Close() - r := ggio.NewDelimitedReader(s, inet.MessageSizeMax) - w := ggio.NewDelimitedWriter(s) + cr := ctxutil.NewReader(ctx, s) // ok to use. we defer close stream in this func + cw := ctxutil.NewWriter(ctx, s) // ok to use. we defer close stream in this func + r := ggio.NewDelimitedReader(cr, inet.MessageSizeMax) + w := ggio.NewDelimitedWriter(cw) start := time.Now()