From: sgf Date: Wed, 26 Oct 2022 18:03:38 +0000 (+0300) Subject: chg(balancer): Even more pointers for Request. X-Git-Url: https://gitweb.sgf-dma.tk/?a=commitdiff_plain;h=c35bf7ed5b9072e79fef78eb0ad223e0c28c8478;p=go.git chg(balancer): Even more pointers for Request. --- diff --git a/concurrency-is-not-parallelism/balancer.go b/concurrency-is-not-parallelism/balancer.go index d39d69e..cea7ea8 100644 --- a/concurrency-is-not-parallelism/balancer.go +++ b/concurrency-is-not-parallelism/balancer.go @@ -27,7 +27,7 @@ func furtherProcess(r *Request, result int) { fmt.Printf("%v: further processing result %v..\n", r, result) } -func requester(work chan<- Request) { +func requester(work chan<- *Request) { c := make(chan int) reqNum := 0 for { @@ -39,7 +39,7 @@ func requester(work chan<- Request) { fn := func () int { return workFn(req) } req.fn = fn fmt.Printf("requester() '%v': sending\n", req) - work <- *req + work <- req result := <-c fmt.Printf("requester() '%v': received result %v\n", req, result) furtherProcess(req, result) @@ -51,7 +51,7 @@ func requester(work chan<- Request) { // FIXME: Add interfaces to abstreact from buffered/unbuffered channels and be // able to switch implementation immediately. type Worker struct { - requests chan Request + requests chan *Request pending int // Used by sort. num int // Used for worker identification. index int // Used by heap. @@ -74,7 +74,7 @@ func (w *Worker) work(done chan<- *Done) { req := <- w.requests fmt.Printf("%v: start computing '%v'\n", w, req) req.c <- req.fn() - done <- &Done{w, &req} + done <- &Done{w, req} fmt.Printf("%v: finished '%v'\n", w, req) } } @@ -110,7 +110,7 @@ type Balancer struct { done chan *Done } -func (b *Balancer) balance(work <-chan Request) { +func (b *Balancer) balance(work <-chan *Request) { timeout := time.After(20 * time.Second) out: for { @@ -132,7 +132,7 @@ out: } } -func (b *Balancer) dispatch(req Request) { +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(b.done) @@ -152,11 +152,11 @@ func (b *Balancer) completed(done *Done) { var nWorker int = 4 func main() { - work := make(chan Request) + work := make(chan *Request) pool := make(Pool, nWorker) for i := 0; i < nWorker; i++ { - requests := make(chan Request) + requests := make(chan *Request) pool[i] = &Worker{requests, 0, i, i} } heap.Init(&pool)