Commit 75d10001 authored by Brian Tiger Chow's avatar Brian Tiger Chow Committed by Jeromy

fix(blockservice) respect context in GetBlocks

License: MIT
Signed-off-by: default avatarBrian Tiger Chow <brian@perfmode.com>
parent 10090dc7
...@@ -99,29 +99,37 @@ func (s *BlockService) GetBlock(ctx context.Context, k u.Key) (*blocks.Block, er ...@@ -99,29 +99,37 @@ func (s *BlockService) GetBlock(ctx context.Context, k u.Key) (*blocks.Block, er
// the returned channel. // the returned channel.
// NB: No guarantees are made about order. // NB: No guarantees are made about order.
func (s *BlockService) GetBlocks(ctx context.Context, ks []u.Key) <-chan *blocks.Block { func (s *BlockService) GetBlocks(ctx context.Context, ks []u.Key) <-chan *blocks.Block {
out := make(chan *blocks.Block, 32) out := make(chan *blocks.Block, 0)
go func() { go func() {
var toFetch []u.Key defer close(out)
var misses []u.Key
for _, k := range ks { for _, k := range ks {
block, err := s.Blockstore.Get(k) hit, err := s.Blockstore.Get(k)
if err != nil { if err != nil {
toFetch = append(toFetch, k) misses = append(misses, k)
continue continue
} }
log.Debug("Blockservice: Got data in datastore.") log.Debug("Blockservice: Got data in datastore.")
out <- block select {
case out <- hit:
case <-ctx.Done():
return
}
} }
nblocks, err := s.Remote.GetBlocks(ctx, toFetch) rblocks, err := s.Remote.GetBlocks(ctx, misses)
if err != nil { if err != nil {
log.Errorf("Error with GetBlocks: %s", err) log.Errorf("Error with GetBlocks: %s", err)
return return
} }
for blk := range nblocks { for b := range rblocks {
out <- blk select {
case out <- b:
case <-ctx.Done():
return
}
} }
close(out)
}() }()
return out return out
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment