Skip to content

Commit

Permalink
refactor: rewrite getBlocks batch loop to be clearer
Browse files Browse the repository at this point in the history
This commit was moved from ipfs/go-blockservice@30ca6aa
  • Loading branch information
Jorropo committed Jul 28, 2022
1 parent 41de1a5 commit 00087bb
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions blockservice/blockservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,23 +333,22 @@ func getBlocks(ctx context.Context, ks []cid.Cid, bs blockstore.Blockstore, fget
return
}

for b := range rblocks {
// batch available blocks together
batch := make([]blocks.Block, 0, 8)
batch = append(batch, b)
logger.Debugf("BlockService.BlockFetched %s", b.Cid())

// batch available blocks together
const batchSize = 32
batch := make([]blocks.Block, 0, batchSize)
for {
var noMoreBlocks bool
batchLoop:
for {
for len(batch) < batchSize {
select {
case moreBlock, ok := <-rblocks:
case b, ok := <-rblocks:
if !ok {
// rblock has been closed, we set it to nil to avoid pulling zero values
rblocks = nil
} else {
logger.Debugf("BlockService.BlockFetched %s", moreBlock.Cid())
batch = append(batch, moreBlock)
noMoreBlocks = true
break batchLoop
}

logger.Debugf("BlockService.BlockFetched %s", b.Cid())
batch = append(batch, b)
case <-ctx.Done():
return
default:
Expand All @@ -370,13 +369,17 @@ func getBlocks(ctx context.Context, ks []cid.Cid, bs blockstore.Blockstore, fget
return
}

for _, b = range batch {
for _, b := range batch {
select {
case out <- b:
case <-ctx.Done():
return
}
}
batch = batch[:0]
if noMoreBlocks {
break
}
}
}()
return out
Expand Down

0 comments on commit 00087bb

Please sign in to comment.