From: sgf Date: Wed, 26 Oct 2022 18:21:26 +0000 (+0300) Subject: fix(balancer): Start workers correctly. X-Git-Url: https://gitweb.sgf-dma.tk/?a=commitdiff_plain;h=e39201e5ea64235e1bd2aa14709c384543da5ddc;p=go.git fix(balancer): Start workers correctly. Balancer should start all required workers beforehand, not dispatch. --- diff --git a/concurrency-is-not-parallelism/balancer.go b/concurrency-is-not-parallelism/balancer.go index 1d17729..2b5df4a 100644 --- a/concurrency-is-not-parallelism/balancer.go +++ b/concurrency-is-not-parallelism/balancer.go @@ -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