iter.go 6.8 KB

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