github.com/amazechain/amc@v0.1.3/internal/p2p/iterator.go (about)

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