Commit a2a4327b authored by Jeromy's avatar Jeromy

use @maybebtc's ForwardBlocks function

parent f1eb07d9
...@@ -16,6 +16,7 @@ import ( ...@@ -16,6 +16,7 @@ import (
strategy "github.com/jbenet/go-ipfs/exchange/bitswap/strategy" strategy "github.com/jbenet/go-ipfs/exchange/bitswap/strategy"
peer "github.com/jbenet/go-ipfs/peer" peer "github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util" u "github.com/jbenet/go-ipfs/util"
async "github.com/jbenet/go-ipfs/util/async"
"github.com/jbenet/go-ipfs/util/eventlog" "github.com/jbenet/go-ipfs/util/eventlog"
) )
...@@ -128,35 +129,12 @@ func (bs *bitswap) GetBlocks(ctx context.Context, keys []u.Key) (<-chan *blocks. ...@@ -128,35 +129,12 @@ func (bs *bitswap) GetBlocks(ctx context.Context, keys []u.Key) (<-chan *blocks.
promise := bs.notifications.Subscribe(ctx, keys...) promise := bs.notifications.Subscribe(ctx, keys...)
select { select {
case bs.batchRequests <- keys: case bs.batchRequests <- keys:
return pipeBlocks(ctx, promise, len(keys)), nil return async.ForwardN(ctx, promise, len(keys)), nil
case <-ctx.Done(): case <-ctx.Done():
return nil, ctx.Err() return nil, ctx.Err()
} }
} }
func pipeBlocks(ctx context.Context, in <-chan *blocks.Block, count int) <-chan *blocks.Block {
out := make(chan *blocks.Block, 1)
go func() {
defer close(out)
for i := 0; i < count; i++ {
select {
case blk, ok := <-in:
if !ok {
return
}
select {
case out <- blk:
case <-ctx.Done():
return
}
case <-ctx.Done():
return
}
}
}()
return out
}
func (bs *bitswap) sendWantListTo(ctx context.Context, peers <-chan peer.Peer) error { func (bs *bitswap) sendWantListTo(ctx context.Context, peers <-chan peer.Peer) error {
if peers == nil { if peers == nil {
panic("Cant send wantlist to nil peerchan") panic("Cant send wantlist to nil peerchan")
......
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