github.com/HugePages/go-ethereum@v1.9.7/p2p/enode/iter.go (about)

     1  // Copyright 2019 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package enode
    18  
    19  import (
    20  	"sync"
    21  	"time"
    22  )
    23  
    24  // Iterator represents a sequence of nodes. The Next method moves to the next node in the
    25  // sequence. It returns false when the sequence has ended or the iterator is closed. Close
    26  // may be called concurrently with Next and Node, and interrupts Next if it is blocked.
    27  type Iterator interface {
    28  	Next() bool  // moves to next node
    29  	Node() *Node // returns current node
    30  	Close()      // ends the iterator
    31  }
    32  
    33  // ReadNodes reads at most n nodes from the given iterator. The return value contains no
    34  // duplicates and no nil values. To prevent looping indefinitely for small repeating node
    35  // sequences, this function calls Next at most n times.
    36  func ReadNodes(it Iterator, n int) []*Node {
    37  	seen := make(map[ID]*Node, n)
    38  	for i := 0; i < n && it.Next(); i++ {
    39  		// Remove duplicates, keeping the node with higher seq.
    40  		node := it.Node()
    41  		prevNode, ok := seen[node.ID()]
    42  		if ok && prevNode.Seq() > node.Seq() {
    43  			continue
    44  		}
    45  		seen[node.ID()] = node
    46  	}
    47  	result := make([]*Node, 0, len(seen))
    48  	for _, node := range seen {
    49  		result = append(result, node)
    50  	}
    51  	return result
    52  }
    53  
    54  // IterNodes makes an iterator which runs through the given nodes once.
    55  func IterNodes(nodes []*Node) Iterator {
    56  	return &sliceIter{nodes: nodes, index: -1}
    57  }
    58  
    59  // CycleNodes makes an iterator which cycles through the given nodes indefinitely.
    60  func CycleNodes(nodes []*Node) Iterator {
    61  	return &sliceIter{nodes: nodes, index: -1, cycle: true}
    62  }
    63  
    64  type sliceIter struct {
    65  	mu    sync.Mutex
    66  	nodes []*Node
    67  	index int
    68  	cycle bool
    69  }
    70  
    71  func (it *sliceIter) Next() bool {
    72  	it.mu.Lock()
    73  	defer it.mu.Unlock()
    74  
    75  	if len(it.nodes) == 0 {
    76  		return false
    77  	}
    78  	it.index++
    79  	if it.index == len(it.nodes) {
    80  		if it.cycle {
    81  			it.index = 0
    82  		} else {
    83  			it.nodes = nil
    84  			return false
    85  		}
    86  	}
    87  	return true
    88  }
    89  
    90  func (it *sliceIter) Node() *Node {
    91  	if len(it.nodes) == 0 {
    92  		return nil
    93  	}
    94  	return it.nodes[it.index]
    95  }
    96  
    97  func (it *sliceIter) Close() {
    98  	it.mu.Lock()
    99  	defer it.mu.Unlock()
   100  
   101  	it.nodes = nil
   102  }
   103  
   104  // Filter wraps an iterator such that Next only returns nodes for which
   105  // the 'check' function returns true.
   106  func Filter(it Iterator, check func(*Node) bool) Iterator {
   107  	return &filterIter{it, check}
   108  }
   109  
   110  type filterIter struct {
   111  	Iterator
   112  	check func(*Node) bool
   113  }
   114  
   115  func (f *filterIter) Next() bool {
   116  	for f.Iterator.Next() {
   117  		if f.check(f.Node()) {
   118  			return true
   119  		}
   120  	}
   121  	return false
   122  }
   123  
   124  // FairMix aggregates multiple node iterators. The mixer itself is an iterator which ends
   125  // only when Close is called. Source iterators added via AddSource are removed from the
   126  // mix when they end.
   127  //
   128  // The distribution of nodes returned by Next is approximately fair, i.e. FairMix
   129  // attempts to draw from all sources equally often. However, if a certain source is slow
   130  // and doesn't return a node within the configured timeout, a node from any other source
   131  // will be returned.
   132  //
   133  // It's safe to call AddSource and Close concurrently with Next.
   134  type FairMix struct {
   135  	wg      sync.WaitGroup
   136  	fromAny chan *Node
   137  	timeout time.Duration
   138  	cur     *Node
   139  
   140  	mu      sync.Mutex
   141  	closed  chan struct{}
   142  	sources []*mixSource
   143  	last    int
   144  }
   145  
   146  type mixSource struct {
   147  	it      Iterator
   148  	next    chan *Node
   149  	timeout time.Duration
   150  }
   151  
   152  // NewFairMix creates a mixer.
   153  //
   154  // The timeout specifies how long the mixer will wait for the next fairly-chosen source
   155  // before giving up and taking a node from any other source. A good way to set the timeout
   156  // is deciding how long you'd want to wait for a node on average. Passing a negative
   157  // timeout makes the mixer completely fair.
   158  func NewFairMix(timeout time.Duration) *FairMix {
   159  	m := &FairMix{
   160  		fromAny: make(chan *Node),
   161  		closed:  make(chan struct{}),
   162  		timeout: timeout,
   163  	}
   164  	return m
   165  }
   166  
   167  // AddSource adds a source of nodes.
   168  func (m *FairMix) AddSource(it Iterator) {
   169  	m.mu.Lock()
   170  	defer m.mu.Unlock()
   171  
   172  	if m.closed == nil {
   173  		return
   174  	}
   175  	m.wg.Add(1)
   176  	source := &mixSource{it, make(chan *Node), m.timeout}
   177  	m.sources = append(m.sources, source)
   178  	go m.runSource(m.closed, source)
   179  }
   180  
   181  // Close shuts down the mixer and all current sources.
   182  // Calling this is required to release resources associated with the mixer.
   183  func (m *FairMix) Close() {
   184  	m.mu.Lock()
   185  	defer m.mu.Unlock()
   186  
   187  	if m.closed == nil {
   188  		return
   189  	}
   190  	for _, s := range m.sources {
   191  		s.it.Close()
   192  	}
   193  	close(m.closed)
   194  	m.wg.Wait()
   195  	close(m.fromAny)
   196  	m.sources = nil
   197  	m.closed = nil
   198  }
   199  
   200  // Next returns a node from a random source.
   201  func (m *FairMix) Next() bool {
   202  	m.cur = nil
   203  
   204  	var timeout <-chan time.Time
   205  	if m.timeout >= 0 {
   206  		timer := time.NewTimer(m.timeout)
   207  		timeout = timer.C
   208  		defer timer.Stop()
   209  	}
   210  	for {
   211  		source := m.pickSource()
   212  		if source == nil {
   213  			return m.nextFromAny()
   214  		}
   215  		select {
   216  		case n, ok := <-source.next:
   217  			if ok {
   218  				m.cur = n
   219  				source.timeout = m.timeout
   220  				return true
   221  			}
   222  			// This source has ended.
   223  			m.deleteSource(source)
   224  		case <-timeout:
   225  			source.timeout /= 2
   226  			return m.nextFromAny()
   227  		}
   228  	}
   229  }
   230  
   231  // Node returns the current node.
   232  func (m *FairMix) Node() *Node {
   233  	return m.cur
   234  }
   235  
   236  // nextFromAny is used when there are no sources or when the 'fair' choice
   237  // doesn't turn up a node quickly enough.
   238  func (m *FairMix) nextFromAny() bool {
   239  	n, ok := <-m.fromAny
   240  	if ok {
   241  		m.cur = n
   242  	}
   243  	return ok
   244  }
   245  
   246  // pickSource chooses the next source to read from, cycling through them in order.
   247  func (m *FairMix) pickSource() *mixSource {
   248  	m.mu.Lock()
   249  	defer m.mu.Unlock()
   250  
   251  	if len(m.sources) == 0 {
   252  		return nil
   253  	}
   254  	m.last = (m.last + 1) % len(m.sources)
   255  	return m.sources[m.last]
   256  }
   257  
   258  // deleteSource deletes a source.
   259  func (m *FairMix) deleteSource(s *mixSource) {
   260  	m.mu.Lock()
   261  	defer m.mu.Unlock()
   262  
   263  	for i := range m.sources {
   264  		if m.sources[i] == s {
   265  			copy(m.sources[i:], m.sources[i+1:])
   266  			m.sources[len(m.sources)-1] = nil
   267  			m.sources = m.sources[:len(m.sources)-1]
   268  			break
   269  		}
   270  	}
   271  }
   272  
   273  // runSource reads a single source in a loop.
   274  func (m *FairMix) runSource(closed chan struct{}, s *mixSource) {
   275  	defer m.wg.Done()
   276  	defer close(s.next)
   277  	for s.it.Next() {
   278  		n := s.it.Node()
   279  		select {
   280  		case s.next <- n:
   281  		case m.fromAny <- n:
   282  		case <-closed:
   283  			return
   284  		}
   285  	}
   286  }