@@ -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 {
@@ -342,7 +348,9 @@ func (pool *serverPool) selectPeerWait(reqID uint64, canSend func(*peer) (bool,
342348func (pool * serverPool ) eventLoop () {
343349 lookupCnt := 0
344350 var convTime mclock.AbsTime
345- pool .discSetPeriod <- time .Millisecond * 100
351+ if pool .discSetPeriod != nil {
352+ pool .discSetPeriod <- time .Millisecond * 100
353+ }
346354 for {
347355 select {
348356 case entry := <- pool .timeout :
@@ -375,39 +383,7 @@ func (pool *serverPool) eventLoop() {
375383
376384 case node := <- pool .discNodes :
377385 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- }
386+ entry := pool .findOrNewNode (discover .NodeID (node .ID ), node .IP , node .TCP )
411387 pool .updateCheckDial (entry )
412388 pool .lock .Unlock ()
413389
@@ -419,12 +395,16 @@ func (pool *serverPool) eventLoop() {
419395 lookupCnt ++
420396 if pool .fastDiscover && (lookupCnt == 50 || time .Duration (mclock .Now ()- convTime ) > time .Minute ) {
421397 pool .fastDiscover = false
422- pool .discSetPeriod <- time .Minute
398+ if pool .discSetPeriod != nil {
399+ pool .discSetPeriod <- time .Minute
400+ }
423401 }
424402 }
425403
426404 case <- pool .quit :
427- close (pool .discSetPeriod )
405+ if pool .discSetPeriod != nil {
406+ close (pool .discSetPeriod )
407+ }
428408 pool .connWg .Wait ()
429409 pool .saveNodes ()
430410 pool .wg .Done ()
@@ -434,6 +414,42 @@ func (pool *serverPool) eventLoop() {
434414 }
435415}
436416
417+ func (pool * serverPool ) findOrNewNode (id discover.NodeID , ip net.IP , port uint16 ) * poolEntry {
418+ now := mclock .Now ()
419+ entry := pool .entries [id ]
420+ if entry == nil {
421+ glog .V (logger .Debug ).Infof ("discovered %v" , id .String ())
422+ entry = & poolEntry {
423+ id : id ,
424+ addr : make (map [string ]* poolEntryAddress ),
425+ addrSelect : * newWeightedRandomSelect (),
426+ shortRetry : shortRetryCnt ,
427+ }
428+ pool .entries [id ] = entry
429+ // initialize previously unknown peers with good statistics to give a chance to prove themselves
430+ entry .connectStats .add (1 , initStatsWeight )
431+ entry .delayStats .add (0 , initStatsWeight )
432+ entry .responseStats .add (0 , initStatsWeight )
433+ entry .timeoutStats .add (0 , initStatsWeight )
434+ }
435+ entry .lastDiscovered = now
436+ addr := & poolEntryAddress {
437+ ip : ip ,
438+ port : port ,
439+ }
440+ if a , ok := entry .addr [addr .strKey ()]; ok {
441+ addr = a
442+ } else {
443+ entry .addr [addr .strKey ()] = addr
444+ }
445+ addr .lastSeen = now
446+ entry .addrSelect .update (addr )
447+ if ! entry .known {
448+ pool .newQueue .setLatest (entry )
449+ }
450+ return entry
451+ }
452+
437453// loadNodes loads known nodes and their statistics from the database
438454func (pool * serverPool ) loadNodes () {
439455 enc , err := pool .db .Get (pool .dbKey )
0 commit comments