mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 06:06:44 +00:00
p2p/enode: unexport asyncFilterIter, bufferIter
This commit is contained in:
parent
8d8047b1b1
commit
945d890350
1 changed files with 13 additions and 12 deletions
|
|
@ -153,9 +153,9 @@ func (f *filterIter) Next() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// AsyncFilterIter wraps an iterator such that Next only returns nodes for which
|
// asyncFilterIter wraps an iterator such that Next only returns nodes for which
|
||||||
// the 'check' function returns a (possibly modified) node.
|
// the 'check' function returns a (possibly modified) node.
|
||||||
type AsyncFilterIter struct {
|
type asyncFilterIter struct {
|
||||||
it Iterator // the iterator to filter
|
it Iterator // the iterator to filter
|
||||||
slots chan struct{} // the slots for parallel checking
|
slots chan struct{} // the slots for parallel checking
|
||||||
passed chan *Node // channel to collect passed nodes
|
passed chan *Node // channel to collect passed nodes
|
||||||
|
|
@ -163,11 +163,12 @@ type AsyncFilterIter struct {
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
closeOnce sync.Once
|
closeOnce sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
type AsyncFilterFunc func(context.Context, *Node) *Node
|
type AsyncFilterFunc func(context.Context, *Node) *Node
|
||||||
|
|
||||||
// AsyncFilter creates an iterator which checks nodes in parallel.
|
// AsyncFilter creates an iterator which checks nodes in parallel.
|
||||||
func AsyncFilter(it Iterator, check AsyncFilterFunc, workers int) Iterator {
|
func AsyncFilter(it Iterator, check AsyncFilterFunc, workers int) Iterator {
|
||||||
f := &AsyncFilterIter{
|
f := &asyncFilterIter{
|
||||||
it: it,
|
it: it,
|
||||||
slots: make(chan struct{}, workers+1),
|
slots: make(chan struct{}, workers+1),
|
||||||
passed: make(chan *Node),
|
passed: make(chan *Node),
|
||||||
|
|
@ -209,18 +210,18 @@ func AsyncFilter(it Iterator, check AsyncFilterFunc, workers int) Iterator {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next blocks until a node is available or the iterator is closed.
|
// Next blocks until a node is available or the iterator is closed.
|
||||||
func (f *AsyncFilterIter) Next() bool {
|
func (f *asyncFilterIter) Next() bool {
|
||||||
f.buffer = <-f.passed
|
f.buffer = <-f.passed
|
||||||
return f.buffer != nil
|
return f.buffer != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Node returns the current node.
|
// Node returns the current node.
|
||||||
func (f *AsyncFilterIter) Node() *Node {
|
func (f *asyncFilterIter) Node() *Node {
|
||||||
return f.buffer
|
return f.buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close ends the iterator, also closing the wrapped iterator.
|
// Close ends the iterator, also closing the wrapped iterator.
|
||||||
func (f *AsyncFilterIter) Close() {
|
func (f *asyncFilterIter) Close() {
|
||||||
f.closeOnce.Do(func() {
|
f.closeOnce.Do(func() {
|
||||||
f.it.Close()
|
f.it.Close()
|
||||||
f.cancel()
|
f.cancel()
|
||||||
|
|
@ -232,9 +233,9 @@ func (f *AsyncFilterIter) Close() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// BufferIter wraps an iterator and buffers the nodes it returns.
|
// bufferIter wraps an iterator and buffers the nodes it returns.
|
||||||
// The buffer is pre-filled with the given size from the wrapped iterator.
|
// The buffer is pre-filled with the given size from the wrapped iterator.
|
||||||
type BufferIter struct {
|
type bufferIter struct {
|
||||||
it Iterator
|
it Iterator
|
||||||
buffer chan *Node
|
buffer chan *Node
|
||||||
head *Node
|
head *Node
|
||||||
|
|
@ -243,7 +244,7 @@ type BufferIter struct {
|
||||||
|
|
||||||
// NewBufferIter creates a new pre-fetch buffer of a given size.
|
// NewBufferIter creates a new pre-fetch buffer of a given size.
|
||||||
func NewBufferIter(it Iterator, size int) Iterator {
|
func NewBufferIter(it Iterator, size int) Iterator {
|
||||||
b := BufferIter{
|
b := bufferIter{
|
||||||
it: it,
|
it: it,
|
||||||
buffer: make(chan *Node, size),
|
buffer: make(chan *Node, size),
|
||||||
}
|
}
|
||||||
|
|
@ -259,16 +260,16 @@ func NewBufferIter(it Iterator, size int) Iterator {
|
||||||
return &b
|
return &b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BufferIter) Next() bool {
|
func (b *bufferIter) Next() bool {
|
||||||
b.head = <-b.buffer
|
b.head = <-b.buffer
|
||||||
return b.head != nil
|
return b.head != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BufferIter) Node() *Node {
|
func (b *bufferIter) Node() *Node {
|
||||||
return b.head
|
return b.head
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BufferIter) Close() {
|
func (b *bufferIter) Close() {
|
||||||
b.closeOnce.Do(func() {
|
b.closeOnce.Do(func() {
|
||||||
b.it.Close()
|
b.it.Close()
|
||||||
// Wait for Next to terminate.
|
// Wait for Next to terminate.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue