github.com/amazechain/amc@v0.1.3/internal/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 it.mu.Lock() 92 defer it.mu.Unlock() 93 if len(it.nodes) == 0 { 94 return nil 95 } 96 return it.nodes[it.index] 97 } 98 99 func (it *sliceIter) Close() { 100 it.mu.Lock() 101 defer it.mu.Unlock() 102 103 it.nodes = nil 104 } 105 106 // Filter wraps an iterator such that Next only returns nodes for which 107 // the 'check' function returns true. 108 func Filter(it Iterator, check func(*Node) bool) Iterator { 109 return &filterIter{it, check} 110 } 111 112 type filterIter struct { 113 Iterator 114 check func(*Node) bool 115 } 116 117 func (f *filterIter) Next() bool { 118 for f.Iterator.Next() { 119 if f.check(f.Node()) { 120 return true 121 } else { 122 //todo 123 return false 124 } 125 } 126 return false 127 } 128 129 // FairMix aggregates multiple node iterators. The mixer itself is an iterator which ends 130 // only when Close is called. Source iterators added via AddSource are removed from the 131 // mix when they end. 132 // 133 // The distribution of nodes returned by Next is approximately fair, i.e. FairMix 134 // attempts to draw from all sources equally often. However, if a certain source is slow 135 // and doesn't return a node within the configured timeout, a node from any other source 136 // will be returned. 137 // 138 // It's safe to call AddSource and Close concurrently with Next. 139 type FairMix struct { 140 wg sync.WaitGroup 141 fromAny chan *Node 142 timeout time.Duration 143 cur *Node 144 145 mu sync.Mutex 146 closed chan struct{} 147 sources []*mixSource 148 last int 149 } 150 151 type mixSource struct { 152 it Iterator 153 next chan *Node 154 timeout time.Duration 155 } 156 157 // NewFairMix creates a mixer. 158 // 159 // The timeout specifies how long the mixer will wait for the next fairly-chosen source 160 // before giving up and taking a node from any other source. A good way to set the timeout 161 // is deciding how long you'd want to wait for a node on average. Passing a negative 162 // timeout makes the mixer completely fair. 163 func NewFairMix(timeout time.Duration) *FairMix { 164 m := &FairMix{ 165 fromAny: make(chan *Node), 166 closed: make(chan struct{}), 167 timeout: timeout, 168 } 169 return m 170 } 171 172 // AddSource adds a source of nodes. 173 func (m *FairMix) AddSource(it Iterator) { 174 m.mu.Lock() 175 defer m.mu.Unlock() 176 177 if m.closed == nil { 178 return 179 } 180 m.wg.Add(1) 181 source := &mixSource{it, make(chan *Node), m.timeout} 182 m.sources = append(m.sources, source) 183 go m.runSource(m.closed, source) 184 } 185 186 // Close shuts down the mixer and all current sources. 187 // Calling this is required to release resources associated with the mixer. 188 func (m *FairMix) Close() { 189 m.mu.Lock() 190 defer m.mu.Unlock() 191 192 if m.closed == nil { 193 return 194 } 195 for _, s := range m.sources { 196 s.it.Close() 197 } 198 close(m.closed) 199 m.wg.Wait() 200 close(m.fromAny) 201 m.sources = nil 202 m.closed = nil 203 } 204 205 // Next returns a node from a random source. 206 func (m *FairMix) Next() bool { 207 m.cur = nil 208 209 for { 210 source := m.pickSource() 211 if source == nil { 212 return m.nextFromAny() 213 } 214 215 var timeout <-chan time.Time 216 if source.timeout >= 0 { 217 timer := time.NewTimer(source.timeout) 218 timeout = timer.C 219 defer timer.Stop() 220 } 221 222 select { 223 case n, ok := <-source.next: 224 if ok { 225 // Here, the timeout is reset to the configured value 226 // because the source delivered a node. 227 source.timeout = m.timeout 228 m.cur = n 229 return true 230 } 231 // This source has ended. 232 m.deleteSource(source) 233 case <-timeout: 234 // The selected source did not deliver a node within the timeout, so the 235 // timeout duration is halved for next time. This is supposed to improve 236 // latency with stuck sources. 237 source.timeout /= 2 238 return m.nextFromAny() 239 } 240 } 241 } 242 243 // Node returns the current node. 244 func (m *FairMix) Node() *Node { 245 return m.cur 246 } 247 248 // nextFromAny is used when there are no sources or when the 'fair' choice 249 // doesn't turn up a node quickly enough. 250 func (m *FairMix) nextFromAny() bool { 251 n, ok := <-m.fromAny 252 if ok { 253 m.cur = n 254 } 255 return ok 256 } 257 258 // pickSource chooses the next source to read from, cycling through them in order. 259 func (m *FairMix) pickSource() *mixSource { 260 m.mu.Lock() 261 defer m.mu.Unlock() 262 263 if len(m.sources) == 0 { 264 return nil 265 } 266 m.last = (m.last + 1) % len(m.sources) 267 return m.sources[m.last] 268 } 269 270 // deleteSource deletes a source. 271 func (m *FairMix) deleteSource(s *mixSource) { 272 m.mu.Lock() 273 defer m.mu.Unlock() 274 275 for i := range m.sources { 276 if m.sources[i] == s { 277 copy(m.sources[i:], m.sources[i+1:]) 278 m.sources[len(m.sources)-1] = nil 279 m.sources = m.sources[:len(m.sources)-1] 280 break 281 } 282 } 283 } 284 285 // runSource reads a single source in a loop. 286 func (m *FairMix) runSource(closed chan struct{}, s *mixSource) { 287 defer m.wg.Done() 288 defer close(s.next) 289 for s.it.Next() { 290 n := s.it.Node() 291 select { 292 case s.next <- n: 293 case m.fromAny <- n: 294 case <-closed: 295 return 296 } 297 } 298 }