From d32177cda06d1ae5988f657711d8fa8d0887c0ab Mon Sep 17 00:00:00 2001 From: rht Date: Sun, 16 Aug 2015 18:22:40 +0700 Subject: [PATCH] Make sure ctx in commands are derived from req.Context License: MIT Signed-off-by: rht --- assets/assets.go | 2 +- commands/http/client.go | 3 +-- core/commands/swarm.go | 3 +-- core/core.go | 5 ++++- core/coreunix/add.go | 36 +++++++++------------------------- core/coreunix/metadata.go | 4 ++-- fuse/ipns/common.go | 2 +- importer/trickle/trickledag.go | 3 +-- ipnsfs/dir.go | 10 ++++++---- ipnsfs/system.go | 2 +- merkledag/utils/utils.go | 3 +-- unixfs/io/dirbuilder.go | 15 ++------------ unixfs/mod/dagmodifier.go | 8 ++++---- 13 files changed, 34 insertions(+), 62 deletions(-) diff --git a/assets/assets.go b/assets/assets.go index 73077ab0d..d267f8a55 100644 --- a/assets/assets.go +++ b/assets/assets.go @@ -54,7 +54,7 @@ func addAssetList(nd *core.IpfsNode, l []string) (*key.Key, error) { fname := filepath.Base(p) k := key.B58KeyDecode(s) - if err := dirb.AddChild(fname, k); err != nil { + if err := dirb.AddChild(nd.Context(), fname, k); err != nil { return nil, fmt.Errorf("assets: could not add '%s' as a child: %s", fname, err) } } diff --git a/commands/http/client.go b/commands/http/client.go index 5cb2be1b3..3da268ffe 100644 --- a/commands/http/client.go +++ b/commands/http/client.go @@ -51,8 +51,7 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) { if req.Context() == nil { log.Warningf("no context set in request") - err := req.SetRootContext(context.TODO()) - if err != nil { + if err := req.SetRootContext(context.TODO()); err != nil { return nil, err } } diff --git a/core/commands/swarm.go b/core/commands/swarm.go index a0018bbf4..884e33f7d 100644 --- a/core/commands/swarm.go +++ b/core/commands/swarm.go @@ -15,7 +15,6 @@ import ( ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" mafilter "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/whyrusleeping/multiaddr-filter" - context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" ) type stringList struct { @@ -211,7 +210,7 @@ ipfs swarm connect /ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3 cmds.StringArg("address", true, true, "address of peer to connect to").EnableStdin(), }, Run: func(req cmds.Request, res cmds.Response) { - ctx := context.TODO() + ctx := req.Context() n, err := req.InvocContext().GetNode() if err != nil { diff --git a/core/core.go b/core/core.go index 56647fc53..0c5a6d47b 100644 --- a/core/core.go +++ b/core/core.go @@ -320,7 +320,7 @@ func setupDiscoveryOption(d config.Discovery) DiscoveryOption { func (n *IpfsNode) HandlePeerFound(p peer.PeerInfo) { log.Warning("trying peer info: ", p) - ctx, _ := context.WithTimeout(context.TODO(), time.Second*10) + ctx, _ := context.WithTimeout(n.Context(), time.Second*10) err := n.PeerHost.Connect(ctx, p) if err != nil { log.Warning("Failed to connect to peer found by discovery: ", err) @@ -367,6 +367,9 @@ func (n *IpfsNode) Close() error { // Context returns the IpfsNode context func (n *IpfsNode) Context() context.Context { + if n.ctx == nil { + n.ctx = context.TODO() + } return n.ctx } diff --git a/core/coreunix/add.go b/core/coreunix/add.go index 1d3f6cf6b..4ab16d25a 100644 --- a/core/coreunix/add.go +++ b/core/coreunix/add.go @@ -66,8 +66,7 @@ func AddR(n *core.IpfsNode, root string) (key string, err error) { } n.Pinning.GetManual().RemovePinWithMode(k, pin.Indirect) - err = n.Pinning.Flush() - if err != nil { + if err := n.Pinning.Flush(); err != nil { return "", err } @@ -95,43 +94,28 @@ func AddWrapped(n *core.IpfsNode, r io.Reader, filename string) (string, *merkle func add(n *core.IpfsNode, reader io.Reader) (*merkledag.Node, error) { mp := n.Pinning.GetManual() - node, err := importer.BuildDagFromReader( + return importer.BuildDagFromReader( n.DAG, chunk.DefaultSplitter(reader), importer.PinIndirectCB(mp), ) - if err != nil { - return nil, err - } - - return node, nil } func addNode(n *core.IpfsNode, node *merkledag.Node) error { - err := n.DAG.AddRecursive(node) // add the file to the graph + local storage - if err != nil { + if err := n.DAG.AddRecursive(node); err != nil { // add the file to the graph + local storage return err } - ctx, cancel := context.WithTimeout(context.TODO(), time.Minute) + ctx, cancel := context.WithTimeout(n.Context(), time.Minute) defer cancel() - err = n.Pinning.Pin(ctx, node, true) // ensure we keep it - if err != nil { - return err - } - return nil + err := n.Pinning.Pin(ctx, node, true) // ensure we keep it + return err } func addFile(n *core.IpfsNode, file files.File) (*merkledag.Node, error) { if file.IsDirectory() { return addDir(n, file) } - - dagnode, err := add(n, file) - if err != nil { - return nil, err - } - - return dagnode, nil + return add(n, file) } func addDir(n *core.IpfsNode, dir files.File) (*merkledag.Node, error) { @@ -155,14 +139,12 @@ Loop: _, name := gopath.Split(file.FileName()) - err = tree.AddNodeLink(name, node) - if err != nil { + if err := tree.AddNodeLink(name, node); err != nil { return nil, err } } - err := addNode(n, tree) - if err != nil { + if err := addNode(n, tree); err != nil { return nil, err } return tree, nil diff --git a/core/coreunix/metadata.go b/core/coreunix/metadata.go index 06aab062b..03b03876e 100644 --- a/core/coreunix/metadata.go +++ b/core/coreunix/metadata.go @@ -14,7 +14,7 @@ import ( func AddMetadataTo(n *core.IpfsNode, skey string, m *ft.Metadata) (string, error) { ukey := key.B58KeyDecode(skey) - ctx, cancel := context.WithTimeout(context.TODO(), time.Minute) + ctx, cancel := context.WithTimeout(n.Context(), time.Minute) defer cancel() nd, err := n.DAG.Get(ctx, ukey) if err != nil { @@ -44,7 +44,7 @@ func AddMetadataTo(n *core.IpfsNode, skey string, m *ft.Metadata) (string, error func Metadata(n *core.IpfsNode, skey string) (*ft.Metadata, error) { ukey := key.B58KeyDecode(skey) - ctx, cancel := context.WithTimeout(context.TODO(), time.Minute) + ctx, cancel := context.WithTimeout(n.Context(), time.Minute) defer cancel() nd, err := n.DAG.Get(ctx, ukey) if err != nil { diff --git a/fuse/ipns/common.go b/fuse/ipns/common.go index b4177f052..199130c9d 100644 --- a/fuse/ipns/common.go +++ b/fuse/ipns/common.go @@ -22,7 +22,7 @@ func InitializeKeyspace(n *core.IpfsNode, key ci.PrivKey) error { return err } - ctx, cancel := context.WithTimeout(context.TODO(), time.Minute) + ctx, cancel := context.WithTimeout(n.Context(), time.Minute) defer cancel() err = n.Pinning.Pin(ctx, emptyDir, false) diff --git a/importer/trickle/trickledag.go b/importer/trickle/trickledag.go index 022833749..b62e5aa73 100644 --- a/importer/trickle/trickledag.go +++ b/importer/trickle/trickledag.go @@ -108,8 +108,7 @@ func TrickleAppend(base *dag.Node, db *h.DagBuilderHelper) (out *dag.Node, err_o } // Last child in this node may not be a full tree, lets file it up - err = appendFillLastChild(ufsn, n-1, layerProgress, db) - if err != nil { + if err := appendFillLastChild(ufsn, n-1, layerProgress, db); err != nil { return nil, err } diff --git a/ipnsfs/dir.go b/ipnsfs/dir.go index 52e5af772..b5c07441e 100644 --- a/ipnsfs/dir.go +++ b/ipnsfs/dir.go @@ -26,12 +26,14 @@ type Directory struct { lock sync.Mutex node *dag.Node + ctx context.Context name string } -func NewDirectory(name string, node *dag.Node, parent childCloser, fs *Filesystem) *Directory { +func NewDirectory(ctx context.Context, name string, node *dag.Node, parent childCloser, fs *Filesystem) *Directory { return &Directory{ + ctx: ctx, fs: fs, name: name, node: node, @@ -121,7 +123,7 @@ func (d *Directory) childDir(name string) (*Directory, error) { switch i.GetType() { case ufspb.Data_Directory: - ndir := NewDirectory(name, nd, d, d.fs) + ndir := NewDirectory(d.ctx, name, nd, d, d.fs) d.childDirs[name] = ndir return ndir, nil case ufspb.Data_File: @@ -138,7 +140,7 @@ func (d *Directory) childDir(name string) (*Directory, error) { func (d *Directory) childFromDag(name string) (*dag.Node, error) { for _, lnk := range d.node.Links { if lnk.Name == name { - ctx, cancel := context.WithTimeout(context.TODO(), time.Minute) + ctx, cancel := context.WithTimeout(d.ctx, time.Minute) defer cancel() return lnk.GetNode(ctx, d.fs.dserv) @@ -244,7 +246,7 @@ func (d *Directory) AddChild(name string, nd *dag.Node) error { switch pbn.GetType() { case ft.TDirectory: - d.childDirs[name] = NewDirectory(name, nd, d, d.fs) + d.childDirs[name] = NewDirectory(d.ctx, name, nd, d, d.fs) case ft.TFile, ft.TMetadata, ft.TRaw: nfi, err := NewFile(name, nd, d, d.fs) if err != nil { diff --git a/ipnsfs/system.go b/ipnsfs/system.go index ff6cd3721..68f834a28 100644 --- a/ipnsfs/system.go +++ b/ipnsfs/system.go @@ -186,7 +186,7 @@ func (fs *Filesystem) newKeyRoot(parent context.Context, k ci.PrivKey) (*KeyRoot switch pbn.GetType() { case ft.TDirectory: - root.val = NewDirectory(pointsTo.String(), mnode, root, fs) + root.val = NewDirectory(ctx, pointsTo.String(), mnode, root, fs) case ft.TFile, ft.TMetadata, ft.TRaw: fi, err := NewFile(pointsTo.String(), mnode, root, fs) if err != nil { diff --git a/merkledag/utils/utils.go b/merkledag/utils/utils.go index 6ab612c17..a7e87f052 100644 --- a/merkledag/utils/utils.go +++ b/merkledag/utils/utils.go @@ -48,8 +48,7 @@ func addLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname s // ensure no link with that name already exists _ = root.RemoveNodeLink(childname) // ignore error, only option is ErrNotFound - err = root.AddNodeLinkClean(childname, childnd) - if err != nil { + if err := root.AddNodeLinkClean(childname, childnd); err != nil { return nil, err } diff --git a/unixfs/io/dirbuilder.go b/unixfs/io/dirbuilder.go index d1b67c758..6fdef9ffb 100644 --- a/unixfs/io/dirbuilder.go +++ b/unixfs/io/dirbuilder.go @@ -1,8 +1,6 @@ package io import ( - "time" - "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" key "github.com/ipfs/go-ipfs/blocks/key" @@ -29,22 +27,13 @@ func NewDirectory(dserv mdag.DAGService) *directoryBuilder { } // AddChild adds a (name, key)-pair to the root node. -func (d *directoryBuilder) AddChild(name string, k key.Key) error { - // TODO(cryptix): consolidate context managment - ctx, cancel := context.WithTimeout(context.TODO(), time.Minute) - defer cancel() - +func (d *directoryBuilder) AddChild(ctx context.Context, name string, k key.Key) error { cnode, err := d.dserv.Get(ctx, k) if err != nil { return err } - err = d.dirnode.AddNodeLinkClean(name, cnode) - if err != nil { - return err - } - - return nil + return d.dirnode.AddNodeLinkClean(name, cnode) } // GetNode returns the root of this directoryBuilder diff --git a/unixfs/mod/dagmodifier.go b/unixfs/mod/dagmodifier.go index be7d92248..0d0ae7fbe 100644 --- a/unixfs/mod/dagmodifier.go +++ b/unixfs/mod/dagmodifier.go @@ -428,7 +428,7 @@ func (dm *DagModifier) Truncate(size int64) error { return dm.expandSparse(int64(size) - realSize) } - nnode, err := dagTruncate(dm.curNode, uint64(size), dm.dagserv) + nnode, err := dagTruncate(dm.ctx, dm.curNode, uint64(size), dm.dagserv) if err != nil { return err } @@ -443,7 +443,7 @@ func (dm *DagModifier) Truncate(size int64) error { } // dagTruncate truncates the given node to 'size' and returns the modified Node -func dagTruncate(nd *mdag.Node, size uint64, ds mdag.DAGService) (*mdag.Node, error) { +func dagTruncate(ctx context.Context, nd *mdag.Node, size uint64, ds mdag.DAGService) (*mdag.Node, error) { if len(nd.Links) == 0 { // TODO: this can likely be done without marshaling and remarshaling pbn, err := ft.FromBytes(nd.Data) @@ -460,7 +460,7 @@ func dagTruncate(nd *mdag.Node, size uint64, ds mdag.DAGService) (*mdag.Node, er var modified *mdag.Node ndata := new(ft.FSNode) for i, lnk := range nd.Links { - ctx, cancel := context.WithTimeout(context.TODO(), time.Minute) + _ctx, cancel := context.WithTimeout(ctx, time.Minute) defer cancel() child, err := lnk.GetNode(ctx, ds) @@ -475,7 +475,7 @@ func dagTruncate(nd *mdag.Node, size uint64, ds mdag.DAGService) (*mdag.Node, er // found the child we want to cut if size < cur+childsize { - nchild, err := dagTruncate(child, size-cur, ds) + nchild, err := dagTruncate(_ctx, child, size-cur, ds) if err != nil { return nil, err }