1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-30 18:13:54 +08:00

simplify provideCollector

This commit is contained in:
Jeromy
2015-03-05 16:27:47 -08:00
parent 8937f5fbda
commit bfee4894b4

View File

@ -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) {
log.Debug("newBlocks channel closed") log.Debug("newBlocks channel closed")
return return
} }
toprovide = append(toprovide, blk.Key()) if keysOut == nil {
case bs.provideKeys <- nextKey: nextKey = blk.Key()
keysOut = bs.provideKeys
} else {
toprovide = append(toprovide, blk.Key())
}
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