Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bitswap: don't re-provide blocks we've provided very recently #3105

Merged
merged 1 commit into from
Aug 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@whyrusleeping, this change modifies the return value so that only the keys that don't already exist are returned. Was this intentional?

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