mirror of
https://github.com/ipfs/kubo.git
synced 2025-10-25 02:16:56 +08:00
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>
This commit is contained in:
@ -120,8 +120,7 @@ may also specify the level of compression by specifying '-l=<1-9>'.
|
||||
bar.Start()
|
||||
defer bar.Finish()
|
||||
|
||||
_, err = io.Copy(file, pbReader)
|
||||
if err != nil {
|
||||
if _, err := io.Copy(file, pbReader); err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
@ -140,10 +139,8 @@ may also specify the level of compression by specifying '-l=<1-9>'.
|
||||
|
||||
bar.Start()
|
||||
defer bar.Finish()
|
||||
|
||||
extractor := &tar.Extractor{outPath}
|
||||
err = extractor.Extract(reader)
|
||||
if err != nil {
|
||||
if err := extractor.Extract(reader); err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
}
|
||||
},
|
||||
@ -169,7 +166,7 @@ func get(ctx context.Context, node *core.IpfsNode, p path.Path, compression int)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return utar.NewReader(p, node.DAG, dagnode, compression)
|
||||
return utar.NewReader(ctx, p, node.DAG, dagnode, compression)
|
||||
}
|
||||
|
||||
// getZip is equivalent to `ipfs getdag $hash | gzip`
|
||||
|
||||
6
thirdparty/tar/extractor.go
vendored
6
thirdparty/tar/extractor.go
vendored
@ -39,15 +39,13 @@ func (te *Extractor) Extract(reader io.Reader) error {
|
||||
}
|
||||
|
||||
if header.Typeflag == tar.TypeDir {
|
||||
err = te.extractDir(header, i, exists)
|
||||
if err != nil {
|
||||
if err := te.extractDir(header, i, exists); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
err = te.extractFile(header, tarReader, i, exists, pathIsDir)
|
||||
if err != nil {
|
||||
if err := te.extractFile(header, tarReader, i, exists, pathIsDir); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,7 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
|
||||
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
||||
cxt "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
||||
|
||||
mdag "github.com/ipfs/go-ipfs/merkledag"
|
||||
path "github.com/ipfs/go-ipfs/path"
|
||||
@ -28,7 +28,7 @@ type Reader struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func NewReader(path path.Path, dag mdag.DAGService, dagnode *mdag.Node, compression int) (*Reader, error) {
|
||||
func NewReader(ctx cxt.Context, path path.Path, dag mdag.DAGService, dagnode *mdag.Node, compression int) (*Reader, error) {
|
||||
|
||||
reader := &Reader{
|
||||
signalChan: make(chan struct{}),
|
||||
@ -49,12 +49,11 @@ func NewReader(path path.Path, dag mdag.DAGService, dagnode *mdag.Node, compress
|
||||
// writeToBuf will write the data to the buffer, and will signal when there
|
||||
// is new data to read
|
||||
_, filename := gopath.Split(path.String())
|
||||
go reader.writeToBuf(dagnode, filename, 0)
|
||||
|
||||
go reader.writeToBuf(ctx, dagnode, filename, 0)
|
||||
return reader, nil
|
||||
}
|
||||
|
||||
func (r *Reader) writeToBuf(dagnode *mdag.Node, path string, depth int) {
|
||||
func (r *Reader) writeToBuf(ctx cxt.Context, dagnode *mdag.Node, path string, depth int) {
|
||||
pb := new(upb.Data)
|
||||
err := proto.Unmarshal(dagnode.Data, pb)
|
||||
if err != nil {
|
||||
@ -80,16 +79,13 @@ func (r *Reader) writeToBuf(dagnode *mdag.Node, path string, depth int) {
|
||||
}
|
||||
r.flush()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*60)
|
||||
defer cancel()
|
||||
|
||||
for i, ng := range r.dag.GetDAG(ctx, dagnode) {
|
||||
childNode, err := ng.Get(ctx)
|
||||
if err != nil {
|
||||
r.emitError(err)
|
||||
return
|
||||
}
|
||||
r.writeToBuf(childNode, gopath.Join(path, dagnode.Links[i].Name), depth+1)
|
||||
r.writeToBuf(ctx, childNode, gopath.Join(path, dagnode.Links[i].Name), depth+1)
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -108,7 +104,7 @@ func (r *Reader) writeToBuf(dagnode *mdag.Node, path string, depth int) {
|
||||
}
|
||||
r.flush()
|
||||
|
||||
reader, err := uio.NewDagReader(context.TODO(), dagnode, r.dag)
|
||||
reader, err := uio.NewDagReader(ctx, dagnode, r.dag)
|
||||
if err != nil {
|
||||
r.emitError(err)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user