Commit 93de01c2 authored by Steven Allen's avatar Steven Allen

delay finding providers

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
parent e12de92f
...@@ -36,6 +36,7 @@ const ( ...@@ -36,6 +36,7 @@ const (
// results. // results.
// TODO: if a 'non-nice' strategy is implemented, consider increasing this value // TODO: if a 'non-nice' strategy is implemented, consider increasing this value
maxProvidersPerRequest = 3 maxProvidersPerRequest = 3
findProviderDelay = 1 * time.Second
providerRequestTimeout = time.Second * 10 providerRequestTimeout = time.Second * 10
provideTimeout = time.Second * 15 provideTimeout = time.Second * 15
sizeBatchRequestChan = 32 sizeBatchRequestChan = 32
...@@ -230,14 +231,6 @@ func (bs *Bitswap) GetBlocks(ctx context.Context, keys []cid.Cid) (<-chan blocks ...@@ -230,14 +231,6 @@ func (bs *Bitswap) GetBlocks(ctx context.Context, keys []cid.Cid) (<-chan blocks
bs.wm.WantBlocks(ctx, keys, nil, mses) 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() remaining := cid.NewSet()
for _, k := range keys { for _, k := range keys {
remaining.Add(k) remaining.Add(k)
...@@ -252,13 +245,37 @@ func (bs *Bitswap) GetBlocks(ctx context.Context, keys []cid.Cid) (<-chan blocks ...@@ -252,13 +245,37 @@ 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 // can't just defer this call on its own, arguments are resolved *when* the defer is created
bs.CancelWants(remaining.Keys(), mses) bs.CancelWants(remaining.Keys(), mses)
}() }()
findProvsDelay := time.NewTimer(findProviderDelay)
defer findProvsDelay.Stop()
findProvsDelayCh := findProvsDelay.C
req := &blockRequest{
Cid: keys[0],
Ctx: ctx,
}
var findProvsReqCh chan<- *blockRequest
for { for {
select { select {
case <-findProvsDelayCh:
// 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.
findProvsReqCh = bs.findKeys
findProvsDelayCh = nil
case findProvsReqCh <- req:
findProvsReqCh = nil
case blk, ok := <-promise: case blk, ok := <-promise:
if !ok { if !ok {
return return
} }
// No need to find providers now.
findProvsDelay.Stop()
findProvsDelayCh = nil
findProvsReqCh = nil
bs.CancelWants([]cid.Cid{blk.Cid()}, mses) bs.CancelWants([]cid.Cid{blk.Cid()}, mses)
remaining.Remove(blk.Cid()) remaining.Remove(blk.Cid())
select { select {
...@@ -272,12 +289,7 @@ func (bs *Bitswap) GetBlocks(ctx context.Context, keys []cid.Cid) (<-chan blocks ...@@ -272,12 +289,7 @@ func (bs *Bitswap) GetBlocks(ctx context.Context, keys []cid.Cid) (<-chan blocks
} }
}() }()
select { return out, nil
case bs.findKeys <- req:
return out, nil
case <-ctx.Done():
return nil, ctx.Err()
}
} }
func (bs *Bitswap) getNextSessionID() uint64 { func (bs *Bitswap) getNextSessionID() uint64 {
......
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