From e907b2e03ccf6ac9f4d828e97fea866e7c3b22c0 Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow <brian.holderchow@gmail.com> Date: Thu, 18 Sep 2014 19:43:03 -0700 Subject: [PATCH] feat(exchange) pass ctx to exchange.HasBlock(...) --- blockservice/blockservice.go | 3 ++- exchange/bitswap/bitswap.go | 3 +-- exchange/bitswap/offline.go | 2 +- exchange/bitswap/offline_test.go | 4 ++-- exchange/interface.go | 10 +++------- 5 files changed, 9 insertions(+), 13 deletions(-) diff --git a/blockservice/blockservice.go b/blockservice/blockservice.go index 3018ae0d..e3c7402b 100644 --- a/blockservice/blockservice.go +++ b/blockservice/blockservice.go @@ -43,7 +43,8 @@ func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) { return k, err } if s.Remote != nil { - err = s.Remote.HasBlock(*b) + ctx := context.TODO() + err = s.Remote.HasBlock(ctx, *b) } return k, err } diff --git a/exchange/bitswap/bitswap.go b/exchange/bitswap/bitswap.go index 62ff1cd2..35a1a90b 100644 --- a/exchange/bitswap/bitswap.go +++ b/exchange/bitswap/bitswap.go @@ -128,8 +128,7 @@ func (bs *bitswap) sendToPeersThatWant(ctx context.Context, block blocks.Block) // HasBlock announces the existance of a block to bitswap, potentially sending // it to peers (Partners) whose WantLists include it. -func (bs *bitswap) HasBlock(blk blocks.Block) error { - ctx := context.TODO() +func (bs *bitswap) HasBlock(ctx context.Context, blk blocks.Block) error { go bs.sendToPeersThatWant(ctx, blk) return bs.routing.Provide(blk.Key()) } diff --git a/exchange/bitswap/offline.go b/exchange/bitswap/offline.go index e35cce2f..9695b0b5 100644 --- a/exchange/bitswap/offline.go +++ b/exchange/bitswap/offline.go @@ -27,6 +27,6 @@ func (_ *offlineExchange) Block(context.Context, u.Key) (*blocks.Block, error) { } // HasBlock always returns nil. -func (_ *offlineExchange) HasBlock(blocks.Block) error { +func (_ *offlineExchange) HasBlock(context.Context, blocks.Block) error { return nil } diff --git a/exchange/bitswap/offline_test.go b/exchange/bitswap/offline_test.go index 19b040cd..26821f2c 100644 --- a/exchange/bitswap/offline_test.go +++ b/exchange/bitswap/offline_test.go @@ -11,7 +11,7 @@ import ( func TestBlockReturnsErr(t *testing.T) { off := NewOfflineExchange() - _, err := off.Block(context.TODO(), u.Key("foo")) + _, err := off.Block(context.Background(), u.Key("foo")) if err != nil { return // as desired } @@ -21,7 +21,7 @@ func TestBlockReturnsErr(t *testing.T) { func TestHasBlockReturnsNil(t *testing.T) { off := NewOfflineExchange() block := testutil.NewBlockOrFail(t, "data") - err := off.HasBlock(block) + err := off.HasBlock(context.Background(), block) if err != nil { t.Fatal("") } diff --git a/exchange/interface.go b/exchange/interface.go index 7e06e1ed..a96094ea 100644 --- a/exchange/interface.go +++ b/exchange/interface.go @@ -12,13 +12,9 @@ import ( type Interface interface { // Block returns the block associated with a given key. - // TODO(brian): pass a context instead of a timeout Block(context.Context, u.Key) (*blocks.Block, error) - // HasBlock asserts the existence of this block - // TODO(brian): rename -> HasBlock - // TODO(brian): accept a value, not a pointer - // TODO(brian): remove error return value. Should callers be concerned with - // whether the block was made available on the network? - HasBlock(blocks.Block) error + // TODO Should callers be concerned with whether the block was made + // available on the network? + HasBlock(context.Context, blocks.Block) error } -- GitLab