mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 14:46:42 +00:00
eth/enode: simplify BufferIter
Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
parent
fab2459f13
commit
c29688a255
1 changed files with 13 additions and 22 deletions
|
|
@ -239,7 +239,7 @@ type BufferIter struct {
|
|||
buffer chan *Node
|
||||
head *Node
|
||||
closed chan struct{}
|
||||
mu sync.Mutex
|
||||
closeOnce sync.Once
|
||||
}
|
||||
|
||||
// NewBufferIter creates a new pre-fetch buffer of a given size.
|
||||
|
|
@ -267,35 +267,26 @@ func NewBufferIter(it Iterator, size int) Iterator {
|
|||
}
|
||||
|
||||
func (b *BufferIter) Next() bool {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
select {
|
||||
case b.head = <-b.buffer:
|
||||
case <-b.closed:
|
||||
return false
|
||||
b.head = nil
|
||||
}
|
||||
return b.head != nil
|
||||
}
|
||||
|
||||
func (b *BufferIter) Node() *Node {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
return b.head
|
||||
}
|
||||
|
||||
func (b *BufferIter) Close() {
|
||||
// Close the wrapped iterator first.
|
||||
b.closeOnce.Do(func() {
|
||||
b.it.Close()
|
||||
close(b.closed)
|
||||
// Wait for Next to terminate, then drain the buffer.
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
// Wait for Next to terminate.
|
||||
for range b.buffer {
|
||||
}
|
||||
b.buffer = nil
|
||||
b.head = nil
|
||||
b.it = nil
|
||||
})
|
||||
}
|
||||
|
||||
// FairMix aggregates multiple node iterators. The mixer itself is an iterator which ends
|
||||
|
|
|
|||
Loading…
Reference in a new issue