提交 e3c35d8b 作者: Juan Batiz-Benet

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: 's avatarJuan Batiz-Benet <juan@benet.ai>
上级 886d4756
...@@ -120,8 +120,7 @@ may also specify the level of compression by specifying '-l=<1-9>'. ...@@ -120,8 +120,7 @@ may also specify the level of compression by specifying '-l=<1-9>'.
bar.Start() bar.Start()
defer bar.Finish() defer bar.Finish()
_, err = io.Copy(file, pbReader) if _, err := io.Copy(file, pbReader); err != nil {
if err != nil {
res.SetError(err, cmds.ErrNormal) res.SetError(err, cmds.ErrNormal)
return return
} }
...@@ -140,10 +139,8 @@ may also specify the level of compression by specifying '-l=<1-9>'. ...@@ -140,10 +139,8 @@ may also specify the level of compression by specifying '-l=<1-9>'.
bar.Start() bar.Start()
defer bar.Finish() defer bar.Finish()
extractor := &tar.Extractor{outPath} extractor := &tar.Extractor{outPath}
err = extractor.Extract(reader) if err := extractor.Extract(reader); err != nil {
if err != nil {
res.SetError(err, cmds.ErrNormal) res.SetError(err, cmds.ErrNormal)
} }
}, },
...@@ -169,7 +166,7 @@ func get(ctx context.Context, node *core.IpfsNode, p path.Path, compression int) ...@@ -169,7 +166,7 @@ func get(ctx context.Context, node *core.IpfsNode, p path.Path, compression int)
return nil, err 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` // getZip is equivalent to `ipfs getdag $hash | gzip`
......
...@@ -39,15 +39,13 @@ func (te *Extractor) Extract(reader io.Reader) error { ...@@ -39,15 +39,13 @@ func (te *Extractor) Extract(reader io.Reader) error {
} }
if header.Typeflag == tar.TypeDir { if header.Typeflag == tar.TypeDir {
err = te.extractDir(header, i, exists) if err := te.extractDir(header, i, exists); err != nil {
if err != nil {
return err return err
} }
continue continue
} }
err = te.extractFile(header, tarReader, i, exists, pathIsDir) if err := te.extractFile(header, tarReader, i, exists, pathIsDir); err != nil {
if err != nil {
return err return err
} }
} }
......
...@@ -9,7 +9,7 @@ import ( ...@@ -9,7 +9,7 @@ import (
"time" "time"
proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto" 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" mdag "github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path" path "github.com/ipfs/go-ipfs/path"
...@@ -28,7 +28,7 @@ type Reader struct { ...@@ -28,7 +28,7 @@ type Reader struct {
err error 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{ reader := &Reader{
signalChan: make(chan struct{}), signalChan: make(chan struct{}),
...@@ -49,12 +49,11 @@ func NewReader(path path.Path, dag mdag.DAGService, dagnode *mdag.Node, compress ...@@ -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 // writeToBuf will write the data to the buffer, and will signal when there
// is new data to read // is new data to read
_, filename := gopath.Split(path.String()) _, filename := gopath.Split(path.String())
go reader.writeToBuf(dagnode, filename, 0) go reader.writeToBuf(ctx, dagnode, filename, 0)
return reader, nil 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) pb := new(upb.Data)
err := proto.Unmarshal(dagnode.Data, pb) err := proto.Unmarshal(dagnode.Data, pb)
if err != nil { if err != nil {
...@@ -80,16 +79,13 @@ func (r *Reader) writeToBuf(dagnode *mdag.Node, path string, depth int) { ...@@ -80,16 +79,13 @@ func (r *Reader) writeToBuf(dagnode *mdag.Node, path string, depth int) {
} }
r.flush() r.flush()
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*60)
defer cancel()
for i, ng := range r.dag.GetDAG(ctx, dagnode) { for i, ng := range r.dag.GetDAG(ctx, dagnode) {
childNode, err := ng.Get(ctx) childNode, err := ng.Get(ctx)
if err != nil { if err != nil {
r.emitError(err) r.emitError(err)
return 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 return
} }
...@@ -108,7 +104,7 @@ func (r *Reader) writeToBuf(dagnode *mdag.Node, path string, depth int) { ...@@ -108,7 +104,7 @@ func (r *Reader) writeToBuf(dagnode *mdag.Node, path string, depth int) {
} }
r.flush() r.flush()
reader, err := uio.NewDagReader(context.TODO(), dagnode, r.dag) reader, err := uio.NewDagReader(ctx, dagnode, r.dag)
if err != nil { if err != nil {
r.emitError(err) r.emitError(err)
return return
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论