From 42770cc39a080358b31349278f617ed12d210b6b Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow <brian.holderchow@gmail.com> Date: Thu, 18 Sep 2014 19:36:18 -0700 Subject: [PATCH] refac(exchange) replace timeout -> context in API --- blockservice/blockservice.go | 8 +++++--- exchange/bitswap/bitswap.go | 15 +++++---------- exchange/bitswap/offline.go | 5 +++-- exchange/bitswap/offline_test.go | 5 +++-- exchange/interface.go | 4 ++-- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/blockservice/blockservice.go b/blockservice/blockservice.go index 89136edb..3018ae0d 100644 --- a/blockservice/blockservice.go +++ b/blockservice/blockservice.go @@ -4,12 +4,13 @@ import ( "fmt" "time" + context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go" + mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" + blocks "github.com/jbenet/go-ipfs/blocks" exchange "github.com/jbenet/go-ipfs/exchange" u "github.com/jbenet/go-ipfs/util" - - mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" ) // BlockService is a block datastore. @@ -65,7 +66,8 @@ func (s *BlockService) GetBlock(k u.Key) (*blocks.Block, error) { }, nil } else if err == ds.ErrNotFound && s.Remote != nil { u.DOut("Blockservice: Searching bitswap.\n") - blk, err := s.Remote.Block(k, time.Second*5) + ctx, _ := context.WithTimeout(context.TODO(), 5*time.Second) + blk, err := s.Remote.Block(ctx, k) if err != nil { return nil, err } diff --git a/exchange/bitswap/bitswap.go b/exchange/bitswap/bitswap.go index d47c9614..173da67e 100644 --- a/exchange/bitswap/bitswap.go +++ b/exchange/bitswap/bitswap.go @@ -2,7 +2,6 @@ package bitswap import ( "errors" - "time" context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go" @@ -65,18 +64,14 @@ type bitswap struct { strategy strategy.Strategy } -// GetBlock attempts to retrieve a particular block from peers, within timeout. -func (bs *bitswap) Block(k u.Key, timeout time.Duration) ( +// GetBlock attempts to retrieve a particular block from peers within the +// deadline enforced by the context +func (bs *bitswap) Block(ctx context.Context, k u.Key) ( *blocks.Block, error) { - ctx, _ := context.WithTimeout(context.Background(), timeout) - // TODO replace timeout with ctx in routing interface - begin := time.Now() - tleft := timeout - time.Now().Sub(begin) provs_ch := bs.routing.FindProvidersAsync(ctx, k, 20) blockChannel := make(chan blocks.Block) - after := time.After(tleft) // TODO: when the data is received, shut down this for loop ASAP go func() { @@ -98,8 +93,8 @@ func (bs *bitswap) Block(k u.Key, timeout time.Duration) ( case block := <-blockChannel: close(blockChannel) return &block, nil - case <-after: - return nil, u.ErrTimeout + case <-ctx.Done(): + return nil, ctx.Err() } } diff --git a/exchange/bitswap/offline.go b/exchange/bitswap/offline.go index a8dbd0f8..e35cce2f 100644 --- a/exchange/bitswap/offline.go +++ b/exchange/bitswap/offline.go @@ -2,7 +2,8 @@ package bitswap import ( "errors" - "time" + + context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" blocks "github.com/jbenet/go-ipfs/blocks" exchange "github.com/jbenet/go-ipfs/exchange" @@ -21,7 +22,7 @@ type offlineExchange struct { // Block returns nil to signal that a block could not be retrieved for the // given key. // NB: This function may return before the timeout expires. -func (_ *offlineExchange) Block(k u.Key, timeout time.Duration) (*blocks.Block, error) { +func (_ *offlineExchange) Block(context.Context, u.Key) (*blocks.Block, error) { return nil, errors.New("Block unavailable. Operating in offline mode") } diff --git a/exchange/bitswap/offline_test.go b/exchange/bitswap/offline_test.go index 2b40ac5e..19b040cd 100644 --- a/exchange/bitswap/offline_test.go +++ b/exchange/bitswap/offline_test.go @@ -2,7 +2,8 @@ package bitswap import ( "testing" - "time" + + context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" u "github.com/jbenet/go-ipfs/util" testutil "github.com/jbenet/go-ipfs/util/testutil" @@ -10,7 +11,7 @@ import ( func TestBlockReturnsErr(t *testing.T) { off := NewOfflineExchange() - _, err := off.Block(u.Key("foo"), time.Second) + _, err := off.Block(context.TODO(), u.Key("foo")) if err != nil { return // as desired } diff --git a/exchange/interface.go b/exchange/interface.go index 75eca06b..7e06e1ed 100644 --- a/exchange/interface.go +++ b/exchange/interface.go @@ -1,7 +1,7 @@ package bitswap import ( - "time" + context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" blocks "github.com/jbenet/go-ipfs/blocks" u "github.com/jbenet/go-ipfs/util" @@ -13,7 +13,7 @@ type Interface interface { // Block returns the block associated with a given key. // TODO(brian): pass a context instead of a timeout - Block(k u.Key, timeout time.Duration) (*blocks.Block, error) + Block(context.Context, u.Key) (*blocks.Block, error) // HasBlock asserts the existence of this block // TODO(brian): rename -> HasBlock -- GitLab