fix(balancer): Start workers correctly.
authorsgf <sgf.dma@gmail.com>
Wed, 26 Oct 2022 18:21:26 +0000 (21:21 +0300)
committersgf <sgf.dma@gmail.com>
Wed, 26 Oct 2022 18:21:26 +0000 (21:21 +0300)
Balancer should start all required workers beforehand, not dispatch.

concurrency-is-not-parallelism/balancer.go

index 1d17729..2b5df4a 100644 (file)
@@ -117,6 +117,12 @@ type Balancer struct {
 }
 
 func (b *Balancer) balance(work <-chan *Request) {
+    fmt.Println("balance(): Starting workers..")
+    for _, w := range b.pool {
+        go w.work(time.Now().Format("05.000"), b.done)
+        time.Sleep(time.Second)
+    }
+
     timeout := time.After(20 * time.Second)
 out:
     for {
@@ -141,7 +147,6 @@ out:
 func (b *Balancer) dispatch(req *Request) {
     w := heap.Pop(&b.pool).(*Worker)
     fmt.Printf("dispatch() '%v': sending to '%v'\n", req, w)
-    go w.work(time.Now().Format("05.000"), b.done)
     w.pending++
     heap.Push(&b.pool, w)
     w.requests <- req