From cbb17c7538420168e7c70512477b225634b7c717 Mon Sep 17 00:00:00 2001 From: plastikfan Date: Sun, 27 Aug 2023 20:37:24 +0100 Subject: [PATCH] ref(async): rename wait group interfaces (#45) --- async/annotated-wait-group.go | 58 +++++++++++++++++------------- async/annotated-wait-group_test.go | 6 ++-- async/worker-pool.go | 4 +-- async/worker-pool_test.go | 2 +- internal/helpers/test-consumer.go | 4 +-- internal/helpers/test-producer.go | 4 +-- 6 files changed, 44 insertions(+), 34 deletions(-) diff --git a/async/annotated-wait-group.go b/async/annotated-wait-group.go index efa6b80..0160e2e 100644 --- a/async/annotated-wait-group.go +++ b/async/annotated-wait-group.go @@ -19,51 +19,61 @@ type namesCollection map[GoRoutineName]string // without the go routine name behind the same methods name, ie Add needs to be // able to be invoked either way. -// AssistedAdder is the interface that is a restricted view of a wait group +// AnnotatedWgAdder is the interface that is a restricted view of a wait group // that only allows adding to the wait group with the addition of being // able to specify the name representing the calling go routine. This interface // can be acquired from the wait group using a standard interface type query. -type AssistedAdder interface { +type AnnotatedWgAdder interface { Add(delta int, name ...GoRoutineName) } -// AssistedQuitter is the interface that is a restricted view of a wait group +// AnnotatedWgQuitter is the interface that is a restricted view of a wait group // that only allows Done signalling on the wait group with the addition of being // able to specify the name representing the calling go routine. This interface // can be acquired from the wait group using a standard interface type query. -type AssistedQuitter interface { +type AnnotatedWgQuitter interface { Done(name ...GoRoutineName) } -// AssistedWaiter is the interface that is a restricted view of a wait group -// that only allows waiting on the wait group with the addition of being +// AnnotatedWgAQ is the interface that is a restricted view of a wait group +// that allows adding to the wait group and Done signalling with the addition of being // able to specify the name representing the calling go routine. This interface // can be acquired from the wait group using a standard interface type query. -type AssistedWaiter interface { - Wait(name ...GoRoutineName) +type AnnotatedWgAQ interface { + AnnotatedWgAdder + AnnotatedWgQuitter } -// WaitGroupEx the extended WaitGroup -type WaitGroupEx interface { - AssistedAdder - AssistedQuitter - AssistedWaiter +// AnnotatedWgWaiter is the interface that is a restricted view of a wait group +// that only allows waiting on the wait group with the addition of being +// able to specify the name representing the calling go routine. This interface +// can be acquired from the wait group using a standard interface type query. +type AnnotatedWgWaiter interface { + Wait(name ...GoRoutineName) } -// AssistedCounter is the interface that is a restricted view of a wait group +// AnnotatedWgCounter is the interface that is a restricted view of a wait group // that only allows querying the wait group count. This interface // can be acquired from the wait group using a standard interface type query. -type AssistedCounter interface { +type AnnotatedWgCounter interface { Count() int } -type waitGroupAssister struct { +// WaitGroupEx the extended WaitGroup +type WaitGroupEx interface { + AnnotatedWgAdder + AnnotatedWgQuitter + AnnotatedWgWaiter + AnnotatedWgCounter +} + +type waitGroupEx struct { counter int32 names namesCollection waitGroupName string } -func (a *waitGroupAssister) Add(delta int, name ...GoRoutineName) { +func (a *waitGroupEx) Add(delta int, name ...GoRoutineName) { a.counter += int32(delta) if len(name) > 0 { @@ -73,7 +83,7 @@ func (a *waitGroupAssister) Add(delta int, name ...GoRoutineName) { } } -func (a *waitGroupAssister) Done(name ...GoRoutineName) { +func (a *waitGroupEx) Done(name ...GoRoutineName) { a.counter-- if len(name) > 0 { @@ -83,20 +93,20 @@ func (a *waitGroupAssister) Done(name ...GoRoutineName) { } } -func (a *waitGroupAssister) Wait(name ...GoRoutineName) { +func (a *waitGroupEx) Wait(name ...GoRoutineName) { if len(name) > 0 { a.indicate("🧭🧭🧭", string(name[0]), "Wait") } } -func (a *waitGroupAssister) indicate(highlight, name, op string) { +func (a *waitGroupEx) indicate(highlight, name, op string) { fmt.Printf( " %v [[ WaitGroupAssister(%v).%v ]] - gr-name: '%v' (count: '%v') (running: '%v')\n", highlight, a.waitGroupName, op, name, a.counter, a.running(), ) } -func (a *waitGroupAssister) running() string { +func (a *waitGroupEx) running() string { runners := lo.Map(lo.Keys(a.names), func(item GoRoutineName, _ int) string { return string(item) }) @@ -109,15 +119,15 @@ func (a *waitGroupAssister) running() string { // diagnosing concurrency issues. type AnnotatedWaitGroup struct { wg sync.WaitGroup - assistant waitGroupAssister + assistant waitGroupEx mux sync.Mutex } // NewAnnotatedWaitGroup creates a new AnnotatedWaitGroup instance containing // the core WaitGroup instance. -func NewAnnotatedWaitGroup(name string) *AnnotatedWaitGroup { +func NewAnnotatedWaitGroup(name string) WaitGroupEx { return &AnnotatedWaitGroup{ - assistant: waitGroupAssister{ + assistant: waitGroupEx{ waitGroupName: name, names: make(namesCollection), }, diff --git a/async/annotated-wait-group_test.go b/async/annotated-wait-group_test.go index 6ba5680..c6d470b 100644 --- a/async/annotated-wait-group_test.go +++ b/async/annotated-wait-group_test.go @@ -12,7 +12,7 @@ var _ = Describe("AnnotatedWaitGroup", func() { Context("Add", func() { It("should: add", func() { - var wg async.WaitGroupEx = async.NewAnnotatedWaitGroup("add-unit-test") + wg := async.NewAnnotatedWaitGroup("add-unit-test") wg.Add(1, "producer") }) @@ -20,7 +20,7 @@ var _ = Describe("AnnotatedWaitGroup", func() { Context("Done", func() { It("should: quit", func() { - var wg async.WaitGroupEx = async.NewAnnotatedWaitGroup("done-unit-test") + wg := async.NewAnnotatedWaitGroup("done-unit-test") wg.Add(1, "producer") wg.Done("producer") @@ -29,7 +29,7 @@ var _ = Describe("AnnotatedWaitGroup", func() { Context("Wait", func() { It("should: quit", func() { - var wg async.WaitGroupEx = async.NewAnnotatedWaitGroup("wait-unit-test") + wg := async.NewAnnotatedWaitGroup("wait-unit-test") wg.Add(1, "producer") go func() { diff --git a/async/worker-pool.go b/async/worker-pool.go index 54f0d33..07a21c6 100644 --- a/async/worker-pool.go +++ b/async/worker-pool.go @@ -46,7 +46,7 @@ type WorkerPool[I, O any] struct { noWorkers int SourceJobsChIn JobStreamR[I] - Quitter AssistedQuitter + Quitter AnnotatedWgQuitter } type NewWorkerPoolParams[I, O any] struct { @@ -54,7 +54,7 @@ type NewWorkerPoolParams[I, O any] struct { Exec ExecutiveFunc[I, O] JobsCh chan Job[I] CancelCh CancelStream - Quitter AssistedQuitter + Quitter AnnotatedWgQuitter } func NewWorkerPool[I, O any](params *NewWorkerPoolParams[I, O]) *WorkerPool[I, O] { diff --git a/async/worker-pool_test.go b/async/worker-pool_test.go index f856126..8fc1e10 100644 --- a/async/worker-pool_test.go +++ b/async/worker-pool_test.go @@ -158,7 +158,7 @@ var _ = Describe("WorkerPool", func() { pipe := start[TestJobInput, TestJobOutput]() defer func() { - if counter, ok := (pipe.wgex).(async.AssistedCounter); ok { + if counter, ok := (pipe.wgex).(async.AnnotatedWgCounter); ok { fmt.Printf("🎈🎈🎈🎈 remaining count: '%v'\n", counter.Count()) } }() diff --git a/internal/helpers/test-consumer.go b/internal/helpers/test-consumer.go index 51c3f46..daaee54 100644 --- a/internal/helpers/test-consumer.go +++ b/internal/helpers/test-consumer.go @@ -8,7 +8,7 @@ import ( ) type Consumer[O any] struct { - quitter async.AssistedQuitter + quitter async.AnnotatedWgQuitter RoutineName async.GoRoutineName OutputsChIn async.OutputStreamR[O] Count int @@ -16,7 +16,7 @@ type Consumer[O any] struct { func StartConsumer[O any]( ctx context.Context, - quitter async.AssistedQuitter, + quitter async.AnnotatedWgQuitter, outputsChIn async.OutputStreamR[O], ) *Consumer[O] { consumer := &Consumer[O]{ diff --git a/internal/helpers/test-producer.go b/internal/helpers/test-producer.go index 3453e23..05492b4 100644 --- a/internal/helpers/test-producer.go +++ b/internal/helpers/test-producer.go @@ -15,7 +15,7 @@ type terminationStream chan termination type ProviderFunc[I any] func() I type Producer[I, O any] struct { - quitter async.AssistedQuitter + quitter async.AnnotatedWgQuitter RoutineName async.GoRoutineName sequenceNo int provider ProviderFunc[I] @@ -30,7 +30,7 @@ type Producer[I, O any] struct { // indicate end of the work load. func StartProducer[I, O any]( ctx context.Context, - quitter async.AssistedQuitter, + quitter async.AnnotatedWgQuitter, capacity int, provider ProviderFunc[I], delay int,