Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions p2p/enode/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ type AsyncFilterFunc func(context.Context, *Node) *Node
func AsyncFilter(it Iterator, check AsyncFilterFunc, workers int) Iterator {
f := &asyncFilterIter{
it: ensureSourceIter(it),
slots: make(chan struct{}, workers+1),
slots: make(chan struct{}, workers+1), // extra 1 slot to make sure all the goroutines can be completed
passed: make(chan iteratorItem),
}
for range cap(f.slots) {
Expand All @@ -193,6 +193,9 @@ func AsyncFilter(it Iterator, check AsyncFilterFunc, workers int) Iterator {
return
case <-f.slots:
}
defer func() {
f.slots <- struct{}{} // the iterator has ended
}()
// read from the iterator and start checking nodes in parallel
// when a node is checked, it will be sent to the passed channel
// and the slot will be released
Expand All @@ -201,7 +204,11 @@ func AsyncFilter(it Iterator, check AsyncFilterFunc, workers int) Iterator {
nodeSource := f.it.NodeSource()

// check the node async, in a separate goroutine
<-f.slots
select {
case <-ctx.Done():
return
case <-f.slots:
}
go func() {
if nn := check(ctx, node); nn != nil {
item := iteratorItem{nn, nodeSource}
Expand All @@ -213,8 +220,6 @@ func AsyncFilter(it Iterator, check AsyncFilterFunc, workers int) Iterator {
f.slots <- struct{}{}
}()
}
// the iterator has ended
f.slots <- struct{}{}
}()

return f
Expand Down
Loading