refac(exchange) replace timeout -> context in API
Brian Tiger Chow committed
Sep 18, 2014 at 19:36 UTC
42770cc39a080358b31349278f617ed12d210b6b
5 files changed
+18
-19
blockservice/blockservice.go
+5
-3
@@ -4,12 +4,13 @@ import (
4
"fmt"
5
"time"
6
7
+ context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
8
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
9
+ mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
10
+
11
blocks "github.com/jbenet/go-ipfs/blocks"
12
exchange "github.com/jbenet/go-ipfs/exchange"
13
u "github.com/jbenet/go-ipfs/util"
11
-
12
- mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
14
)
15
16
// BlockService is a block datastore.
@@ -65,7 +66,8 @@ func (s *BlockService) GetBlock(k u.Key) (*blocks.Block, error) {
66
}, nil
67
} else if err == ds.ErrNotFound && s.Remote != nil {
68
u.DOut("Blockservice: Searching bitswap.\n")
68
- blk, err := s.Remote.Block(k, time.Second*5)
69
+ ctx, _ := context.WithTimeout(context.TODO(), 5*time.Second)
70
+ blk, err := s.Remote.Block(ctx, k)
71
if err != nil {
72
return nil, err
73
}
exchange/bitswap/bitswap.go
+5
-10
@@ -2,7 +2,6 @@ package bitswap
2
3
import (
4
"errors"
5
- "time"
5
6
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
7
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
@@ -65,18 +64,14 @@ type bitswap struct {
64
strategy strategy.Strategy
65
}
66
68
-// GetBlock attempts to retrieve a particular block from peers, within timeout.
69
-func (bs *bitswap) Block(k u.Key, timeout time.Duration) (
67
+// GetBlock attempts to retrieve a particular block from peers within the
68
+// deadline enforced by the context
69
+func (bs *bitswap) Block(ctx context.Context, k u.Key) (
70
*blocks.Block, error) {
71
- ctx, _ := context.WithTimeout(context.Background(), timeout)
71
73
- // TODO replace timeout with ctx in routing interface
74
- begin := time.Now()
75
- tleft := timeout - time.Now().Sub(begin)
72
provs_ch := bs.routing.FindProvidersAsync(ctx, k, 20)
73
74
blockChannel := make(chan blocks.Block)
79
- after := time.After(tleft)
75
76
// TODO: when the data is received, shut down this for loop ASAP
77
go func() {
@@ -98,8 +93,8 @@ func (bs *bitswap) Block(k u.Key, timeout time.Duration) (
93
case block := <-blockChannel:
94
close(blockChannel)
95
return &block, nil
101
- case <-after:
102
- return nil, u.ErrTimeout
96
+ case <-ctx.Done():
97
+ return nil, ctx.Err()
98
}
99
}
100
exchange/bitswap/offline.go
+3
-2
@@ -2,7 +2,8 @@ package bitswap
2
3
import (
4
"errors"
5
- "time"
5
+
6
+ context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
7
8
blocks "github.com/jbenet/go-ipfs/blocks"
9
exchange "github.com/jbenet/go-ipfs/exchange"
@@ -21,7 +22,7 @@ type offlineExchange struct {
22
// Block returns nil to signal that a block could not be retrieved for the
23
// given key.
24
// NB: This function may return before the timeout expires.
24
-func (_ *offlineExchange) Block(k u.Key, timeout time.Duration) (*blocks.Block, error) {
25
+func (_ *offlineExchange) Block(context.Context, u.Key) (*blocks.Block, error) {
26
return nil, errors.New("Block unavailable. Operating in offline mode")
27
}
28
exchange/bitswap/offline_test.go
+3
-2
@@ -2,7 +2,8 @@ package bitswap
2
3
import (
4
"testing"
5
- "time"
5
+
6
+ context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
7
8
u "github.com/jbenet/go-ipfs/util"
9
testutil "github.com/jbenet/go-ipfs/util/testutil"
@@ -10,7 +11,7 @@ import (
11
12
func TestBlockReturnsErr(t *testing.T) {
13
off := NewOfflineExchange()
13
- _, err := off.Block(u.Key("foo"), time.Second)
14
+ _, err := off.Block(context.TODO(), u.Key("foo"))
15
if err != nil {
16
return // as desired
17
}
exchange/interface.go
+2
-2
@@ -1,7 +1,7 @@
1
package bitswap
2
3
import (
4
- "time"
4
+ context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
5
6
blocks "github.com/jbenet/go-ipfs/blocks"
7
u "github.com/jbenet/go-ipfs/util"
@@ -13,7 +13,7 @@ type Interface interface {
13
14
// Block returns the block associated with a given key.
15
// TODO(brian): pass a context instead of a timeout
16
- Block(k u.Key, timeout time.Duration) (*blocks.Block, error)
16
+ Block(context.Context, u.Key) (*blocks.Block, error)
17
18
// HasBlock asserts the existence of this block
19
// TODO(brian): rename -> HasBlock