@cryptotaxi247 / kubo / commits / c64338a81

diag/net: io must respect timeout ctx

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.

Juan Batiz-Benet committed Jan 17, 2015 at 14:54 UTC c64338a816d52b737b3a79f804a078eda8ad7635
1 file changed +5 -2
diagnostics/diag.go
+5 -2
@@ -16,6 +16,7 @@ import (
16 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
17 ggio "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/gogoprotobuf/io"
18 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
19 + ctxutil "github.com/jbenet/go-ipfs/util/ctx"
20
21 host "github.com/jbenet/go-ipfs/p2p/host"
22 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
221 }
222 defer s.Close()
223
223 - r := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
224 - w := ggio.NewDelimitedWriter(s)
224 + cr := ctxutil.NewReader(ctx, s) // ok to use. we defer close stream in this func
225 + cw := ctxutil.NewWriter(ctx, s) // ok to use. we defer close stream in this func
226 + r := ggio.NewDelimitedReader(cr, inet.MessageSizeMax)
227 + w := ggio.NewDelimitedWriter(cw)
228
229 start := time.Now()
230