From d32177cda06d1ae5988f657711d8fa8d0887c0ab Mon Sep 17 00:00:00 2001 From: rht Date: Sun, 16 Aug 2015 18:22:40 +0700 Subject: [PATCH 1/7] 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 73077ab0d2f..d267f8a55ba 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 5cb2be1b39c..3da268ffee9 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 a0018bbf4ea..884e33f7d59 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 56647fc539a..0c5a6d47b06 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 1d3f6cf6b08..4ab16d25acf 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 06aab062b0a..03b03876ebd 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 b4177f052f8..199130c9da2 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 0228337497f..b62e5aa7394 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 52e5af772f7..b5c07441e12 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 ff6cd372166..68f834a284f 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 6ab612c175a..a7e87f05248 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 d1b67c758e7..6fdef9ffb0e 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 be7d92248db..0d0ae7fbe48 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 } From 5d8e15042f361e59f729d2c8f11724d58f2c7b1e Mon Sep 17 00:00:00 2001 From: rht Date: Mon, 17 Aug 2015 15:40:48 +0700 Subject: [PATCH 2/7] Replace WithTimeout with WithCancel whenever possible License: MIT Signed-off-by: rht --- core/commands/ls.go | 3 +-- core/commands/unixfs/ls.go | 3 +-- core/core.go | 3 +-- core/corerepo/pinning.go | 5 ++--- core/coreunix/add.go | 3 +-- core/coreunix/metadata.go | 6 ++---- fuse/ipns/common.go | 4 +--- ipnsfs/dir.go | 3 +-- path/resolver.go | 2 +- unixfs/mod/dagmodifier.go | 3 +-- 10 files changed, 12 insertions(+), 23 deletions(-) diff --git a/core/commands/ls.go b/core/commands/ls.go index 92115532191..01408e2dac6 100644 --- a/core/commands/ls.go +++ b/core/commands/ls.go @@ -5,7 +5,6 @@ import ( "fmt" "io" "text/tabwriter" - "time" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" @@ -81,7 +80,7 @@ it contains, with the following format: Links: make([]LsLink, len(dagnode.Links)), } for j, link := range dagnode.Links { - ctx, cancel := context.WithTimeout(req.Context(), time.Minute) + ctx, cancel := context.WithCancel(req.Context()) defer cancel() link.Node, err = link.GetNode(ctx, node.DAG) if err != nil { diff --git a/core/commands/unixfs/ls.go b/core/commands/unixfs/ls.go index 256d87db60a..2cd7e768657 100644 --- a/core/commands/unixfs/ls.go +++ b/core/commands/unixfs/ls.go @@ -6,7 +6,6 @@ import ( "io" "sort" "text/tabwriter" - "time" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" @@ -111,7 +110,7 @@ size is the IPFS link size. links := make([]LsLink, len(merkleNode.Links)) output.Objects[hash].Links = links for i, link := range merkleNode.Links { - getCtx, cancel := context.WithTimeout(ctx, time.Minute) + getCtx, cancel := context.WithCancel(ctx) defer cancel() link.Node, err = link.GetNode(getCtx, node.DAG) if err != nil { diff --git a/core/core.go b/core/core.go index 0c5a6d47b06..b63d88d7cbb 100644 --- a/core/core.go +++ b/core/core.go @@ -321,8 +321,7 @@ func setupDiscoveryOption(d config.Discovery) DiscoveryOption { func (n *IpfsNode) HandlePeerFound(p peer.PeerInfo) { log.Warning("trying peer info: ", p) ctx, _ := context.WithTimeout(n.Context(), time.Second*10) - err := n.PeerHost.Connect(ctx, p) - if err != nil { + if err := n.PeerHost.Connect(ctx, p); err != nil { log.Warning("Failed to connect to peer found by discovery: ", err) } } diff --git a/core/corerepo/pinning.go b/core/corerepo/pinning.go index 047346d5bc6..0c30b5592e9 100644 --- a/core/corerepo/pinning.go +++ b/core/corerepo/pinning.go @@ -15,7 +15,6 @@ package corerepo import ( "fmt" - "time" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" @@ -42,7 +41,7 @@ func Pin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool) return nil, err } - ctx, cancel := context.WithTimeout(ctx, time.Minute) + ctx, cancel := context.WithCancel(ctx) defer cancel() err = n.Pinning.Pin(ctx, dagnode, recursive) if err != nil { @@ -74,7 +73,7 @@ func Unpin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool for _, dagnode := range dagnodes { k, _ := dagnode.Key() - ctx, cancel := context.WithTimeout(ctx, time.Minute) + ctx, cancel := context.WithCancel(ctx) defer cancel() err := n.Pinning.Unpin(ctx, k, recursive) if err != nil { diff --git a/core/coreunix/add.go b/core/coreunix/add.go index 4ab16d25acf..e17a82450ac 100644 --- a/core/coreunix/add.go +++ b/core/coreunix/add.go @@ -5,7 +5,6 @@ import ( "io/ioutil" "os" gopath "path" - "time" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" @@ -105,7 +104,7 @@ func addNode(n *core.IpfsNode, node *merkledag.Node) error { if err := n.DAG.AddRecursive(node); err != nil { // add the file to the graph + local storage return err } - ctx, cancel := context.WithTimeout(n.Context(), time.Minute) + ctx, cancel := context.WithCancel(n.Context()) defer cancel() err := n.Pinning.Pin(ctx, node, true) // ensure we keep it return err diff --git a/core/coreunix/metadata.go b/core/coreunix/metadata.go index 03b03876ebd..426bdf2e152 100644 --- a/core/coreunix/metadata.go +++ b/core/coreunix/metadata.go @@ -1,8 +1,6 @@ package coreunix import ( - "time" - context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" key "github.com/ipfs/go-ipfs/blocks/key" @@ -14,7 +12,7 @@ import ( func AddMetadataTo(n *core.IpfsNode, skey string, m *ft.Metadata) (string, error) { ukey := key.B58KeyDecode(skey) - ctx, cancel := context.WithTimeout(n.Context(), time.Minute) + ctx, cancel := context.WithCancel(n.Context()) defer cancel() nd, err := n.DAG.Get(ctx, ukey) if err != nil { @@ -44,7 +42,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(n.Context(), time.Minute) + ctx, cancel := context.WithCancel(n.Context()) 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 199130c9da2..0ef0ca0bd36 100644 --- a/fuse/ipns/common.go +++ b/fuse/ipns/common.go @@ -1,8 +1,6 @@ package ipns import ( - "time" - context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" "github.com/ipfs/go-ipfs/core" @@ -22,7 +20,7 @@ func InitializeKeyspace(n *core.IpfsNode, key ci.PrivKey) error { return err } - ctx, cancel := context.WithTimeout(n.Context(), time.Minute) + ctx, cancel := context.WithCancel(n.Context()) defer cancel() err = n.Pinning.Pin(ctx, emptyDir, false) diff --git a/ipnsfs/dir.go b/ipnsfs/dir.go index b5c07441e12..ba203bd927a 100644 --- a/ipnsfs/dir.go +++ b/ipnsfs/dir.go @@ -5,7 +5,6 @@ import ( "fmt" "os" "sync" - "time" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" @@ -140,7 +139,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(d.ctx, time.Minute) + ctx, cancel := context.WithCancel(d.ctx) defer cancel() return lnk.GetNode(ctx, d.fs.dserv) diff --git a/path/resolver.go b/path/resolver.go index 28807500040..5740e829ef0 100644 --- a/path/resolver.go +++ b/path/resolver.go @@ -87,7 +87,7 @@ func (s *Resolver) ResolvePathComponents(ctx context.Context, fpath Path) ([]*me } log.Debug("Resolve dag get.") - ctx, cancel := context.WithTimeout(ctx, time.Minute) + ctx, cancel := context.WithCancel(ctx) defer cancel() nd, err := s.DAG.Get(ctx, key.Key(h)) if err != nil { diff --git a/unixfs/mod/dagmodifier.go b/unixfs/mod/dagmodifier.go index 0d0ae7fbe48..2ed5f9d27e8 100644 --- a/unixfs/mod/dagmodifier.go +++ b/unixfs/mod/dagmodifier.go @@ -5,7 +5,6 @@ import ( "errors" "io" "os" - "time" proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto" mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" @@ -460,7 +459,7 @@ func dagTruncate(ctx context.Context, nd *mdag.Node, size uint64, ds mdag.DAGSer var modified *mdag.Node ndata := new(ft.FSNode) for i, lnk := range nd.Links { - _ctx, cancel := context.WithTimeout(ctx, time.Minute) + _ctx, cancel := context.WithCancel(ctx) defer cancel() child, err := lnk.GetNode(ctx, ds) From 34e06f6c95a5475c00743f72b5b5e56ddf747116 Mon Sep 17 00:00:00 2001 From: rht Date: Thu, 20 Aug 2015 07:59:52 +0700 Subject: [PATCH 3/7] Wire a context down to (n *helpers.UnixfsNode) GetChild License: MIT Signed-off-by: rht --- importer/helpers/helpers.go | 4 +-- importer/trickle/trickle_test.go | 17 ++++++----- importer/trickle/trickledag.go | 51 ++++++++++++-------------------- unixfs/mod/dagmodifier.go | 2 +- 4 files changed, 32 insertions(+), 42 deletions(-) diff --git a/importer/helpers/helpers.go b/importer/helpers/helpers.go index 7a7d7169759..ac7d3cd8a6e 100644 --- a/importer/helpers/helpers.go +++ b/importer/helpers/helpers.go @@ -77,8 +77,8 @@ func (n *UnixfsNode) NumChildren() int { return n.ufmt.NumChildren() } -func (n *UnixfsNode) GetChild(i int, ds dag.DAGService) (*UnixfsNode, error) { - ctx, cancel := context.WithTimeout(context.TODO(), time.Minute) +func (n *UnixfsNode) GetChild(ctx context.Context, i int, ds dag.DAGService) (*UnixfsNode, error) { + ctx, cancel := context.WithTimeout(ctx, time.Minute) defer cancel() nd, err := n.node.Links[i].GetNode(ctx, ds) diff --git a/importer/trickle/trickle_test.go b/importer/trickle/trickle_test.go index b6cc0ec8d50..a64b1f4ec6d 100644 --- a/importer/trickle/trickle_test.go +++ b/importer/trickle/trickle_test.go @@ -443,7 +443,8 @@ func TestAppend(t *testing.T) { r := bytes.NewReader(should[nbytes/2:]) blks, errs := chunk.Chan(chunk.NewSizeSplitter(r, 500)) - nnode, err := TrickleAppend(nd, dbp.New(blks, errs)) + ctx := context.TODO() + nnode, err := TrickleAppend(ctx, nd, dbp.New(blks, errs)) if err != nil { t.Fatal(err) } @@ -453,7 +454,7 @@ func TestAppend(t *testing.T) { t.Fatal(err) } - fread, err := uio.NewDagReader(context.TODO(), nnode, ds) + fread, err := uio.NewDagReader(ctx, nnode, ds) if err != nil { t.Fatal(err) } @@ -491,10 +492,11 @@ func TestMultipleAppends(t *testing.T) { spl := chunk.SizeSplitterGen(500) + ctx := context.TODO() for i := 0; i < len(should); i++ { blks, errs := chunk.Chan(spl(bytes.NewReader(should[i : i+1]))) - nnode, err := TrickleAppend(nd, dbp.New(blks, errs)) + nnode, err := TrickleAppend(ctx, nd, dbp.New(blks, errs)) if err != nil { t.Fatal(err) } @@ -504,7 +506,7 @@ func TestMultipleAppends(t *testing.T) { t.Fatal(err) } - fread, err := uio.NewDagReader(context.TODO(), nnode, ds) + fread, err := uio.NewDagReader(ctx, nnode, ds) if err != nil { t.Fatal(err) } @@ -538,19 +540,20 @@ func TestAppendSingleBytesToEmpty(t *testing.T) { blks, errs := chunk.Chan(spl(bytes.NewReader(data[:1]))) - nnode, err := TrickleAppend(nd, dbp.New(blks, errs)) + ctx := context.TODO() + nnode, err := TrickleAppend(ctx, nd, dbp.New(blks, errs)) if err != nil { t.Fatal(err) } blks, errs = chunk.Chan(spl(bytes.NewReader(data[1:]))) - nnode, err = TrickleAppend(nnode, dbp.New(blks, errs)) + nnode, err = TrickleAppend(ctx, nnode, dbp.New(blks, errs)) if err != nil { t.Fatal(err) } - fread, err := uio.NewDagReader(context.TODO(), nnode, ds) + fread, err := uio.NewDagReader(ctx, nnode, ds) if err != nil { t.Fatal(err) } diff --git a/importer/trickle/trickledag.go b/importer/trickle/trickledag.go index b62e5aa7394..7dbdd18f380 100644 --- a/importer/trickle/trickledag.go +++ b/importer/trickle/trickledag.go @@ -18,19 +18,16 @@ const layerRepeat = 4 func TrickleLayout(db *h.DagBuilderHelper) (*dag.Node, error) { root := h.NewUnixfsNode() - err := db.FillNodeLayer(root) - if err != nil { + if err := db.FillNodeLayer(root); err != nil { return nil, err } for level := 1; !db.Done(); level++ { for i := 0; i < layerRepeat && !db.Done(); i++ { next := h.NewUnixfsNode() - err := fillTrickleRec(db, next, level) - if err != nil { + if err := fillTrickleRec(db, next, level); err != nil { return nil, err } - err = root.AddChild(next, db) - if err != nil { + if err := root.AddChild(next, db); err != nil { return nil, err } } @@ -41,8 +38,7 @@ func TrickleLayout(db *h.DagBuilderHelper) (*dag.Node, error) { return nil, err } - err = db.Close() - if err != nil { + if err := db.Close(); err != nil { return nil, err } @@ -51,21 +47,18 @@ func TrickleLayout(db *h.DagBuilderHelper) (*dag.Node, error) { func fillTrickleRec(db *h.DagBuilderHelper, node *h.UnixfsNode, depth int) error { // Always do this, even in the base case - err := db.FillNodeLayer(node) - if err != nil { + if err := db.FillNodeLayer(node); err != nil { return err } for i := 1; i < depth && !db.Done(); i++ { for j := 0; j < layerRepeat && !db.Done(); j++ { next := h.NewUnixfsNode() - err := fillTrickleRec(db, next, i) - if err != nil { + if err := fillTrickleRec(db, next, i); err != nil { return err } - err = node.AddChild(next, db) - if err != nil { + if err := node.AddChild(next, db); err != nil { return err } } @@ -74,11 +67,10 @@ func fillTrickleRec(db *h.DagBuilderHelper, node *h.UnixfsNode, depth int) error } // TrickleAppend appends the data in `db` to the dag, using the Trickledag format -func TrickleAppend(base *dag.Node, db *h.DagBuilderHelper) (out *dag.Node, err_out error) { +func TrickleAppend(ctx context.Context, base *dag.Node, db *h.DagBuilderHelper) (out *dag.Node, err_out error) { defer func() { if err_out == nil { - err := db.Close() - if err != nil { + if err := db.Close(); err != nil { err_out = err } } @@ -94,8 +86,7 @@ func TrickleAppend(base *dag.Node, db *h.DagBuilderHelper) (out *dag.Node, err_o n, layerProgress := trickleDepthInfo(ufsn, db.Maxlinks()) if n == 0 { // If direct blocks not filled... - err := db.FillNodeLayer(ufsn) - if err != nil { + if err := db.FillNodeLayer(ufsn); err != nil { return nil, err } @@ -108,7 +99,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 - if err := appendFillLastChild(ufsn, n-1, layerProgress, db); err != nil { + if err := appendFillLastChild(ctx, ufsn, n-1, layerProgress, db); err != nil { return nil, err } @@ -138,19 +129,19 @@ func TrickleAppend(base *dag.Node, db *h.DagBuilderHelper) (out *dag.Node, err_o // appendFillLastChild will take in an incomplete trickledag node (uncomplete meaning, not full) and // fill it out to the specified depth with blocks from the given DagBuilderHelper -func appendFillLastChild(ufsn *h.UnixfsNode, depth int, layerFill int, db *h.DagBuilderHelper) error { +func appendFillLastChild(ctx context.Context, ufsn *h.UnixfsNode, depth int, layerFill int, db *h.DagBuilderHelper) error { if ufsn.NumChildren() <= db.Maxlinks() { return nil } // Recursive step, grab last child last := ufsn.NumChildren() - 1 - lastChild, err := ufsn.GetChild(last, db.GetDagServ()) + lastChild, err := ufsn.GetChild(ctx, last, db.GetDagServ()) if err != nil { return err } // Fill out last child (may not be full tree) - nchild, err := trickleAppendRec(lastChild, db, depth-1) + nchild, err := trickleAppendRec(ctx, lastChild, db, depth-1) if err != nil { return err } @@ -182,7 +173,7 @@ func appendFillLastChild(ufsn *h.UnixfsNode, depth int, layerFill int, db *h.Dag } // recursive call for TrickleAppend -func trickleAppendRec(ufsn *h.UnixfsNode, db *h.DagBuilderHelper, depth int) (*h.UnixfsNode, error) { +func trickleAppendRec(ctx context.Context, ufsn *h.UnixfsNode, db *h.DagBuilderHelper, depth int) (*h.UnixfsNode, error) { if depth == 0 || db.Done() { return ufsn, nil } @@ -191,8 +182,7 @@ func trickleAppendRec(ufsn *h.UnixfsNode, db *h.DagBuilderHelper, depth int) (*h n, layerProgress := trickleDepthInfo(ufsn, db.Maxlinks()) if n == 0 { // If direct blocks not filled... - err := db.FillNodeLayer(ufsn) - if err != nil { + if err := db.FillNodeLayer(ufsn); err != nil { return nil, err } n++ @@ -203,8 +193,7 @@ func trickleAppendRec(ufsn *h.UnixfsNode, db *h.DagBuilderHelper, depth int) (*h return ufsn, nil } - err := appendFillLastChild(ufsn, n, layerProgress, db) - if err != nil { + if err := appendFillLastChild(ctx, ufsn, n, layerProgress, db); err != nil { return nil, err } @@ -217,13 +206,11 @@ func trickleAppendRec(ufsn *h.UnixfsNode, db *h.DagBuilderHelper, depth int) (*h for i := n; i < depth && !db.Done(); i++ { for j := 0; j < layerRepeat && !db.Done(); j++ { next := h.NewUnixfsNode() - err := fillTrickleRec(db, next, i) - if err != nil { + if err := fillTrickleRec(db, next, i); err != nil { return nil, err } - err = ufsn.AddChild(next, db) - if err != nil { + if err := ufsn.AddChild(next, db); err != nil { return nil, err } } diff --git a/unixfs/mod/dagmodifier.go b/unixfs/mod/dagmodifier.go index 2ed5f9d27e8..6ea761989d9 100644 --- a/unixfs/mod/dagmodifier.go +++ b/unixfs/mod/dagmodifier.go @@ -312,7 +312,7 @@ func (dm *DagModifier) appendData(node *mdag.Node, blks <-chan []byte, errs <-ch NodeCB: imp.BasicPinnerCB(dm.mp), } - return trickle.TrickleAppend(node, dbp.New(blks, errs)) + return trickle.TrickleAppend(dm.ctx, node, dbp.New(blks, errs)) } // Read data from this dag starting at the current offset From a7202fa94cbb57689940873c19aac9ad786d9f29 Mon Sep 17 00:00:00 2001 From: rht Date: Sun, 23 Aug 2015 19:33:53 +0700 Subject: [PATCH 4/7] Fix 'ctx, _' to have explicit cancel License: MIT Signed-off-by: rht --- blockservice/test/blocks_test.go | 6 ++++-- core/bootstrap.go | 3 ++- core/core.go | 3 ++- diagnostics/diag.go | 3 ++- exchange/bitswap/bitswap_test.go | 15 ++++++++++----- .../bitswap/notifications/notifications_test.go | 3 ++- namesys/publisher.go | 9 +++++---- pin/pin_test.go | 12 ++++++------ routing/dht/ext_test.go | 6 ++++-- 9 files changed, 37 insertions(+), 23 deletions(-) diff --git a/blockservice/test/blocks_test.go b/blockservice/test/blocks_test.go index dbbc5156205..c94a2357e07 100644 --- a/blockservice/test/blocks_test.go +++ b/blockservice/test/blocks_test.go @@ -42,7 +42,8 @@ func TestBlocks(t *testing.T) { t.Error("returned key is not equal to block key", err) } - ctx, _ := context.WithTimeout(context.TODO(), time.Second*5) + ctx, cancel := context.WithTimeout(context.TODO(), time.Second*5) + defer cancel() b2, err := bs.GetBlock(ctx, b.Key()) if err != nil { t.Error("failed to retrieve block from BlockService", err) @@ -75,7 +76,8 @@ func TestGetBlocksSequential(t *testing.T) { t.Log("one instance at a time, get blocks concurrently") for i := 1; i < len(servs); i++ { - ctx, _ := context.WithTimeout(context.TODO(), time.Second*50) + ctx, cancel := context.WithTimeout(context.TODO(), time.Second*50) + defer cancel() out := servs[i].GetBlocks(ctx, keys) gotten := make(map[key.Key]*blocks.Block) for blk := range out { diff --git a/core/bootstrap.go b/core/bootstrap.go index 2c47529706a..a7508de1f4e 100644 --- a/core/bootstrap.go +++ b/core/bootstrap.go @@ -110,7 +110,8 @@ func Bootstrap(n *IpfsNode, cfg BootstrapConfig) (io.Closer, error) { func bootstrapRound(ctx context.Context, host host.Host, cfg BootstrapConfig) error { - ctx, _ = context.WithTimeout(ctx, cfg.ConnectionTimeout) + ctx, cancel := context.WithTimeout(ctx, cfg.ConnectionTimeout) + defer cancel() id := host.ID() // get bootstrap peers from config. retrieving them here makes diff --git a/core/core.go b/core/core.go index b63d88d7cbb..6a47c04f0c8 100644 --- a/core/core.go +++ b/core/core.go @@ -320,7 +320,8 @@ func setupDiscoveryOption(d config.Discovery) DiscoveryOption { func (n *IpfsNode) HandlePeerFound(p peer.PeerInfo) { log.Warning("trying peer info: ", p) - ctx, _ := context.WithTimeout(n.Context(), time.Second*10) + ctx, cancel := context.WithTimeout(n.Context(), time.Second*10) + defer cancel() if err := n.PeerHost.Connect(ctx, p); err != nil { log.Warning("Failed to connect to peer found by discovery: ", err) } diff --git a/diagnostics/diag.go b/diagnostics/diag.go index 104cb597712..3877ee139a4 100644 --- a/diagnostics/diag.go +++ b/diagnostics/diag.go @@ -298,7 +298,8 @@ func (d *Diagnostics) HandleMessage(ctx context.Context, s inet.Stream) error { if timeout < HopTimeoutDecrement { return fmt.Errorf("timeout too short: %s", timeout) } - ctx, _ = context.WithTimeout(ctx, timeout) + ctx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() pmes.SetTimeoutDuration(timeout - HopTimeoutDecrement) dpeers, err := d.getDiagnosticFromPeers(ctx, d.getPeers(), pmes) diff --git a/exchange/bitswap/bitswap_test.go b/exchange/bitswap/bitswap_test.go index e70b3885a36..41f0e6c0841 100644 --- a/exchange/bitswap/bitswap_test.go +++ b/exchange/bitswap/bitswap_test.go @@ -50,7 +50,8 @@ func TestProviderForKeyButNetworkCannotFind(t *testing.T) { // TODO revisit this solo := g.Next() defer solo.Exchange.Close() - ctx, _ := context.WithTimeout(context.Background(), time.Nanosecond) + ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond) + defer cancel() _, err := solo.Exchange.GetBlock(ctx, block.Key()) if err != context.DeadlineExceeded { @@ -76,7 +77,8 @@ func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) { wantsBlock := peers[1] defer wantsBlock.Exchange.Close() - ctx, _ := context.WithTimeout(context.Background(), time.Second) + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() received, err := wantsBlock.Exchange.GetBlock(ctx, block.Key()) if err != nil { t.Log(err) @@ -226,14 +228,16 @@ func TestSendToWantingPeer(t *testing.T) { alpha := bg.Next() // peerA requests and waits for block alpha - ctx, _ := context.WithTimeout(context.TODO(), waitTime) + ctx, cancel := context.WithTimeout(context.TODO(), waitTime) + defer cancel() alphaPromise, err := peerA.Exchange.GetBlocks(ctx, []key.Key{alpha.Key()}) if err != nil { t.Fatal(err) } // peerB announces to the network that he has block alpha - ctx, _ = context.WithTimeout(context.TODO(), timeout) + ctx, cancel = context.WithTimeout(context.TODO(), timeout) + defer cancel() err = peerB.Exchange.HasBlock(ctx, alpha) if err != nil { t.Fatal(err) @@ -266,7 +270,8 @@ func TestBasicBitswap(t *testing.T) { t.Fatal(err) } - ctx, _ := context.WithTimeout(context.TODO(), time.Second*5) + ctx, cancel := context.WithTimeout(context.TODO(), time.Second*5) + defer cancel() blk, err := instances[1].Exchange.GetBlock(ctx, blocks[0].Key()) if err != nil { t.Fatal(err) diff --git a/exchange/bitswap/notifications/notifications_test.go b/exchange/bitswap/notifications/notifications_test.go index e9be15aa409..8ab9887ff3b 100644 --- a/exchange/bitswap/notifications/notifications_test.go +++ b/exchange/bitswap/notifications/notifications_test.go @@ -112,7 +112,8 @@ func TestSubscribeIsANoopWhenCalledWithNoKeys(t *testing.T) { func TestCarryOnWhenDeadlineExpires(t *testing.T) { impossibleDeadline := time.Nanosecond - fastExpiringCtx, _ := context.WithTimeout(context.Background(), impossibleDeadline) + fastExpiringCtx, cancel := context.WithTimeout(context.Background(), impossibleDeadline) + defer cancel() n := New() defer n.Shutdown() diff --git a/namesys/publisher.go b/namesys/publisher.go index 3f5e15ae5e9..e3dd1d81bb3 100644 --- a/namesys/publisher.go +++ b/namesys/publisher.go @@ -60,7 +60,8 @@ func (p *ipnsPublisher) Publish(ctx context.Context, k ci.PrivKey, value path.Pa log.Debugf("Storing pubkey at: %s", namekey) // Store associated public key - timectx, _ := context.WithDeadline(ctx, time.Now().Add(time.Second*10)) + timectx, cancel := context.WithDeadline(ctx, time.Now().Add(time.Second*10)) + defer cancel() err = p.routing.PutValue(timectx, namekey, pkbytes) if err != nil { return err @@ -70,9 +71,9 @@ func (p *ipnsPublisher) Publish(ctx context.Context, k ci.PrivKey, value path.Pa log.Debugf("Storing ipns entry at: %s", ipnskey) // Store ipns entry at "/ipns/"+b58(h(pubkey)) - timectx, _ = context.WithDeadline(ctx, time.Now().Add(time.Second*10)) - err = p.routing.PutValue(timectx, ipnskey, data) - if err != nil { + timectx, cancel = context.WithDeadline(ctx, time.Now().Add(time.Second*10)) + defer cancel() + if err := p.routing.PutValue(timectx, ipnskey, data); err != nil { return err } diff --git a/pin/pin_test.go b/pin/pin_test.go index 223beb03e5f..d3947254d55 100644 --- a/pin/pin_test.go +++ b/pin/pin_test.go @@ -210,21 +210,21 @@ func TestPinRecursiveFail(t *testing.T) { } // Note: this isnt a time based test, we expect the pin to fail - mctx, _ := context.WithTimeout(ctx, time.Millisecond) + mctx, cancel := context.WithTimeout(ctx, time.Millisecond) + defer cancel() err = p.Pin(mctx, a, true) if err == nil { t.Fatal("should have failed to pin here") } - _, err = dserv.Add(b) - if err != nil { + if _, err := dserv.Add(b); err != nil { t.Fatal(err) } // this one is time based... but shouldnt cause any issues - mctx, _ = context.WithTimeout(ctx, time.Second) - err = p.Pin(mctx, a, true) - if err != nil { + mctx, cancel = context.WithTimeout(ctx, time.Second) + defer cancel() + if err := p.Pin(mctx, a, true); err != nil { t.Fatal(err) } } diff --git a/routing/dht/ext_test.go b/routing/dht/ext_test.go index c771165782d..75219da5ccd 100644 --- a/routing/dht/ext_test.go +++ b/routing/dht/ext_test.go @@ -202,7 +202,8 @@ func TestNotFound(t *testing.T) { } // long timeout to ensure timing is not at play. - ctx, _ = context.WithTimeout(ctx, time.Second*20) + ctx, cancel := context.WithTimeout(ctx, time.Second*20) + defer cancel() v, err := d.GetValue(ctx, key.Key("hello")) log.Debugf("get value got %v", v) if err != nil { @@ -274,7 +275,8 @@ func TestLessThanKResponses(t *testing.T) { }) } - ctx, _ = context.WithTimeout(ctx, time.Second*30) + ctx, cancel := context.WithTimeout(ctx, time.Second*30) + defer cancel() if _, err := d.GetValue(ctx, key.Key("hello")); err != nil { switch err { case routing.ErrNotFound: From fcf915fc4712566973cff2adac39e6f33ec56c84 Mon Sep 17 00:00:00 2001 From: rht Date: Sun, 23 Aug 2015 19:37:23 +0700 Subject: [PATCH 5/7] Define discoveryConnTimeout and set it to 30s License: MIT Signed-off-by: rht --- core/core.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/core.go b/core/core.go index 6a47c04f0c8..7eb19e9976c 100644 --- a/core/core.go +++ b/core/core.go @@ -39,8 +39,8 @@ import ( routing "github.com/ipfs/go-ipfs/routing" dht "github.com/ipfs/go-ipfs/routing/dht" kb "github.com/ipfs/go-ipfs/routing/kbucket" - offroute "github.com/ipfs/go-ipfs/routing/offline" nilrouting "github.com/ipfs/go-ipfs/routing/none" + offroute "github.com/ipfs/go-ipfs/routing/offline" bstore "github.com/ipfs/go-ipfs/blocks/blockstore" bserv "github.com/ipfs/go-ipfs/blockservice" @@ -63,6 +63,7 @@ import ( const IpnsValidatorTag = "ipns" const kSizeBlockstoreWriteCache = 100 const kReprovideFrequency = time.Hour * 12 +const discoveryConnTimeout = time.Second * 30 var log = eventlog.Logger("core") @@ -320,7 +321,7 @@ func setupDiscoveryOption(d config.Discovery) DiscoveryOption { func (n *IpfsNode) HandlePeerFound(p peer.PeerInfo) { log.Warning("trying peer info: ", p) - ctx, cancel := context.WithTimeout(n.Context(), time.Second*10) + ctx, cancel := context.WithTimeout(n.Context(), discoveryConnTimeout) defer cancel() if err := n.PeerHost.Connect(ctx, p); err != nil { log.Warning("Failed to connect to peer found by discovery: ", err) From 30e03b2da610b158ff061769080594dacc39df2b Mon Sep 17 00:00:00 2001 From: rht Date: Sun, 23 Aug 2015 19:55:45 +0700 Subject: [PATCH 6/7] Replace context.TODO in test files with context.Background License: MIT Signed-off-by: rht --- blockservice/test/blocks_test.go | 4 ++-- core/core_test.go | 2 +- core/coreunix/metadata_test.go | 5 +++-- exchange/bitswap/bitswap_test.go | 13 +++++++------ .../bitswap/notifications/notifications_test.go | 2 +- exchange/bitswap/testutils.go | 2 +- fuse/ipns/ipns_test.go | 2 +- importer/importer_test.go | 4 ++-- importer/trickle/trickle_test.go | 6 +++--- merkledag/merkledag_test.go | 12 ++++++------ merkledag/utils/utils_test.go | 2 +- p2p/net/swarm/swarm_test.go | 4 ++-- 12 files changed, 30 insertions(+), 28 deletions(-) diff --git a/blockservice/test/blocks_test.go b/blockservice/test/blocks_test.go index c94a2357e07..6ba5eb40ffb 100644 --- a/blockservice/test/blocks_test.go +++ b/blockservice/test/blocks_test.go @@ -42,7 +42,7 @@ func TestBlocks(t *testing.T) { t.Error("returned key is not equal to block key", err) } - ctx, cancel := context.WithTimeout(context.TODO(), time.Second*5) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() b2, err := bs.GetBlock(ctx, b.Key()) if err != nil { @@ -76,7 +76,7 @@ func TestGetBlocksSequential(t *testing.T) { t.Log("one instance at a time, get blocks concurrently") for i := 1; i < len(servs); i++ { - ctx, cancel := context.WithTimeout(context.TODO(), time.Second*50) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*50) defer cancel() out := servs[i].GetBlocks(ctx, keys) gotten := make(map[key.Key]*blocks.Block) diff --git a/core/core_test.go b/core/core_test.go index f4d2e5bfeec..23b46d36dd7 100644 --- a/core/core_test.go +++ b/core/core_test.go @@ -10,7 +10,7 @@ import ( ) func TestInitialization(t *testing.T) { - ctx := context.TODO() + ctx := context.Background() id := testIdentity good := []*config.Config{ diff --git a/core/coreunix/metadata_test.go b/core/coreunix/metadata_test.go index 2b38905395c..034cb7c89ef 100644 --- a/core/coreunix/metadata_test.go +++ b/core/coreunix/metadata_test.go @@ -30,6 +30,7 @@ func getDagserv(t *testing.T) merkledag.DAGService { } func TestMetadata(t *testing.T) { + ctx := context.Background() // Make some random node ds := getDagserv(t) data := make([]byte, 1000) @@ -64,12 +65,12 @@ func TestMetadata(t *testing.T) { t.Fatalf("something went wrong in conversion: '%s' != '%s'", rec.MimeType, m.MimeType) } - retnode, err := ds.Get(context.Background(), key.B58KeyDecode(mdk)) + retnode, err := ds.Get(ctx, key.B58KeyDecode(mdk)) if err != nil { t.Fatal(err) } - ndr, err := uio.NewDagReader(context.TODO(), retnode, ds) + ndr, err := uio.NewDagReader(ctx, retnode, ds) if err != nil { t.Fatal(err) } diff --git a/exchange/bitswap/bitswap_test.go b/exchange/bitswap/bitswap_test.go index 41f0e6c0841..8f4b6f61fe4 100644 --- a/exchange/bitswap/bitswap_test.go +++ b/exchange/bitswap/bitswap_test.go @@ -144,6 +144,7 @@ func TestLargeFileTwoPeers(t *testing.T) { } func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) { + ctx := context.Background() if testing.Short() { t.SkipNow() } @@ -161,7 +162,7 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) { first := instances[0] for _, b := range blocks { blkeys = append(blkeys, b.Key()) - first.Exchange.HasBlock(context.Background(), b) + first.Exchange.HasBlock(ctx, b) } t.Log("Distribute!") @@ -171,7 +172,7 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) { wg.Add(1) go func(inst Instance) { defer wg.Done() - outch, err := inst.Exchange.GetBlocks(context.TODO(), blkeys) + outch, err := inst.Exchange.GetBlocks(ctx, blkeys) if err != nil { t.Fatal(err) } @@ -228,7 +229,7 @@ func TestSendToWantingPeer(t *testing.T) { alpha := bg.Next() // peerA requests and waits for block alpha - ctx, cancel := context.WithTimeout(context.TODO(), waitTime) + ctx, cancel := context.WithTimeout(context.Background(), waitTime) defer cancel() alphaPromise, err := peerA.Exchange.GetBlocks(ctx, []key.Key{alpha.Key()}) if err != nil { @@ -236,7 +237,7 @@ func TestSendToWantingPeer(t *testing.T) { } // peerB announces to the network that he has block alpha - ctx, cancel = context.WithTimeout(context.TODO(), timeout) + ctx, cancel = context.WithTimeout(context.Background(), timeout) defer cancel() err = peerB.Exchange.HasBlock(ctx, alpha) if err != nil { @@ -265,12 +266,12 @@ func TestBasicBitswap(t *testing.T) { instances := sg.Instances(2) blocks := bg.Blocks(1) - err := instances[0].Exchange.HasBlock(context.TODO(), blocks[0]) + err := instances[0].Exchange.HasBlock(context.Background(), blocks[0]) if err != nil { t.Fatal(err) } - ctx, cancel := context.WithTimeout(context.TODO(), time.Second*5) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() blk, err := instances[1].Exchange.GetBlock(ctx, blocks[0].Key()) if err != nil { diff --git a/exchange/bitswap/notifications/notifications_test.go b/exchange/bitswap/notifications/notifications_test.go index 8ab9887ff3b..96ed1c4e3f0 100644 --- a/exchange/bitswap/notifications/notifications_test.go +++ b/exchange/bitswap/notifications/notifications_test.go @@ -103,7 +103,7 @@ func TestDuplicateSubscribe(t *testing.T) { func TestSubscribeIsANoopWhenCalledWithNoKeys(t *testing.T) { n := New() defer n.Shutdown() - ch := n.Subscribe(context.TODO()) // no keys provided + ch := n.Subscribe(context.Background()) // no keys provided if _, ok := <-ch; ok { t.Fatal("should be closed if no keys provided") } diff --git a/exchange/bitswap/testutils.go b/exchange/bitswap/testutils.go index 3dad2afed8c..5bf28036d7f 100644 --- a/exchange/bitswap/testutils.go +++ b/exchange/bitswap/testutils.go @@ -18,7 +18,7 @@ import ( // WARNING: this uses RandTestBogusIdentity DO NOT USE for NON TESTS! func NewTestSessionGenerator( net tn.Network) SessionGenerator { - ctx, cancel := context.WithCancel(context.TODO()) + ctx, cancel := context.WithCancel(context.Background()) return SessionGenerator{ net: net, seq: 0, diff --git a/fuse/ipns/ipns_test.go b/fuse/ipns/ipns_test.go index b7349793b9e..6f5fde0b493 100644 --- a/fuse/ipns/ipns_test.go +++ b/fuse/ipns/ipns_test.go @@ -106,7 +106,7 @@ func setupIpnsTest(t *testing.T, node *core.IpfsNode) (*core.IpfsNode, *fstest.M t.Fatal(err) } - ipnsfs, err := nsfs.NewFilesystem(context.TODO(), node.DAG, node.Namesys, node.Pinning, node.PrivateKey) + ipnsfs, err := nsfs.NewFilesystem(context.Background(), node.DAG, node.Namesys, node.Pinning, node.PrivateKey) if err != nil { t.Fatal(err) } diff --git a/importer/importer_test.go b/importer/importer_test.go index 4985ed92b0b..96b20341e1d 100644 --- a/importer/importer_test.go +++ b/importer/importer_test.go @@ -45,7 +45,7 @@ func TestBalancedDag(t *testing.T) { t.Fatal(err) } - dr, err := uio.NewDagReader(context.TODO(), nd, ds) + dr, err := uio.NewDagReader(context.Background(), nd, ds) if err != nil { t.Fatal(err) } @@ -102,7 +102,7 @@ func BenchmarkTrickleReadFull(b *testing.B) { func runReadBench(b *testing.B, nd *dag.Node, ds dag.DAGService) { for i := 0; i < b.N; i++ { - ctx, cancel := context.WithCancel(context.TODO()) + ctx, cancel := context.WithCancel(context.Background()) read, err := uio.NewDagReader(ctx, nd, ds) if err != nil { b.Fatal(err) diff --git a/importer/trickle/trickle_test.go b/importer/trickle/trickle_test.go index a64b1f4ec6d..b58acac97b9 100644 --- a/importer/trickle/trickle_test.go +++ b/importer/trickle/trickle_test.go @@ -443,7 +443,7 @@ func TestAppend(t *testing.T) { r := bytes.NewReader(should[nbytes/2:]) blks, errs := chunk.Chan(chunk.NewSizeSplitter(r, 500)) - ctx := context.TODO() + ctx := context.Background() nnode, err := TrickleAppend(ctx, nd, dbp.New(blks, errs)) if err != nil { t.Fatal(err) @@ -492,7 +492,7 @@ func TestMultipleAppends(t *testing.T) { spl := chunk.SizeSplitterGen(500) - ctx := context.TODO() + ctx := context.Background() for i := 0; i < len(should); i++ { blks, errs := chunk.Chan(spl(bytes.NewReader(should[i : i+1]))) @@ -540,7 +540,7 @@ func TestAppendSingleBytesToEmpty(t *testing.T) { blks, errs := chunk.Chan(spl(bytes.NewReader(data[:1]))) - ctx := context.TODO() + ctx := context.Background() nnode, err := TrickleAppend(ctx, nd, dbp.New(blks, errs)) if err != nil { t.Fatal(err) diff --git a/merkledag/merkledag_test.go b/merkledag/merkledag_test.go index 788041646a5..40bc457405a 100644 --- a/merkledag/merkledag_test.go +++ b/merkledag/merkledag_test.go @@ -155,6 +155,7 @@ func TestBatchFetchDupBlock(t *testing.T) { } func runBatchFetchTest(t *testing.T, read io.Reader) { + ctx := context.Background() var dagservs []DAGService for _, bsi := range bstest.Mocks(5) { dagservs = append(dagservs, NewDAGService(bsi)) @@ -169,7 +170,7 @@ func runBatchFetchTest(t *testing.T, read io.Reader) { t.Log("finished setup.") - dagr, err := uio.NewDagReader(context.TODO(), root, dagservs[0]) + dagr, err := uio.NewDagReader(ctx, root, dagservs[0]) if err != nil { t.Fatal(err) } @@ -196,13 +197,13 @@ func runBatchFetchTest(t *testing.T, read io.Reader) { wg.Add(1) go func(i int) { defer wg.Done() - first, err := dagservs[i].Get(context.Background(), k) + first, err := dagservs[i].Get(ctx, k) if err != nil { t.Fatal(err) } fmt.Println("Got first node back.") - read, err := uio.NewDagReader(context.TODO(), first, dagservs[i]) + read, err := uio.NewDagReader(ctx, first, dagservs[i]) if err != nil { t.Fatal(err) } @@ -266,8 +267,7 @@ func assertCanGet(t *testing.T, ds DAGService, n *Node) { t.Fatal(err) } - _, err = ds.Get(context.TODO(), k) - if err != nil { + if _, err := ds.Get(context.Background(), k); err != nil { t.Fatal(err) } } @@ -281,7 +281,7 @@ func TestCantGet(t *testing.T) { t.Fatal(err) } - _, err = dsp.ds.Get(context.TODO(), k) + _, err = dsp.ds.Get(context.Background(), k) if !strings.Contains(err.Error(), "not found") { t.Fatal("expected err not found, got: ", err) } diff --git a/merkledag/utils/utils_test.go b/merkledag/utils/utils_test.go index b49958d15ed..b9164126743 100644 --- a/merkledag/utils/utils_test.go +++ b/merkledag/utils/utils_test.go @@ -104,7 +104,7 @@ func testInsert(t *testing.T, e *Editor, path, data string, create bool, experr } } - err = e.InsertNodeAtPath(context.TODO(), path, ck, c) + err = e.InsertNodeAtPath(context.Background(), path, ck, c) if experr != "" { var got string if err != nil { diff --git a/p2p/net/swarm/swarm_test.go b/p2p/net/swarm/swarm_test.go index 6e53cc339ed..9193db0109a 100644 --- a/p2p/net/swarm/swarm_test.go +++ b/p2p/net/swarm/swarm_test.go @@ -288,13 +288,13 @@ func TestAddrBlocking(t *testing.T) { swarms[1].Filters.AddDialFilter(block) swarms[1].peers.AddAddr(swarms[0].LocalPeer(), swarms[0].ListenAddresses()[0], peer.PermanentAddrTTL) - _, err = swarms[1].Dial(context.TODO(), swarms[0].LocalPeer()) + _, err = swarms[1].Dial(ctx, swarms[0].LocalPeer()) if err == nil { t.Fatal("dial should have failed") } swarms[0].peers.AddAddr(swarms[1].LocalPeer(), swarms[1].ListenAddresses()[0], peer.PermanentAddrTTL) - _, err = swarms[0].Dial(context.TODO(), swarms[1].LocalPeer()) + _, err = swarms[0].Dial(ctx, swarms[1].LocalPeer()) if err == nil { t.Fatal("dial should have failed") } From dc2153574cc69dbf519f718369eba3a609799c2f Mon Sep 17 00:00:00 2001 From: rht Date: Sun, 23 Aug 2015 19:57:44 +0700 Subject: [PATCH 7/7] Remove ctx timeout in unixfsNode GetChild License: MIT Signed-off-by: rht --- importer/helpers/helpers.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/importer/helpers/helpers.go b/importer/helpers/helpers.go index ac7d3cd8a6e..cb8422126e6 100644 --- a/importer/helpers/helpers.go +++ b/importer/helpers/helpers.go @@ -2,7 +2,6 @@ package helpers import ( "fmt" - "time" "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" key "github.com/ipfs/go-ipfs/blocks/key" @@ -78,9 +77,6 @@ func (n *UnixfsNode) NumChildren() int { } func (n *UnixfsNode) GetChild(ctx context.Context, i int, ds dag.DAGService) (*UnixfsNode, error) { - ctx, cancel := context.WithTimeout(ctx, time.Minute) - defer cancel() - nd, err := n.node.Links[i].GetNode(ctx, ds) if err != nil { return nil, err