@@ -160,10 +160,10 @@ func (pool *serverPool) connect(p *peer, ip net.IP, port uint16) *poolEntry {
160160 defer pool .lock .Unlock ()
161161 entry := pool .entries [p .ID ()]
162162 if entry == nil {
163- return nil
163+ entry = pool . findOrNewNode ( p . ID (), ip , port )
164164 }
165165 glog .V (logger .Debug ).Infof ("connecting to %v, state: %v" , p .id , entry .state )
166- if entry .state != psDialed {
166+ if entry .state == psConnected || entry . state == psRegistered {
167167 return nil
168168 }
169169 pool .connWg .Add (1 )
@@ -250,11 +250,17 @@ type poolStatAdjust struct {
250250
251251// adjustBlockDelay adjusts the block announce delay statistics of a node
252252func (pool * serverPool ) adjustBlockDelay (entry * poolEntry , time time.Duration ) {
253+ if entry == nil {
254+ return
255+ }
253256 pool .adjustStats <- poolStatAdjust {pseBlockDelay , entry , time }
254257}
255258
256259// adjustResponseTime adjusts the request response time statistics of a node
257260func (pool * serverPool ) adjustResponseTime (entry * poolEntry , time time.Duration , timeout bool ) {
261+ if entry == nil {
262+ return
263+ }
258264 if timeout {
259265 pool .adjustStats <- poolStatAdjust {pseResponseTimeout , entry , time }
260266 } else {
@@ -375,39 +381,7 @@ func (pool *serverPool) eventLoop() {
375381
376382 case node := <- pool .discNodes :
377383 pool .lock .Lock ()
378- now := mclock .Now ()
379- id := discover .NodeID (node .ID )
380- entry := pool .entries [id ]
381- if entry == nil {
382- glog .V (logger .Debug ).Infof ("discovered %v" , node .String ())
383- entry = & poolEntry {
384- id : id ,
385- addr : make (map [string ]* poolEntryAddress ),
386- addrSelect : * newWeightedRandomSelect (),
387- shortRetry : shortRetryCnt ,
388- }
389- pool .entries [id ] = entry
390- // initialize previously unknown peers with good statistics to give a chance to prove themselves
391- entry .connectStats .add (1 , initStatsWeight )
392- entry .delayStats .add (0 , initStatsWeight )
393- entry .responseStats .add (0 , initStatsWeight )
394- entry .timeoutStats .add (0 , initStatsWeight )
395- }
396- entry .lastDiscovered = now
397- addr := & poolEntryAddress {
398- ip : node .IP ,
399- port : node .TCP ,
400- }
401- if a , ok := entry .addr [addr .strKey ()]; ok {
402- addr = a
403- } else {
404- entry .addr [addr .strKey ()] = addr
405- }
406- addr .lastSeen = now
407- entry .addrSelect .update (addr )
408- if ! entry .known {
409- pool .newQueue .setLatest (entry )
410- }
384+ entry := pool .findOrNewNode (discover .NodeID (node .ID ), node .IP , node .TCP )
411385 pool .updateCheckDial (entry )
412386 pool .lock .Unlock ()
413387
@@ -434,6 +408,42 @@ func (pool *serverPool) eventLoop() {
434408 }
435409}
436410
411+ func (pool * serverPool ) findOrNewNode (id discover.NodeID , ip net.IP , port uint16 ) * poolEntry {
412+ now := mclock .Now ()
413+ entry := pool .entries [id ]
414+ if entry == nil {
415+ glog .V (logger .Debug ).Infof ("discovered %v" , id .String ())
416+ entry = & poolEntry {
417+ id : id ,
418+ addr : make (map [string ]* poolEntryAddress ),
419+ addrSelect : * newWeightedRandomSelect (),
420+ shortRetry : shortRetryCnt ,
421+ }
422+ pool .entries [id ] = entry
423+ // initialize previously unknown peers with good statistics to give a chance to prove themselves
424+ entry .connectStats .add (1 , initStatsWeight )
425+ entry .delayStats .add (0 , initStatsWeight )
426+ entry .responseStats .add (0 , initStatsWeight )
427+ entry .timeoutStats .add (0 , initStatsWeight )
428+ }
429+ entry .lastDiscovered = now
430+ addr := & poolEntryAddress {
431+ ip : ip ,
432+ port : port ,
433+ }
434+ if a , ok := entry .addr [addr .strKey ()]; ok {
435+ addr = a
436+ } else {
437+ entry .addr [addr .strKey ()] = addr
438+ }
439+ addr .lastSeen = now
440+ entry .addrSelect .update (addr )
441+ if ! entry .known {
442+ pool .newQueue .setLatest (entry )
443+ }
444+ return entry
445+ }
446+
437447// loadNodes loads known nodes and their statistics from the database
438448func (pool * serverPool ) loadNodes () {
439449 enc , err := pool .db .Get (pool .dbKey )
0 commit comments