@@ -398,12 +398,11 @@ type dialer interface {
398398func (srv * Server ) run (dialstate dialer ) {
399399 defer srv .loopWG .Done ()
400400 var (
401- peers = make (map [discover.NodeID ]* Peer )
402- trusted = make (map [discover.NodeID ]bool , len (srv .TrustedNodes ))
403-
404- tasks []task
405- pendingTasks []task
401+ peers = make (map [discover.NodeID ]* Peer )
402+ trusted = make (map [discover.NodeID ]bool , len (srv .TrustedNodes ))
406403 taskdone = make (chan task , maxActiveDialTasks )
404+ runningTasks []task
405+ queuedTasks []task // tasks that can't run yet
407406 )
408407 // Put trusted nodes into a map to speed up checks.
409408 // Trusted peers are loaded on startup and cannot be
@@ -412,39 +411,39 @@ func (srv *Server) run(dialstate dialer) {
412411 trusted [n .ID ] = true
413412 }
414413
415- // Some task list helpers.
414+ // removes t from runningTasks
416415 delTask := func (t task ) {
417- for i := range tasks {
418- if tasks [i ] == t {
419- tasks = append (tasks [:i ], tasks [i + 1 :]... )
416+ for i := range runningTasks {
417+ if runningTasks [i ] == t {
418+ runningTasks = append (runningTasks [:i ], runningTasks [i + 1 :]... )
420419 break
421420 }
422421 }
423422 }
424- scheduleTasks := func (new []task ) {
425- pt := append (pendingTasks , new ... )
426- start := maxActiveDialTasks - len (tasks )
427- if len (pt ) < start {
428- start = len (pt )
423+ // starts until max number of active tasks is satisfied
424+ startTasks := func (ts []task ) (rest []task ) {
425+ i := 0
426+ for ; len (runningTasks ) < maxActiveDialTasks && i < len (ts ); i ++ {
427+ t := ts [i ]
428+ glog .V (logger .Detail ).Infoln ("new task:" , t )
429+ go func () { t .Do (srv ); taskdone <- t }()
430+ runningTasks = append (runningTasks , t )
429431 }
430- if start > 0 {
431- tasks = append ( tasks , pt [: start ] ... )
432- for _ , t := range pt [: start ] {
433- t := t
434- glog . V ( logger . Detail ). Infoln ( "new task:" , t )
435- go func () { t . Do ( srv ); taskdone <- t }()
436- }
437- copy ( pt , pt [ start :] )
438- pendingTasks = pt [: len ( pt ) - start ]
432+ return ts [ i :]
433+ }
434+ scheduleTasks := func () {
435+ // Start from queue first.
436+ queuedTasks = append ( queuedTasks [: 0 ], startTasks ( queuedTasks ) ... )
437+ // Query dialer for new tasks and start as many as possible now.
438+ if len ( runningTasks ) < maxActiveDialTasks {
439+ nt := dialstate . newTasks ( len ( runningTasks ) + len ( queuedTasks ), peers , time . Now () )
440+ queuedTasks = append ( queuedTasks , startTasks ( nt ) ... )
439441 }
440442 }
441443
442444running:
443445 for {
444- // Query the dialer for new tasks and launch them.
445- now := time .Now ()
446- nt := dialstate .newTasks (len (pendingTasks )+ len (tasks ), peers , now )
447- scheduleTasks (nt )
446+ scheduleTasks ()
448447
449448 select {
450449 case <- srv .quit :
@@ -466,7 +465,7 @@ running:
466465 // can update its state and remove it from the active
467466 // tasks list.
468467 glog .V (logger .Detail ).Infoln ("<-taskdone:" , t )
469- dialstate .taskDone (t , now )
468+ dialstate .taskDone (t , time . Now () )
470469 delTask (t )
471470 case c := <- srv .posthandshake :
472471 // A connection has passed the encryption handshake so
@@ -513,7 +512,7 @@ running:
513512 // Wait for peers to shut down. Pending connections and tasks are
514513 // not handled here and will terminate soon-ish because srv.quit
515514 // is closed.
516- glog .V (logger .Detail ).Infof ("ignoring %d pending tasks at spindown" , len (tasks ))
515+ glog .V (logger .Detail ).Infof ("ignoring %d pending tasks at spindown" , len (runningTasks ))
517516 for len (peers ) > 0 {
518517 p := <- srv .delpeer
519518 glog .V (logger .Detail ).Infoln ("<-delpeer (spindown):" , p )
0 commit comments