added cancel func calls previously ignored
Juan Batiz-Benet committed
Mar 7, 2015 at 09:31 UTC
1d5b9036119748d3c8596a6af3dcad4a0371d606
10 files changed
+27
-13
core/commands/ping.go
+4
-2
@@ -126,7 +126,8 @@ func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int)
126
Text: fmt.Sprintf("Looking up peer %s", pid.Pretty()),
127
}
128
129
- ctx, _ := context.WithTimeout(ctx, kPingTimeout)
129
+ ctx, cancel := context.WithTimeout(ctx, kPingTimeout)
130
+ defer cancel()
131
p, err := n.Routing.FindPeer(ctx, pid)
132
if err != nil {
133
outChan <- &PingResult{Text: fmt.Sprintf("Peer lookup error: %s", err)}
@@ -147,7 +148,8 @@ func pingPeer(ctx context.Context, n *core.IpfsNode, pid peer.ID, numPings int)
148
default:
149
}
150
150
- ctx, _ := context.WithTimeout(ctx, kPingTimeout)
151
+ ctx, cancel := context.WithTimeout(ctx, kPingTimeout)
152
+ defer cancel()
153
took, err := n.Routing.Ping(ctx, pid)
154
if err != nil {
155
log.Debugf("Ping error: %s", err)
core/corenet/net.go
+2
-1
@@ -54,7 +54,8 @@ func Listen(nd *core.IpfsNode, protocol string) (*ipfsListener, error) {
54
}
55
56
func Dial(nd *core.IpfsNode, p peer.ID, protocol string) (net.Stream, error) {
57
- ctx, _ := context.WithTimeout(nd.Context(), time.Second*30)
57
+ ctx, cancel := context.WithTimeout(nd.Context(), time.Second*30)
58
+ defer cancel()
59
err := nd.PeerHost.Connect(ctx, peer.PeerInfo{ID: p})
60
if err != nil {
61
return nil, err
diagnostics/diag.go
+3
-2
@@ -14,12 +14,12 @@ import (
14
15
ggio "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/gogoprotobuf/io"
16
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
17
- ctxutil "github.com/jbenet/go-ipfs/util/ctx"
17
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
18
host "github.com/jbenet/go-ipfs/p2p/host"
19
inet "github.com/jbenet/go-ipfs/p2p/net"
20
peer "github.com/jbenet/go-ipfs/p2p/peer"
21
protocol "github.com/jbenet/go-ipfs/p2p/protocol"
22
+ ctxutil "github.com/jbenet/go-ipfs/util/ctx"
23
24
pb "github.com/jbenet/go-ipfs/diagnostics/internal/pb"
25
util "github.com/jbenet/go-ipfs/util"
@@ -138,7 +138,8 @@ func newID() string {
138
// GetDiagnostic runs a diagnostics request across the entire network
139
func (d *Diagnostics) GetDiagnostic(timeout time.Duration) ([]*DiagInfo, error) {
140
log.Debug("Getting diagnostic.")
141
- ctx, _ := context.WithTimeout(context.TODO(), timeout)
141
+ ctx, cancel := context.WithTimeout(context.TODO(), timeout)
142
+ defer cancel()
143
144
diagID := newID()
145
d.diagLock.Lock()
exchange/bitswap/bitswap.go
+4
-2
@@ -269,7 +269,8 @@ func (bs *Bitswap) sendWantlistToProviders(ctx context.Context, entries []wantli
269
go func(k u.Key) {
270
defer wg.Done()
271
272
- child, _ := context.WithTimeout(ctx, providerRequestTimeout)
272
+ child, cancel := context.WithTimeout(ctx, providerRequestTimeout)
273
+ defer cancel()
274
providers := bs.network.FindProvidersAsync(child, k, maxProvidersPerRequest)
275
for prov := range providers {
276
sendToPeers <- prov
@@ -311,10 +312,11 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
312
// Should only track *useful* messages in ledger
313
314
for _, block := range incoming.Blocks() {
314
- hasBlockCtx, _ := context.WithTimeout(ctx, hasBlockTimeout)
315
+ hasBlockCtx, cancel := context.WithTimeout(ctx, hasBlockTimeout)
316
if err := bs.HasBlock(hasBlockCtx, block); err != nil {
317
log.Debug(err)
318
}
319
+ cancel()
320
}
321
322
var keys []u.Key
merkledag/merkledag.go
+2
-1
@@ -88,7 +88,8 @@ func (n *dagService) Get(k u.Key) (*Node, error) {
88
return nil, fmt.Errorf("dagService is nil")
89
}
90
91
- ctx, _ := context.WithTimeout(context.TODO(), time.Minute)
91
+ ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
92
+ defer cancel()
93
// we shouldn't use an arbitrary timeout here.
94
// since Get doesnt take in a context yet, we give a large upper bound.
95
// think of an http request. we want it to go on as long as the client requests it.
namesys/publisher.go
+3
-1
@@ -60,9 +60,11 @@ func (p *ipnsPublisher) Publish(ctx context.Context, k ci.PrivKey, value u.Key)
60
nameb := u.Hash(pkbytes)
61
namekey := u.Key("/pk/" + string(nameb))
62
63
+ timectx, cancel := context.WithDeadline(ctx, time.Now().Add(time.Second*10))
64
+ defer cancel()
65
+
66
log.Debugf("Storing pubkey at: %s", namekey)
67
// Store associated public key
65
- timectx, _ := context.WithDeadline(ctx, time.Now().Add(time.Second*10))
68
err = p.routing.PutValue(timectx, namekey, pkbytes)
69
if err != nil {
70
return err
p2p/net/swarm/swarm_dial.go
+2
-1
@@ -227,8 +227,9 @@ func (s *Swarm) gatedDialAttempt(ctx context.Context, p peer.ID) (*Conn, error)
227
// if it succeeds, dial will add the conn to the swarm itself.
228
229
defer log.EventBegin(ctx, "swarmDialAttemptStart", logdial).Done()
230
- ctxT, _ := context.WithTimeout(ctx, s.dialT)
230
+ ctxT, cancel := context.WithTimeout(ctx, s.dialT)
231
conn, err := s.dial(ctxT, p)
232
+ cancel()
233
s.dsync.Unlock(p)
234
log.Debugf("dial end %s", conn)
235
if err != nil {
pin/pin.go
+3
-1
@@ -172,7 +172,9 @@ func (p *pinner) pinIndirectRecurse(node *mdag.Node) error {
172
}
173
174
func (p *pinner) pinLinks(node *mdag.Node) error {
175
- ctx, _ := context.WithTimeout(context.Background(), time.Second*60)
175
+ ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
176
+ defer cancel()
177
+
178
for _, ng := range p.dserv.GetDAG(ctx, node) {
179
subnode, err := ng.Get()
180
if err != nil {
routing/dht/dht.go
+2
-1
@@ -357,11 +357,12 @@ func (dht *IpfsDHT) PingRoutine(t time.Duration) {
357
rand.Read(id)
358
peers := dht.routingTable.NearestPeers(kb.ConvertKey(u.Key(id)), 5)
359
for _, p := range peers {
360
- ctx, _ := context.WithTimeout(dht.Context(), time.Second*5)
360
+ ctx, cancel := context.WithTimeout(dht.Context(), time.Second*5)
361
_, err := dht.Ping(ctx, p)
362
if err != nil {
363
log.Debugf("Ping error: %s", err)
364
}
365
+ cancel()
366
}
367
case <-dht.Closing():
368
return
unixfs/tar/reader.go
+2
-1
@@ -86,7 +86,8 @@ func (r *Reader) writeToBuf(dagnode *mdag.Node, path string, depth int) {
86
}
87
r.flush()
88
89
- ctx, _ := context.WithTimeout(context.TODO(), time.Second*60)
89
+ ctx, cancel := context.WithTimeout(context.TODO(), time.Second*60)
90
+ defer cancel()
91
92
for i, ng := range r.dag.GetDAG(ctx, dagnode) {
93
childNode, err := ng.Get()