Skip to content

Commit a965316

Browse files
committed
Merge pull request #1585 from rht/cleanup-context
Make sure ctx in commands are derived from req.Context
2 parents a052087 + de5c0ce commit a965316

40 files changed

+139
-213
lines changed

assets/assets.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func addAssetList(nd *core.IpfsNode, l []string) (*key.Key, error) {
5454

5555
fname := filepath.Base(p)
5656
k := key.B58KeyDecode(s)
57-
if err := dirb.AddChild(fname, k); err != nil {
57+
if err := dirb.AddChild(nd.Context(), fname, k); err != nil {
5858
return nil, fmt.Errorf("assets: could not add '%s' as a child: %s", fname, err)
5959
}
6060
}

blockservice/test/blocks_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ func TestBlocks(t *testing.T) {
4242
t.Error("returned key is not equal to block key", err)
4343
}
4444

45-
ctx, _ := context.WithTimeout(context.TODO(), time.Second*5)
45+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
46+
defer cancel()
4647
b2, err := bs.GetBlock(ctx, b.Key())
4748
if err != nil {
4849
t.Error("failed to retrieve block from BlockService", err)
@@ -75,7 +76,8 @@ func TestGetBlocksSequential(t *testing.T) {
7576
t.Log("one instance at a time, get blocks concurrently")
7677

7778
for i := 1; i < len(servs); i++ {
78-
ctx, _ := context.WithTimeout(context.TODO(), time.Second*50)
79+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*50)
80+
defer cancel()
7981
out := servs[i].GetBlocks(ctx, keys)
8082
gotten := make(map[key.Key]*blocks.Block)
8183
for blk := range out {

commands/http/client.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
5151

5252
if req.Context() == nil {
5353
log.Warningf("no context set in request")
54-
err := req.SetRootContext(context.TODO())
55-
if err != nil {
54+
if err := req.SetRootContext(context.TODO()); err != nil {
5655
return nil, err
5756
}
5857
}

core/bootstrap.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ func Bootstrap(n *IpfsNode, cfg BootstrapConfig) (io.Closer, error) {
110110

111111
func bootstrapRound(ctx context.Context, host host.Host, cfg BootstrapConfig) error {
112112

113-
ctx, _ = context.WithTimeout(ctx, cfg.ConnectionTimeout)
113+
ctx, cancel := context.WithTimeout(ctx, cfg.ConnectionTimeout)
114+
defer cancel()
114115
id := host.ID()
115116

116117
// get bootstrap peers from config. retrieving them here makes

core/commands/ls.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import (
55
"fmt"
66
"io"
77
"text/tabwriter"
8-
"time"
9-
10-
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
118

129
cmds "github.com/ipfs/go-ipfs/commands"
1310
core "github.com/ipfs/go-ipfs/core"
@@ -81,9 +78,7 @@ it contains, with the following format:
8178
Links: make([]LsLink, len(dagnode.Links)),
8279
}
8380
for j, link := range dagnode.Links {
84-
ctx, cancel := context.WithTimeout(req.Context(), time.Minute)
85-
defer cancel()
86-
link.Node, err = link.GetNode(ctx, node.DAG)
81+
link.Node, err = link.GetNode(req.Context(), node.DAG)
8782
if err != nil {
8883
res.SetError(err, cmds.ErrNormal)
8984
return

core/commands/object.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ import (
99
"io/ioutil"
1010
"strings"
1111
"text/tabwriter"
12-
"time"
1312

1413
mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
15-
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
1614

1715
key "github.com/ipfs/go-ipfs/blocks/key"
1816
cmds "github.com/ipfs/go-ipfs/commands"
@@ -480,14 +478,11 @@ resulting object hash.
480478
return
481479
}
482480

483-
ctx, cancel := context.WithTimeout(req.Context(), time.Second*30)
484-
rnode, err := nd.DAG.Get(ctx, rhash)
481+
rnode, err := nd.DAG.Get(req.Context(), rhash)
485482
if err != nil {
486483
res.SetError(err, cmds.ErrNormal)
487-
cancel()
488484
return
489485
}
490-
cancel()
491486

492487
action := req.Arguments()[1]
493488

core/commands/swarm.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
1717
mafilter "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/whyrusleeping/multiaddr-filter"
18-
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
1918
)
2019

2120
type stringList struct {
@@ -211,7 +210,7 @@ ipfs swarm connect /ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3
211210
cmds.StringArg("address", true, true, "address of peer to connect to").EnableStdin(),
212211
},
213212
Run: func(req cmds.Request, res cmds.Response) {
214-
ctx := context.TODO()
213+
ctx := req.Context()
215214

216215
n, err := req.InvocContext().GetNode()
217216
if err != nil {

core/commands/unixfs/ls.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import (
66
"io"
77
"sort"
88
"text/tabwriter"
9-
"time"
10-
11-
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
129

1310
cmds "github.com/ipfs/go-ipfs/commands"
1411
core "github.com/ipfs/go-ipfs/core"
@@ -111,9 +108,7 @@ size is the IPFS link size.
111108
links := make([]LsLink, len(merkleNode.Links))
112109
output.Objects[hash].Links = links
113110
for i, link := range merkleNode.Links {
114-
getCtx, cancel := context.WithTimeout(ctx, time.Minute)
115-
defer cancel()
116-
link.Node, err = link.GetNode(getCtx, node.DAG)
111+
link.Node, err = link.GetNode(ctx, node.DAG)
117112
if err != nil {
118113
res.SetError(err, cmds.ErrNormal)
119114
return

core/core.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ import (
3939
routing "github.com/ipfs/go-ipfs/routing"
4040
dht "github.com/ipfs/go-ipfs/routing/dht"
4141
kb "github.com/ipfs/go-ipfs/routing/kbucket"
42-
offroute "github.com/ipfs/go-ipfs/routing/offline"
4342
nilrouting "github.com/ipfs/go-ipfs/routing/none"
43+
offroute "github.com/ipfs/go-ipfs/routing/offline"
4444

4545
bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
4646
bserv "github.com/ipfs/go-ipfs/blockservice"
@@ -63,6 +63,7 @@ import (
6363
const IpnsValidatorTag = "ipns"
6464
const kSizeBlockstoreWriteCache = 100
6565
const kReprovideFrequency = time.Hour * 12
66+
const discoveryConnTimeout = time.Second * 30
6667

6768
var log = eventlog.Logger("core")
6869

@@ -320,9 +321,9 @@ func setupDiscoveryOption(d config.Discovery) DiscoveryOption {
320321

321322
func (n *IpfsNode) HandlePeerFound(p peer.PeerInfo) {
322323
log.Warning("trying peer info: ", p)
323-
ctx, _ := context.WithTimeout(context.TODO(), time.Second*10)
324-
err := n.PeerHost.Connect(ctx, p)
325-
if err != nil {
324+
ctx, cancel := context.WithTimeout(n.Context(), discoveryConnTimeout)
325+
defer cancel()
326+
if err := n.PeerHost.Connect(ctx, p); err != nil {
326327
log.Warning("Failed to connect to peer found by discovery: ", err)
327328
}
328329
}
@@ -367,6 +368,9 @@ func (n *IpfsNode) Close() error {
367368

368369
// Context returns the IpfsNode context
369370
func (n *IpfsNode) Context() context.Context {
371+
if n.ctx == nil {
372+
n.ctx = context.TODO()
373+
}
370374
return n.ctx
371375
}
372376

core/core_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func TestInitialization(t *testing.T) {
13-
ctx := context.TODO()
13+
ctx := context.Background()
1414
id := testIdentity
1515

1616
good := []*config.Config{

core/corehttp/gateway_handler.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
367367
}
368368
}
369369

370-
err = i.node.DAG.AddRecursive(newnode)
371-
if err != nil {
370+
if err := i.node.DAG.AddRecursive(newnode); err != nil {
372371
webError(w, "Could not add recursively new node", err, http.StatusInternalServerError)
373372
return
374373
}
@@ -439,8 +438,7 @@ func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
439438
}
440439
}
441440

442-
err = i.node.DAG.AddRecursive(newnode)
443-
if err != nil {
441+
if err := i.node.DAG.AddRecursive(newnode); err != nil {
444442
webError(w, "Could not add recursively new node", err, http.StatusInternalServerError)
445443
return
446444
}

core/corerepo/pinning.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ package corerepo
1515

1616
import (
1717
"fmt"
18-
"time"
1918

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

@@ -42,7 +41,7 @@ func Pin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool)
4241
return nil, err
4342
}
4443

45-
ctx, cancel := context.WithTimeout(ctx, time.Minute)
44+
ctx, cancel := context.WithCancel(ctx)
4645
defer cancel()
4746
err = n.Pinning.Pin(ctx, dagnode, recursive)
4847
if err != nil {
@@ -74,7 +73,7 @@ func Unpin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool
7473
for _, dagnode := range dagnodes {
7574
k, _ := dagnode.Key()
7675

77-
ctx, cancel := context.WithTimeout(ctx, time.Minute)
76+
ctx, cancel := context.WithCancel(ctx)
7877
defer cancel()
7978
err := n.Pinning.Unpin(ctx, k, recursive)
8079
if err != nil {

core/coreunix/add.go

+9-28
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"io/ioutil"
66
"os"
77
gopath "path"
8-
"time"
98

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

@@ -66,8 +65,7 @@ func AddR(n *core.IpfsNode, root string) (key string, err error) {
6665
}
6766

6867
n.Pinning.GetManual().RemovePinWithMode(k, pin.Indirect)
69-
err = n.Pinning.Flush()
70-
if err != nil {
68+
if err := n.Pinning.Flush(); err != nil {
7169
return "", err
7270
}
7371

@@ -95,43 +93,28 @@ func AddWrapped(n *core.IpfsNode, r io.Reader, filename string) (string, *merkle
9593
func add(n *core.IpfsNode, reader io.Reader) (*merkledag.Node, error) {
9694
mp := n.Pinning.GetManual()
9795

98-
node, err := importer.BuildDagFromReader(
96+
return importer.BuildDagFromReader(
9997
n.DAG,
10098
chunk.DefaultSplitter(reader),
10199
importer.PinIndirectCB(mp),
102100
)
103-
if err != nil {
104-
return nil, err
105-
}
106-
107-
return node, nil
108101
}
109102

110103
func addNode(n *core.IpfsNode, node *merkledag.Node) error {
111-
err := n.DAG.AddRecursive(node) // add the file to the graph + local storage
112-
if err != nil {
104+
if err := n.DAG.AddRecursive(node); err != nil { // add the file to the graph + local storage
113105
return err
114106
}
115-
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
107+
ctx, cancel := context.WithCancel(n.Context())
116108
defer cancel()
117-
err = n.Pinning.Pin(ctx, node, true) // ensure we keep it
118-
if err != nil {
119-
return err
120-
}
121-
return nil
109+
err := n.Pinning.Pin(ctx, node, true) // ensure we keep it
110+
return err
122111
}
123112

124113
func addFile(n *core.IpfsNode, file files.File) (*merkledag.Node, error) {
125114
if file.IsDirectory() {
126115
return addDir(n, file)
127116
}
128-
129-
dagnode, err := add(n, file)
130-
if err != nil {
131-
return nil, err
132-
}
133-
134-
return dagnode, nil
117+
return add(n, file)
135118
}
136119

137120
func addDir(n *core.IpfsNode, dir files.File) (*merkledag.Node, error) {
@@ -155,14 +138,12 @@ Loop:
155138

156139
_, name := gopath.Split(file.FileName())
157140

158-
err = tree.AddNodeLink(name, node)
159-
if err != nil {
141+
if err := tree.AddNodeLink(name, node); err != nil {
160142
return nil, err
161143
}
162144
}
163145

164-
err := addNode(n, tree)
165-
if err != nil {
146+
if err := addNode(n, tree); err != nil {
166147
return nil, err
167148
}
168149
return tree, nil

core/coreunix/metadata.go

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package coreunix
22

33
import (
4-
"time"
5-
6-
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
7-
84
key "github.com/ipfs/go-ipfs/blocks/key"
95
core "github.com/ipfs/go-ipfs/core"
106
dag "github.com/ipfs/go-ipfs/merkledag"
@@ -14,9 +10,7 @@ import (
1410
func AddMetadataTo(n *core.IpfsNode, skey string, m *ft.Metadata) (string, error) {
1511
ukey := key.B58KeyDecode(skey)
1612

17-
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
18-
defer cancel()
19-
nd, err := n.DAG.Get(ctx, ukey)
13+
nd, err := n.DAG.Get(n.Context(), ukey)
2014
if err != nil {
2115
return "", err
2216
}
@@ -28,8 +22,7 @@ func AddMetadataTo(n *core.IpfsNode, skey string, m *ft.Metadata) (string, error
2822
}
2923

3024
mdnode.Data = mdata
31-
err = mdnode.AddNodeLinkClean("file", nd)
32-
if err != nil {
25+
if err := mdnode.AddNodeLinkClean("file", nd); err != nil {
3326
return "", err
3427
}
3528

@@ -44,9 +37,7 @@ func AddMetadataTo(n *core.IpfsNode, skey string, m *ft.Metadata) (string, error
4437
func Metadata(n *core.IpfsNode, skey string) (*ft.Metadata, error) {
4538
ukey := key.B58KeyDecode(skey)
4639

47-
ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
48-
defer cancel()
49-
nd, err := n.DAG.Get(ctx, ukey)
40+
nd, err := n.DAG.Get(n.Context(), ukey)
5041
if err != nil {
5142
return nil, err
5243
}

core/coreunix/metadata_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func getDagserv(t *testing.T) merkledag.DAGService {
3030
}
3131

3232
func TestMetadata(t *testing.T) {
33+
ctx := context.Background()
3334
// Make some random node
3435
ds := getDagserv(t)
3536
data := make([]byte, 1000)
@@ -64,12 +65,12 @@ func TestMetadata(t *testing.T) {
6465
t.Fatalf("something went wrong in conversion: '%s' != '%s'", rec.MimeType, m.MimeType)
6566
}
6667

67-
retnode, err := ds.Get(context.Background(), key.B58KeyDecode(mdk))
68+
retnode, err := ds.Get(ctx, key.B58KeyDecode(mdk))
6869
if err != nil {
6970
t.Fatal(err)
7071
}
7172

72-
ndr, err := uio.NewDagReader(context.TODO(), retnode, ds)
73+
ndr, err := uio.NewDagReader(ctx, retnode, ds)
7374
if err != nil {
7475
t.Fatal(err)
7576
}

diagnostics/diag.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ func (d *Diagnostics) HandleMessage(ctx context.Context, s inet.Stream) error {
298298
if timeout < HopTimeoutDecrement {
299299
return fmt.Errorf("timeout too short: %s", timeout)
300300
}
301-
ctx, _ = context.WithTimeout(ctx, timeout)
301+
ctx, cancel := context.WithTimeout(ctx, timeout)
302+
defer cancel()
302303
pmes.SetTimeoutDuration(timeout - HopTimeoutDecrement)
303304

304305
dpeers, err := d.getDiagnosticFromPeers(ctx, d.getPeers(), pmes)

0 commit comments

Comments
 (0)