From f629e091b4e5b7536499441155f043ed2f97231e Mon Sep 17 00:00:00 2001 From: Carl Johnson Date: Wed, 21 Jun 2023 15:11:14 -0400 Subject: [PATCH] =?UTF-8?q?DoAll=20=E2=86=92=20DoEach?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++--- do.go | 2 +- do_all.go => do_each.go | 4 ++-- do_all_test.go => do_each_test.go | 4 ++-- example_test.go | 8 ++++---- panic_test.go | 4 ++-- 6 files changed, 14 insertions(+), 14 deletions(-) rename do_all.go => do_each.go (83%) rename do_all_test.go => do_each_test.go (76%) diff --git a/README.md b/README.md index f1a89c9..27e515d 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Workgroup is a generic Go library that provides a structured approach to concurrent programming. It lets you easily manage concurrent tasks in a manner that is predictable and scalable, and it provides a simple, yet effective approach to structuring concurrency. -Workgroup has a simple API consisting of three core functions: `Do`, `DoAll`, and `DoTasks`. It automatically handles spawning workers, collecting errors, and recovering from panics. +Workgroup has a simple API consisting of three core functions: `Do`, `DoEach`, and `DoTasks`. It automatically handles spawning workers, collecting errors, and recovering from panics. Workgroup requires Go 1.20+. @@ -80,7 +80,7 @@ err := errors.Join(errs...) ### Execute homogenous tasks -`workgroup.DoAll` is useful if you need to execute the same task on slice of items using a worker pool: +`workgroup.DoEach` is useful if you need to execute the same task on each item in a slice using a worker pool: @@ -93,7 +93,7 @@ err := errors.Join(errs...) ```go things := []someType{thingA, thingB, thingC} -err := workgroup.DoAll(numWorkers, things, +err := workgroup.DoEach(numWorkers, things, func(thing someType) error { foo := thing.Frobincate() return foo.DoSomething() diff --git a/do.go b/do.go index 78e010c..5869ca1 100644 --- a/do.go +++ b/do.go @@ -7,7 +7,7 @@ package workgroup // If a function panics during execution, // the panic will be caught and rethrown in the main Goroutine. func Do(n int, fns ...func() error) error { - return DoAll(n, fns, func(in func() error) error { + return DoEach(n, fns, func(in func() error) error { return in() }) } diff --git a/do_all.go b/do_each.go similarity index 83% rename from do_all.go rename to do_each.go index 687fb90..9d5ffc1 100644 --- a/do_all.go +++ b/do_each.go @@ -2,13 +2,13 @@ package workgroup import "errors" -// DoAll starts n concurrent workers (or GOMAXPROCS workers if n < 1) +// DoEach starts n concurrent workers (or GOMAXPROCS workers if n < 1) // and processes each initial input as a task. // Errors returned by a task do not halt execution, // but are joined into a multierror return value. // If a task panics during execution, // the panic will be caught and rethrown in the main Goroutine. -func DoAll[Input any](n int, items []Input, task func(Input) error) error { +func DoEach[Input any](n int, items []Input, task func(Input) error) error { var recovered any errs := make([]error, 0, len(items)) runner := func(in Input) (r any, err error) { diff --git a/do_all_test.go b/do_each_test.go similarity index 76% rename from do_all_test.go rename to do_each_test.go index 1c00dc5..31b3c00 100644 --- a/do_all_test.go +++ b/do_each_test.go @@ -7,10 +7,10 @@ import ( "github.com/carlmjohnson/workgroup" ) -func TestDoAll_err(t *testing.T) { +func TestDoEach_err(t *testing.T) { a := errors.New("a") b := errors.New("b") - errs := workgroup.DoAll(1, []int{1, 2, 3}, func(i int) error { + errs := workgroup.DoEach(1, []int{1, 2, 3}, func(i int) error { switch i { case 1: return a diff --git a/example_test.go b/example_test.go index 9498f24..7b5269a 100644 --- a/example_test.go +++ b/example_test.go @@ -101,14 +101,14 @@ func ExampleDoTasks() { // - / } -func ExampleDoAll() { +func ExampleDoEach() { times := []time.Duration{ 50 * time.Millisecond, 100 * time.Millisecond, 200 * time.Millisecond, } start := time.Now() - err := workgroup.DoAll(3, times, func(d time.Duration) error { + err := workgroup.DoEach(3, times, func(d time.Duration) error { time.Sleep(d) fmt.Println("slept", d) return nil @@ -150,7 +150,7 @@ func ExampleDo() { // executed concurrently? true } -func ExampleDoAll_cancel() { +func ExampleDoEach_cancel() { // To cancel execution early, communicate via a context.CancelFunc ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -179,7 +179,7 @@ func ExampleDoAll_cancel() { return nil } start := time.Now() - if err := workgroup.DoAll(3, times, task); err != nil { + if err := workgroup.DoEach(3, times, task); err != nil { fmt.Println("error", err) } fmt.Println("exited promptly?", time.Since(start) < 150*time.Millisecond) diff --git a/panic_test.go b/panic_test.go index a8728d6..51b97df 100644 --- a/panic_test.go +++ b/panic_test.go @@ -42,13 +42,13 @@ func TestDoTasks_panic(t *testing.T) { } } -func TestDoAll_panic(t *testing.T) { +func TestDoEach_panic(t *testing.T) { var ( n atomic.Int64 err error ) r := try(func() { - err = workgroup.DoAll(1, []int64{1, 2, 3}, + err = workgroup.DoEach(1, []int64{1, 2, 3}, func(delta int64) error { if delta == 2 { panic("boom")