Skip to content
This repository has been archived by the owner on Jul 7, 2023. It is now read-only.

Commit

Permalink
DoAll → DoEach
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed Jun 21, 2023
1 parent e6d366d commit f629e09
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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+.

Expand Down Expand Up @@ -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:

<table>
<tr>
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion do.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
}
4 changes: 2 additions & 2 deletions do_all.go → do_each.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions do_all_test.go → do_each_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions panic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit f629e09

Please sign in to comment.