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
// the returned channel.
// NB: No guarantees are made about order.
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() {
var toFetch []u.Key
defer close(out)
var misses []u.Key
for _, k := range ks {
block, err := s.Blockstore.Get(k)
hit, err := s.Blockstore.Get(k)
if err != nil {
toFetch = append(toFetch, k)
misses = append(misses, k)
continue
}
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 {
log.Errorf("Error with GetBlocks: %s", err)
return
}
for blk := range nblocks {
out <- blk
for b := range rblocks {
select {
case out <- b:
case <-ctx.Done():
return
}
}
close(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