Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iter-err: add ForEachErr and ForEachIdxErr #104

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refine ForEachIdxErr
  • Loading branch information
collateral-damager committed Apr 26, 2024
commit 0bfcec1d8d92cf6484c5696ba7c765b18c75a14f
14 changes: 8 additions & 6 deletions iter/iter.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package iter

import (
"errors"
"runtime"
"sync"
"sync/atomic"

"github.com/sourcegraph/conc"
"github.com/sourcegraph/conc/internal/multierror"
)

// defaultMaxGoroutines returns the default maximum number of
Expand Down Expand Up @@ -127,7 +127,7 @@ func (iter Iterator[T]) ForEachIdxErr(input []T, f func(int, *T) error) error {
iter.MaxGoroutines = numInput
}

var errs error
var errs []error
var errsMu sync.Mutex
var idx atomic.Int64
var failed atomic.Bool
Expand All @@ -137,9 +137,11 @@ func (iter Iterator[T]) ForEachIdxErr(input []T, f func(int, *T) error) error {
i := int(idx.Add(1) - 1)
for ; i < numInput && !failed.Load(); i = int(idx.Add(1) - 1) {
if err := f(i, &input[i]); err != nil {
errsMu.Lock()
errs = multierror.Join(errs, err)
errsMu.Unlock()
if alreadyFailedFast := failed.Swap(iter.FailFast); !alreadyFailedFast {
errsMu.Lock()
errs = append(errs, err)
errsMu.Unlock()
}

failed.Store(iter.FailFast)
}
Expand All @@ -152,5 +154,5 @@ func (iter Iterator[T]) ForEachIdxErr(input []T, f func(int, *T) error) error {
}
wg.Wait()

return errs
return errors.Join(errs...)
}
30 changes: 15 additions & 15 deletions iter/iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ func TestForIterator_EachIdxErr(t *testing.T) {
t.Parallel()

t.Run("failFast=false", func(t *testing.T) {
it := Iterator[int]{MaxGoroutines: 999}
it := iter.Iterator[int]{MaxGoroutines: 999}
forEach := noIndex(it.ForEachIdxErr)
testForEachErr(t, false, forEach)
})

t.Run("failFast=true", func(t *testing.T) {
it := Iterator[int]{MaxGoroutines: 999}
it := iter.Iterator[int]{MaxGoroutines: 999}
forEach := noIndex(it.ForEachIdxErr)
testForEachErr(t, true, forEach)
})
Expand All @@ -190,7 +190,7 @@ func TestForIterator_EachIdxErr(t *testing.T) {

input := []int{1, 2, 3, 4, 5}
errTest := errors.New("test error")
iterator := Iterator[int]{MaxGoroutines: 1, FailFast: true}
iterator := iter.Iterator[int]{MaxGoroutines: 1, FailFast: true}

var mu sync.Mutex
var results []int
Expand All @@ -211,7 +211,7 @@ func TestForIterator_EachIdxErr(t *testing.T) {
t.Run("safe for reuse", func(t *testing.T) {
t.Parallel()

iterator := Iterator[int]{MaxGoroutines: 999}
iterator := iter.Iterator[int]{MaxGoroutines: 999}

// iter.Concurrency > numInput case that updates iter.Concurrency
_ = iterator.ForEachIdxErr([]int{1, 2, 3}, func(i int, t *int) error {
Expand All @@ -224,12 +224,12 @@ func TestForIterator_EachIdxErr(t *testing.T) {
t.Run("allows more than defaultMaxGoroutines() concurrent tasks", func(t *testing.T) {
t.Parallel()

wantConcurrency := 2 * defaultMaxGoroutines()
wantConcurrency := 2 * iter.DefaultMaxGoroutines()

maxConcurrencyHit := make(chan struct{})

tasks := make([]int, wantConcurrency)
iterator := Iterator[int]{MaxGoroutines: wantConcurrency}
iterator := iter.Iterator[int]{MaxGoroutines: wantConcurrency}

var concurrentTasks atomic.Int64
_ = iterator.ForEachIdxErr(tasks, func(_ int, t *int) error {
Expand Down Expand Up @@ -257,19 +257,19 @@ func TestForIterator_EachErr(t *testing.T) {
t.Parallel()

t.Run("failFast=false", func(t *testing.T) {
it := Iterator[int]{MaxGoroutines: 999}
it := iter.Iterator[int]{MaxGoroutines: 999}
testForEachErr(t, false, it.ForEachErr)
})

t.Run("failFast=true", func(t *testing.T) {
it := Iterator[int]{MaxGoroutines: 999}
it := iter.Iterator[int]{MaxGoroutines: 999}
testForEachErr(t, true, it.ForEachErr)
})

t.Run("safe for reuse", func(t *testing.T) {
t.Parallel()

iterator := Iterator[int]{MaxGoroutines: 999}
iterator := iter.Iterator[int]{MaxGoroutines: 999}

// iter.Concurrency > numInput case that updates iter.Concurrency
_ = iterator.ForEachErr([]int{1, 2, 3}, func(t *int) error {
Expand All @@ -284,7 +284,7 @@ func TestForIterator_EachErr(t *testing.T) {

input := []int{1, 2, 3, 4, 5}
errTest := errors.New("test error")
iterator := Iterator[int]{MaxGoroutines: 1, FailFast: true}
iterator := iter.Iterator[int]{MaxGoroutines: 1, FailFast: true}

var mu sync.Mutex
var results []int
Expand All @@ -305,12 +305,12 @@ func TestForIterator_EachErr(t *testing.T) {
t.Run("allows more than defaultMaxGoroutines() concurrent tasks", func(t *testing.T) {
t.Parallel()

wantConcurrency := 2 * defaultMaxGoroutines()
wantConcurrency := 2 * iter.DefaultMaxGoroutines()

maxConcurrencyHit := make(chan struct{})

tasks := make([]int, wantConcurrency)
iterator := Iterator[int]{MaxGoroutines: wantConcurrency}
iterator := iter.Iterator[int]{MaxGoroutines: wantConcurrency}

var concurrentTasks atomic.Int64
_ = iterator.ForEachErr(tasks, func(t *int) error {
Expand Down Expand Up @@ -338,7 +338,7 @@ func TestForEachIdxErr(t *testing.T) {
t.Parallel()

t.Run("standart", func(t *testing.T) {
forEach := noIndex(ForEachIdxErr[int])
forEach := noIndex(iter.ForEachIdxErr[int])
testForEachErr(t, false, forEach)
})

Expand All @@ -347,7 +347,7 @@ func TestForEachIdxErr(t *testing.T) {
got := []int{}
gotMu := sync.Mutex{}

err := ForEachIdxErr(ints, func(i int, _ *int) error {
err := iter.ForEachIdxErr(ints, func(i int, _ *int) error {
gotMu.Lock()
defer gotMu.Unlock()
got = append(got, i)
Expand All @@ -362,7 +362,7 @@ func TestForEachIdxErr(t *testing.T) {
func TestForEachErr(t *testing.T) {
t.Parallel()

testForEachErr(t, false, ForEachErr[int])
testForEachErr(t, false, iter.ForEachErr[int])
}

// noIndex converts a ForEachIdxErr function (or method) into a ForEachErr function (or method).
Expand Down