From: sgf Date: Tue, 1 Nov 2022 15:41:27 +0000 (+0300) Subject: chg(balancer): Use embedding instead of new type definition for WorkerId. X-Git-Url: https://gitweb.sgf-dma.tk/?a=commitdiff_plain;h=bb2c6ae76dc668fc88eb2e74488f3b2f0baa1bf0;p=go.git chg(balancer): Use embedding instead of new type definition for WorkerId. Embedding preserves the method set of *worker, thus WorkerId will have the same method set as *worker. --- diff --git a/concurrency-is-not-parallelism/balancer_v2/balancer/balancer.go b/concurrency-is-not-parallelism/balancer_v2/balancer/balancer.go index a1bacda..cc295d9 100644 --- a/concurrency-is-not-parallelism/balancer_v2/balancer/balancer.go +++ b/concurrency-is-not-parallelism/balancer_v2/balancer/balancer.go @@ -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 }