Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gpe dc2153574cc69dbf519f718369eba3a609799c2f #1600

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
6 changes: 4 additions & 2 deletions blockservice/test/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.Background(), time.Second*5)
defer cancel()
b2, err := bs.GetBlock(ctx, b.Key())
if err != nil {
t.Error("failed to retrieve block from BlockService", err)
Expand Down Expand Up @@ -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.Background(), time.Second*50)
defer cancel()
out := servs[i].GetBlocks(ctx, keys)
gotten := make(map[key.Key]*blocks.Block)
for blk := range out {
Expand Down
3 changes: 1 addition & 2 deletions commands/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
3 changes: 2 additions & 1 deletion core/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions core/commands/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"text/tabwriter"
"time"

context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"

Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions core/commands/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions core/commands/unixfs/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"sort"
"text/tabwriter"
"time"

context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"

Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 8 additions & 4 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")

Expand Down Expand Up @@ -320,9 +321,9 @@ 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)
err := n.PeerHost.Connect(ctx, p)
if err != nil {
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)
}
}
Expand Down Expand Up @@ -367,6 +368,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
}

Expand Down
2 changes: 1 addition & 1 deletion core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestInitialization(t *testing.T) {
ctx := context.TODO()
ctx := context.Background()
id := testIdentity

good := []*config.Config{
Expand Down
5 changes: 2 additions & 3 deletions core/corerepo/pinning.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package corerepo

import (
"fmt"
"time"

context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"

Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
37 changes: 9 additions & 28 deletions core/coreunix/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -66,8 +65,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
}

Expand Down Expand Up @@ -95,43 +93,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.WithCancel(n.Context())
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) {
Expand All @@ -155,14 +138,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
Expand Down
6 changes: 2 additions & 4 deletions core/coreunix/metadata.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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(context.TODO(), time.Minute)
ctx, cancel := context.WithCancel(n.Context())
defer cancel()
nd, err := n.DAG.Get(ctx, ukey)
if err != nil {
Expand Down Expand Up @@ -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(context.TODO(), time.Minute)
ctx, cancel := context.WithCancel(n.Context())
defer cancel()
nd, err := n.DAG.Get(ctx, ukey)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions core/coreunix/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion diagnostics/diag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading