github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/p2p/dial.go (about) 1 // Copyright 2015 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 p2p 18 19 import ( 20 "container/heap" 21 "crypto/rand" 22 "fmt" 23 "net" 24 "time" 25 26 "github.com/ethereumproject/go-ethereum/logger" 27 "github.com/ethereumproject/go-ethereum/logger/glog" 28 "github.com/ethereumproject/go-ethereum/p2p/discover" 29 ) 30 31 const ( 32 // This is the amount of time spent waiting in between 33 // redialing a certain node. 34 dialHistoryExpiration = 30 * time.Second 35 36 // Discovery lookups are throttled and can only run 37 // once every few seconds. 38 lookupInterval = 4 * time.Second 39 40 // Endpoint resolution is throttled with bounded backoff. 41 initialResolveDelay = 60 * time.Second 42 maxResolveDelay = time.Hour 43 ) 44 45 // dialstate schedules dials and discovery lookups. 46 // it get's a chance to compute new tasks on every iteration 47 // of the main loop in Server.run. 48 type dialstate struct { 49 maxDynDials int 50 ntab discoverTable 51 52 lookupRunning bool 53 dialing map[discover.NodeID]connFlag 54 lookupBuf []*discover.Node // current discovery lookup results 55 randomNodes []*discover.Node // filled from Table 56 static map[discover.NodeID]*dialTask 57 hist *dialHistory 58 } 59 60 type discoverTable interface { 61 Self() *discover.Node 62 Close() 63 Resolve(target discover.NodeID) *discover.Node 64 Lookup(target discover.NodeID) []*discover.Node 65 ReadRandomNodes([]*discover.Node) int 66 } 67 68 // the dial history remembers recent dials. 69 type dialHistory []pastDial 70 71 // pastDial is an entry in the dial history. 72 type pastDial struct { 73 id discover.NodeID 74 exp time.Time 75 } 76 77 type task interface { 78 Do(*Server) 79 } 80 81 // A dialTask is generated for each node that is dialed. Its 82 // fields cannot be accessed while the task is running. 83 type dialTask struct { 84 flags connFlag 85 dest *discover.Node 86 lastResolved time.Time 87 resolveDelay time.Duration 88 } 89 90 // discoverTask runs discovery table operations. 91 // Only one discoverTask is active at any time. 92 // discoverTask.Do performs a random lookup. 93 type discoverTask struct { 94 results []*discover.Node 95 } 96 97 // A waitExpireTask is generated if there are no other tasks 98 // to keep the loop in Server.run ticking. 99 type waitExpireTask struct { 100 time.Duration 101 } 102 103 func newDialState(static []*discover.Node, ntab discoverTable, maxdyn int) *dialstate { 104 s := &dialstate{ 105 maxDynDials: maxdyn, 106 ntab: ntab, 107 static: make(map[discover.NodeID]*dialTask), 108 dialing: make(map[discover.NodeID]connFlag), 109 randomNodes: make([]*discover.Node, maxdyn/2), 110 hist: new(dialHistory), 111 } 112 for _, n := range static { 113 s.addStatic(n) 114 } 115 return s 116 } 117 118 func (s *dialstate) addStatic(n *discover.Node) { 119 // This overwites the task instead of updating an existing 120 // entry, giving users the opportunity to force a resolve operation. 121 s.static[n.ID] = &dialTask{flags: staticDialedConn, dest: n} 122 } 123 124 func (s *dialstate) removeStatic(n *discover.Node) { 125 // This removes a task so future attempts to connect will not be made. 126 delete(s.static, n.ID) 127 // This removes a previous dial timestamp so that application 128 // can force a server to reconnect with chosen peer immediately. 129 s.hist.remove(n.ID) 130 } 131 132 func (s *dialstate) newTasks(nRunning int, peers map[discover.NodeID]*Peer, now time.Time) []task { 133 var newtasks []task 134 isDialing := func(id discover.NodeID) bool { 135 _, found := s.dialing[id] 136 return found || peers[id] != nil || s.hist.contains(id) 137 } 138 addDial := func(flag connFlag, n *discover.Node) bool { 139 if isDialing(n.ID) { 140 return false 141 } 142 s.dialing[n.ID] = flag 143 newtasks = append(newtasks, &dialTask{flags: flag, dest: n}) 144 return true 145 } 146 147 // Compute number of dynamic dials necessary at this point. 148 needDynDials := s.maxDynDials 149 for _, p := range peers { 150 if p.rw.is(dynDialedConn) { 151 needDynDials-- 152 } 153 } 154 for _, flag := range s.dialing { 155 if flag&dynDialedConn != 0 { 156 needDynDials-- 157 } 158 } 159 160 // Expire the dial history on every invocation. 161 s.hist.expire(now) 162 163 // Create dials for static nodes if they are not connected. 164 for id, t := range s.static { 165 if !isDialing(id) { 166 s.dialing[id] = t.flags 167 newtasks = append(newtasks, t) 168 } 169 } 170 171 // Use random nodes from the table for half of the necessary 172 // dynamic dials. 173 randomCandidates := needDynDials / 2 174 if randomCandidates > 0 { 175 n := s.ntab.ReadRandomNodes(s.randomNodes) 176 for i := 0; i < randomCandidates && i < n; i++ { 177 if addDial(dynDialedConn, s.randomNodes[i]) { 178 needDynDials-- 179 } 180 } 181 } 182 // Create dynamic dials from random lookup results, removing tried 183 // items from the result buffer. 184 i := 0 185 for ; i < len(s.lookupBuf) && needDynDials > 0; i++ { 186 if addDial(dynDialedConn, s.lookupBuf[i]) { 187 needDynDials-- 188 } 189 } 190 s.lookupBuf = s.lookupBuf[:copy(s.lookupBuf, s.lookupBuf[i:])] 191 // Launch a discovery lookup if more candidates are needed. 192 if len(s.lookupBuf) < needDynDials && !s.lookupRunning { 193 s.lookupRunning = true 194 newtasks = append(newtasks, &discoverTask{}) 195 } 196 197 // Launch a timer to wait for the next node to expire if all 198 // candidates have been tried and no task is currently active. 199 // This should prevent cases where the dialer logic is not ticked 200 // because there are no pending events. 201 if nRunning == 0 && len(newtasks) == 0 && s.hist.Len() > 0 { 202 t := &waitExpireTask{s.hist.min().exp.Sub(now)} 203 newtasks = append(newtasks, t) 204 } 205 return newtasks 206 } 207 208 func (s *dialstate) taskDone(t task, now time.Time) { 209 switch t := t.(type) { 210 case *dialTask: 211 s.hist.add(t.dest.ID, now.Add(dialHistoryExpiration)) 212 delete(s.dialing, t.dest.ID) 213 case *discoverTask: 214 s.lookupRunning = false 215 s.lookupBuf = append(s.lookupBuf, t.results...) 216 } 217 } 218 219 func (t *dialTask) Do(srv *Server) { 220 if t.dest.Incomplete() { 221 if !t.resolve(srv) { 222 return 223 } 224 } 225 success := t.dial(srv, t.dest) 226 // Try resolving the ID of static nodes if dialing failed. 227 if !success && t.flags&staticDialedConn != 0 { 228 if t.resolve(srv) { 229 t.dial(srv, t.dest) 230 } 231 } 232 } 233 234 // resolve attempts to find the current endpoint for the destination 235 // using discovery. 236 // 237 // Resolve operations are throttled with backoff to avoid flooding the 238 // discovery network with useless queries for nodes that don't exist. 239 // The backoff delay resets when the node is found. 240 func (t *dialTask) resolve(srv *Server) bool { 241 if srv.ntab == nil { 242 glog.V(logger.Debug).Infof("can't resolve node %x: discovery is disabled", t.dest.ID[:6]) 243 return false 244 } 245 if t.resolveDelay == 0 { 246 t.resolveDelay = initialResolveDelay 247 } 248 if time.Since(t.lastResolved) < t.resolveDelay { 249 return false 250 } 251 resolved := srv.ntab.Resolve(t.dest.ID) 252 t.lastResolved = time.Now() 253 if resolved == nil { 254 t.resolveDelay *= 2 255 if t.resolveDelay > maxResolveDelay { 256 t.resolveDelay = maxResolveDelay 257 } 258 glog.V(logger.Debug).Infof("resolving node %x failed (new delay: %v)", t.dest.ID[:6], t.resolveDelay) 259 return false 260 } 261 // The node was found. 262 t.resolveDelay = initialResolveDelay 263 t.dest = resolved 264 glog.V(logger.Debug).Infof("resolved node %x: %v:%d", t.dest.ID[:6], t.dest.IP, t.dest.TCP) 265 return true 266 } 267 268 // dial performs the actual connection attempt. 269 func (t *dialTask) dial(srv *Server, dest *discover.Node) bool { 270 addr := &net.TCPAddr{IP: dest.IP, Port: int(dest.TCP)} 271 glog.V(logger.Detail).Infof("dial tcp %v (%x)\n", addr, dest.ID[:6]) 272 fd, err := srv.Dialer.Dial("tcp", addr.String()) 273 if err != nil { 274 glog.V(logger.Detail).Infof("%v", err) 275 return false 276 } 277 mfd := newMeteredConn(fd, false) 278 srv.setupConn(mfd, t.flags, dest) 279 return true 280 } 281 282 func (t *dialTask) String() string { 283 return fmt.Sprintf("%v %x %v:%d", t.flags, t.dest.ID[:8], t.dest.IP, t.dest.TCP) 284 } 285 286 func (t *discoverTask) Do(srv *Server) { 287 // newTasks generates a lookup task whenever dynamic dials are 288 // necessary. Lookups need to take some time, otherwise the 289 // event loop spins too fast. 290 next := srv.lastLookup.Add(lookupInterval) 291 if now := time.Now(); now.Before(next) { 292 time.Sleep(next.Sub(now)) 293 } 294 srv.lastLookup = time.Now() 295 var target discover.NodeID 296 rand.Read(target[:]) 297 t.results = srv.ntab.Lookup(target) 298 } 299 300 func (t *discoverTask) String() string { 301 s := "discovery lookup" 302 if len(t.results) > 0 { 303 s += fmt.Sprintf(" (%d results)", len(t.results)) 304 } 305 return s 306 } 307 308 func (t waitExpireTask) Do(*Server) { 309 time.Sleep(t.Duration) 310 } 311 func (t waitExpireTask) String() string { 312 return fmt.Sprintf("wait for dial hist expire (%v)", t.Duration) 313 } 314 315 // Use only these methods to access or modify dialHistory. 316 func (h dialHistory) min() pastDial { 317 return h[0] 318 } 319 func (h *dialHistory) add(id discover.NodeID, exp time.Time) { 320 heap.Push(h, pastDial{id, exp}) 321 322 } 323 func (h *dialHistory) remove(id discover.NodeID) bool { 324 for i, v := range *h { 325 if v.id == id { 326 heap.Remove(h, i) 327 return true 328 } 329 } 330 return false 331 } 332 func (h dialHistory) contains(id discover.NodeID) bool { 333 for _, v := range h { 334 if v.id == id { 335 return true 336 } 337 } 338 return false 339 } 340 func (h *dialHistory) expire(now time.Time) { 341 for h.Len() > 0 && h.min().exp.Before(now) { 342 heap.Pop(h) 343 } 344 } 345 346 // heap.Interface boilerplate 347 func (h dialHistory) Len() int { return len(h) } 348 func (h dialHistory) Less(i, j int) bool { return h[i].exp.Before(h[j].exp) } 349 func (h dialHistory) Swap(i, j int) { h[i], h[j] = h[j], h[i] } 350 func (h *dialHistory) Push(x interface{}) { 351 *h = append(*h, x.(pastDial)) 352 } 353 func (h *dialHistory) Pop() interface{} { 354 old := *h 355 n := len(old) 356 x := old[n-1] 357 *h = old[0 : n-1] 358 return x 359 }