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 all commits
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
73 changes: 73 additions & 0 deletions iter/iter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package iter

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

"github.com/sourcegraph/conc"
Expand All @@ -22,6 +24,10 @@ type Iterator[T any] struct {
//
// If unset, MaxGoroutines defaults to runtime.GOMAXPROCS(0).
MaxGoroutines int

// FailFast controls whether the ForEach*Err methods should
// stop iterating over the input if an error is returned by callback.
FailFast bool
}

// ForEach executes f in parallel over each element in input.
Expand Down Expand Up @@ -54,6 +60,19 @@ func (iter Iterator[T]) ForEach(input []T, f func(*T)) {
// index of the element to the callback.
func ForEachIdx[T any](input []T, f func(int, *T)) { Iterator[T]{}.ForEachIdx(input, f) }

// ForEachIdxErr is the same as ForEachIdx except it returns an error.
// This function will not stop iterating over the input if an error is returned by callback.
// All returned errors will be returned as a multierror.
func ForEachIdxErr[T any](input []T, f func(int, *T) error) error {
return Iterator[T]{}.ForEachIdxErr(input, f)
}

// ForEachErr is the same as ForEach except it returns an error.
// This function will not stop iterating over the input if an error is returned by callback.
func ForEachErr[T any](input []T, f func(*T) error) error {
return Iterator[T]{}.ForEachErr(input, f)
}

// ForEachIdx is the same as ForEach except it also provides the
// index of the element to the callback.
func (iter Iterator[T]) ForEachIdx(input []T, f func(int, *T)) {
Expand Down Expand Up @@ -83,3 +102,57 @@ func (iter Iterator[T]) ForEachIdx(input []T, f func(int, *T)) {
}
wg.Wait()
}

// ForEachErr is the same as ForEach except it returns an error.
// If Iterator.FailFast is true, ForEachErr will stop iterating on the first error.
// If Iterator.FailFast is false, ForEachErr will return all errors and iterate over the all the input.
func (iter Iterator[T]) ForEachErr(input []T, f func(*T) error) error {
return iter.ForEachIdxErr(input, func(_ int, t *T) error {
return f(t)
})
}

// ForEachIdxErr is the same as ForEachIdx except it returns an error.
// If Iterator.FailFast is true, ForEachIdxErr will stop iterating on the first error.
// If Iterator.FailFast is false, ForEachIdxErr will return all errors and iterate over the all the input.
func (iter Iterator[T]) ForEachIdxErr(input []T, f func(int, *T) error) error {
if iter.MaxGoroutines == 0 {
// iter is a value receiver and is hence safe to mutate
iter.MaxGoroutines = defaultMaxGoroutines()
}

numInput := len(input)
if iter.MaxGoroutines > numInput {
// No more concurrent tasks than the number of input items.
iter.MaxGoroutines = numInput
}

var errs []error
var errsMu sync.Mutex
var idx atomic.Int64
var failed atomic.Bool

// Create the task outside the loop to avoid extra closure allocations.
task := func() {
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 {
if alreadyFailedFast := failed.Swap(iter.FailFast); !alreadyFailedFast {
errsMu.Lock()
errs = append(errs, err)
errsMu.Unlock()
}

failed.Store(iter.FailFast)
}
}
}

var wg conc.WaitGroup
for i := 0; i < iter.MaxGoroutines && !failed.Load(); i++ {
wg.Go(task)
}
wg.Wait()

return errors.Join(errs...)
}
Loading