Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
delay finding providers
Browse files Browse the repository at this point in the history
It's expensive and causes quite a bit of dialing. Let's give bitswap a second to
work it's magic before we try this.

fixes #16
  • Loading branch information
Stebalien committed Oct 22, 2018
1 parent e12de92 commit 4ac28bc
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
// results.
// TODO: if a 'non-nice' strategy is implemented, consider increasing this value
maxProvidersPerRequest = 3
findProviderDelay = 1 * time.Second
providerRequestTimeout = time.Second * 10
provideTimeout = time.Second * 15
sizeBatchRequestChan = 32
Expand Down Expand Up @@ -230,14 +231,6 @@ func (bs *Bitswap) GetBlocks(ctx context.Context, keys []cid.Cid) (<-chan blocks

bs.wm.WantBlocks(ctx, keys, nil, mses)

// NB: Optimization. Assumes that providers of key[0] are likely to
// be able to provide for all keys. This currently holds true in most
// every situation. Later, this assumption may not hold as true.
req := &blockRequest{
Cid: keys[0],
Ctx: ctx,
}

remaining := cid.NewSet()
for _, k := range keys {
remaining.Add(k)
Expand All @@ -252,12 +245,19 @@ func (bs *Bitswap) GetBlocks(ctx context.Context, keys []cid.Cid) (<-chan blocks
// can't just defer this call on its own, arguments are resolved *when* the defer is created
bs.CancelWants(remaining.Keys(), mses)
}()
findProvsDelay := time.NewTimer(findProviderDelay)
defer findProvsDelay.Stop()
for {
select {
case blk, ok := <-promise:
if !ok {
return
}
findProvsDelay.Stop()
select {
case <-findProvsDelay.C:
default:
}

bs.CancelWants([]cid.Cid{blk.Cid()}, mses)
remaining.Remove(blk.Cid())
Expand All @@ -266,18 +266,21 @@ func (bs *Bitswap) GetBlocks(ctx context.Context, keys []cid.Cid) (<-chan blocks
case <-ctx.Done():
return
}
case <-findProvsDelay.C:
// NB: Optimization. Assumes that providers of key[0] are likely to
// be able to provide for all keys. This currently holds true in most
// every situation. Later, this assumption may not hold as true.
bs.findKeys <- &blockRequest{
Cid: keys[0],
Ctx: ctx,
}
case <-ctx.Done():
return
}
}
}()

select {
case bs.findKeys <- req:
return out, nil
case <-ctx.Done():
return nil, ctx.Err()
}
return out, nil
}

func (bs *Bitswap) getNextSessionID() uint64 {
Expand Down

0 comments on commit 4ac28bc

Please sign in to comment.