chg(balancer): Use embedding instead of new type definition for WorkerId.
authorsgf <sgf.dma@gmail.com>
Tue, 1 Nov 2022 15:41:27 +0000 (18:41 +0300)
committersgf <sgf.dma@gmail.com>
Tue, 1 Nov 2022 15:51:46 +0000 (18:51 +0300)
Embedding preserves the method set of *worker, thus WorkerId will have
the same method set as *worker.

concurrency-is-not-parallelism/balancer_v2/balancer/balancer.go

index a1bacda..cc295d9 100644 (file)
@@ -16,7 +16,9 @@ type Requester interface {
 
 // WorkerId is identification of a particular runner.
 // It's needed to abstract '*worker' pointer in Done channel.
-type WorkerId *worker
+type WorkerId struct {
+    *worker
+}
 
 // WorkRunner is an interface, which every work runner should conform to.
 // This is an actual implementation running Requester's Fn() function and
@@ -122,7 +124,7 @@ func (b *Balancer) dispatch(req Requester) {
 // completed re-inserts worker, which finished request, into proper place in the pool.
 func (b *Balancer) completed(done *Done) {
     fmt.Printf("completed() '%v': finished '%v'\n", done.Worker, done.Req)
-    w := (*worker)(done.Worker)
+    w := done.Worker.worker
     w.pending--
     heap.Remove(&b.pool, w.index)
     heap.Push(&b.pool, w)
@@ -134,7 +136,7 @@ func InitBalancer(runners []WorkRunner) *Balancer {
     pool := make(pool, n)
     for i := 0; i < n; i++ {
         w := worker{num: i, pending: 0, index: i}
-        runners[i].InitRunner(&w)
+        runners[i].InitRunner(WorkerId{&w})
         w.runner = runners[i]
         pool[i] = &w
     }