Commit 5a8b79b2 authored by Jeromy Johnson's avatar Jeromy Johnson Committed by GitHub

Merge pull request #3105 from ipfs/feat/bitswap/dup-prov-cache

bitswap: don't re-provide blocks we've provided very recently
parents a2bba212 582e5dee
......@@ -43,7 +43,15 @@ func New(bs blockstore.Blockstore, rem exchange.Interface) *BlockService {
// TODO pass a context into this if the remote.HasBlock is going to remain here.
func (s *BlockService) AddBlock(b blocks.Block) (key.Key, error) {
k := b.Key()
err := s.Blockstore.Put(b)
has, err := s.Blockstore.Has(k)
if err != nil {
return k, err
}
if has {
return k, nil
}
err = s.Blockstore.Put(b)
if err != nil {
return k, err
}
......@@ -54,13 +62,27 @@ func (s *BlockService) AddBlock(b blocks.Block) (key.Key, error) {
}
func (s *BlockService) AddBlocks(bs []blocks.Block) ([]key.Key, error) {
err := s.Blockstore.PutMany(bs)
var toput []blocks.Block
for _, b := range bs {
has, err := s.Blockstore.Has(b.Key())
if err != nil {
return nil, err
}
if has {
continue
}
toput = append(toput, b)
}
err := s.Blockstore.PutMany(toput)
if err != nil {
return nil, err
}
var ks []key.Key
for _, b := range bs {
for _, b := range toput {
if err := s.Exchange.HasBlock(b); err != nil {
return nil, errors.New("blockservice is closed")
}
......
......@@ -133,6 +133,7 @@ func (bs *Bitswap) provideCollector(ctx context.Context) {
log.Debug("newBlocks channel closed")
return
}
if keysOut == nil {
nextKey = blk.Key()
keysOut = bs.provideKeys
......
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