Skip to content

Commit

Permalink
Merge pull request #3105 from ipfs/feat/bitswap/dup-prov-cache
Browse files Browse the repository at this point in the history
bitswap: don't re-provide blocks we've provided very recently
  • Loading branch information
whyrusleeping authored Aug 25, 2016
2 parents a2bba21 + 582e5de commit 5a8b79b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
28 changes: 25 additions & 3 deletions blockservice/blockservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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")
}
Expand Down
1 change: 1 addition & 0 deletions exchange/bitswap/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5a8b79b

Please sign in to comment.