Commit c3ce1319 authored by Jeromy's avatar Jeromy

simplify provideCollector

parent 6e6c6638
...@@ -83,17 +83,7 @@ func (bs *Bitswap) provideCollector(ctx context.Context) { ...@@ -83,17 +83,7 @@ func (bs *Bitswap) provideCollector(ctx context.Context) {
defer close(bs.provideKeys) defer close(bs.provideKeys)
var toprovide []u.Key var toprovide []u.Key
var nextKey u.Key var nextKey u.Key
var keysOut chan u.Key
select {
case blk, ok := <-bs.newBlocks:
if !ok {
log.Debug("newBlocks channel closed")
return
}
nextKey = blk.Key()
case <-ctx.Done():
return
}
for { for {
select { select {
...@@ -102,21 +92,18 @@ func (bs *Bitswap) provideCollector(ctx context.Context) { ...@@ -102,21 +92,18 @@ func (bs *Bitswap) provideCollector(ctx context.Context) {
log.Debug("newBlocks channel closed") log.Debug("newBlocks channel closed")
return return
} }
if keysOut == nil {
nextKey = blk.Key()
keysOut = bs.provideKeys
} else {
toprovide = append(toprovide, blk.Key()) toprovide = append(toprovide, blk.Key())
case bs.provideKeys <- nextKey: }
case keysOut <- nextKey:
if len(toprovide) > 0 { if len(toprovide) > 0 {
nextKey = toprovide[0] nextKey = toprovide[0]
toprovide = toprovide[1:] toprovide = toprovide[1:]
} else { } else {
select { keysOut = nil
case blk, ok := <-bs.newBlocks:
if !ok {
return
}
nextKey = blk.Key()
case <-ctx.Done():
return
}
} }
case <-ctx.Done(): case <-ctx.Done():
return return
......
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