@cryptotaxi247 / kubo / commits / e3c35d8b7

cmds/get: fix context timeout problem

Get had a random timeout of 60s. This commit fixes that, wiring up our contexts correctly. License: MIT Signed-off-by: Juan Batiz-Benet <juan@benet.ai>

Juan Batiz-Benet committed Jul 24, 2015 at 14:43 UTC e3c35d8b761f3b0ea5570ce1a04388b2161785ec
3 files changed +11 -20
core/commands/get.go
+3 -6
@@ -120,8 +120,7 @@ may also specify the level of compression by specifying '-l=<1-9>'.
120 bar.Start()
121 defer bar.Finish()
122
123 - _, err = io.Copy(file, pbReader)
124 - if err != nil {
123 + if _, err := io.Copy(file, pbReader); err != nil {
124 res.SetError(err, cmds.ErrNormal)
125 return
126 }
@@ -140,10 +139,8 @@ may also specify the level of compression by specifying '-l=<1-9>'.
139
140 bar.Start()
141 defer bar.Finish()
143 -
142 extractor := &tar.Extractor{outPath}
145 - err = extractor.Extract(reader)
146 - if err != nil {
143 + if err := extractor.Extract(reader); err != nil {
144 res.SetError(err, cmds.ErrNormal)
145 }
146 },
@@ -169,7 +166,7 @@ func get(ctx context.Context, node *core.IpfsNode, p path.Path, compression int)
166 return nil, err
167 }
168
172 - return utar.NewReader(p, node.DAG, dagnode, compression)
169 + return utar.NewReader(ctx, p, node.DAG, dagnode, compression)
170 }
171
172 // getZip is equivalent to `ipfs getdag $hash | gzip`
thirdparty/tar/extractor.go
+2 -4
@@ -39,15 +39,13 @@ func (te *Extractor) Extract(reader io.Reader) error {
39 }
40
41 if header.Typeflag == tar.TypeDir {
42 - err = te.extractDir(header, i, exists)
43 - if err != nil {
42 + if err := te.extractDir(header, i, exists); err != nil {
43 return err
44 }
45 continue
46 }
47
49 - err = te.extractFile(header, tarReader, i, exists, pathIsDir)
50 - if err != nil {
48 + if err := te.extractFile(header, tarReader, i, exists, pathIsDir); err != nil {
49 return err
50 }
51 }
unixfs/tar/reader.go
+6 -10
@@ -9,7 +9,7 @@ import (
9 "time"
10
11 proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
12 - "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
12 + cxt "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
13
14 mdag "github.com/ipfs/go-ipfs/merkledag"
15 path "github.com/ipfs/go-ipfs/path"
@@ -28,7 +28,7 @@ type Reader struct {
28 err error
29 }
30
31 -func NewReader(path path.Path, dag mdag.DAGService, dagnode *mdag.Node, compression int) (*Reader, error) {
31 +func NewReader(ctx cxt.Context, path path.Path, dag mdag.DAGService, dagnode *mdag.Node, compression int) (*Reader, error) {
32
33 reader := &Reader{
34 signalChan: make(chan struct{}),
@@ -49,12 +49,11 @@ func NewReader(path path.Path, dag mdag.DAGService, dagnode *mdag.Node, compress
49 // writeToBuf will write the data to the buffer, and will signal when there
50 // is new data to read
51 _, filename := gopath.Split(path.String())
52 - go reader.writeToBuf(dagnode, filename, 0)
53 -
52 + go reader.writeToBuf(ctx, dagnode, filename, 0)
53 return reader, nil
54 }
55
57 -func (r *Reader) writeToBuf(dagnode *mdag.Node, path string, depth int) {
56 +func (r *Reader) writeToBuf(ctx cxt.Context, dagnode *mdag.Node, path string, depth int) {
57 pb := new(upb.Data)
58 err := proto.Unmarshal(dagnode.Data, pb)
59 if err != nil {
@@ -80,16 +79,13 @@ func (r *Reader) writeToBuf(dagnode *mdag.Node, path string, depth int) {
79 }
80 r.flush()
81
83 - ctx, cancel := context.WithTimeout(context.TODO(), time.Second*60)
84 - defer cancel()
85 -
82 for i, ng := range r.dag.GetDAG(ctx, dagnode) {
83 childNode, err := ng.Get(ctx)
84 if err != nil {
85 r.emitError(err)
86 return
87 }
92 - r.writeToBuf(childNode, gopath.Join(path, dagnode.Links[i].Name), depth+1)
88 + r.writeToBuf(ctx, childNode, gopath.Join(path, dagnode.Links[i].Name), depth+1)
89 }
90 return
91 }
@@ -108,7 +104,7 @@ func (r *Reader) writeToBuf(dagnode *mdag.Node, path string, depth int) {
104 }
105 r.flush()
106
111 - reader, err := uio.NewDagReader(context.TODO(), dagnode, r.dag)
107 + reader, err := uio.NewDagReader(ctx, dagnode, r.dag)
108 if err != nil {
109 r.emitError(err)
110 return