github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/p2p/iterator.go (about) 1 package p2p 2 3 import ( 4 "context" 5 6 "github.com/ethereum/go-ethereum/p2p/enode" 7 ) 8 9 // filterNodes wraps an iterator such that Next only returns nodes for which 10 // the 'check' function returns true. This custom implementation also 11 // checks for context deadlines so that in the event the parent context has 12 // expired, we do exit from the search rather than perform more network 13 // lookups for additional peers. 14 func filterNodes(ctx context.Context, it enode.Iterator, check func(*enode.Node) bool) enode.Iterator { 15 return &filterIter{ctx, it, check} 16 } 17 18 type filterIter struct { 19 context.Context 20 enode.Iterator 21 check func(*enode.Node) bool 22 } 23 24 // Next looks up for the next valid node according to our 25 // filter criteria. 26 func (f *filterIter) Next() bool { 27 for f.Iterator.Next() { 28 if f.Context.Err() != nil { 29 return false 30 } 31 if f.check(f.Node()) { 32 return true 33 } 34 } 35 return false 36 }