Commit bc186b26 authored by Jeromy's avatar Jeromy

parallelize block processing

parent 89c950aa
...@@ -279,14 +279,22 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg ...@@ -279,14 +279,22 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
// quickly send out cancels, reduces chances of duplicate block receives // quickly send out cancels, reduces chances of duplicate block receives
var keys []u.Key var keys []u.Key
for _, block := range iblocks { for _, block := range iblocks {
if _, found := bs.wm.wl.Contains(block.Key()); !found {
log.Notice("received un-asked-for block: %s", block)
continue
}
keys = append(keys, block.Key()) keys = append(keys, block.Key())
} }
bs.wm.CancelWants(keys) bs.wm.CancelWants(keys)
wg := sync.WaitGroup{}
for _, block := range iblocks { for _, block := range iblocks {
wg.Add(1)
go func(b *blocks.Block) {
defer wg.Done()
bs.counterLk.Lock() bs.counterLk.Lock()
bs.blocksRecvd++ bs.blocksRecvd++
has, err := bs.blockstore.Has(block.Key()) has, err := bs.blockstore.Has(b.Key())
if err == nil && has { if err == nil && has {
bs.dupBlocksRecvd++ bs.dupBlocksRecvd++
} }
...@@ -294,24 +302,18 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg ...@@ -294,24 +302,18 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
bdup := bs.dupBlocksRecvd bdup := bs.dupBlocksRecvd
bs.counterLk.Unlock() bs.counterLk.Unlock()
if has { if has {
continue return
}
// put this after the duplicate check as a block not on our wantlist may
// have already been received.
if _, found := bs.wm.wl.Contains(block.Key()); !found {
log.Notice("received un-asked-for block: %s", block)
continue
} }
log.Infof("got block %s from %s (%d,%d)", block, p, brecvd, bdup) log.Debugf("got block %s from %s (%d,%d)", b, p, brecvd, bdup)
hasBlockCtx, cancel := context.WithTimeout(ctx, hasBlockTimeout) hasBlockCtx, cancel := context.WithTimeout(ctx, hasBlockTimeout)
if err := bs.HasBlock(hasBlockCtx, block); err != nil { if err := bs.HasBlock(hasBlockCtx, b); err != nil {
log.Warningf("ReceiveMessage HasBlock error: %s", err) log.Warningf("ReceiveMessage HasBlock error: %s", err)
} }
cancel() cancel()
}(block)
} }
wg.Wait()
} }
// Connected/Disconnected warns bitswap about peer connections // Connected/Disconnected warns bitswap about peer connections
......
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