diff --git a/pool/context_pool.go b/pool/context_pool.go index b2d7f8a..85c34e5 100644 --- a/pool/context_pool.go +++ b/pool/context_pool.go @@ -81,6 +81,16 @@ func (p *ContextPool) WithCancelOnError() *ContextPool { return p } +// WithFailFast is an alias for the combination of WithFirstError and +// WithCancelOnError. By default, the errors from all tasks are returned and +// the pool's context is not canceled until the parent context is canceled. +func (p *ContextPool) WithFailFast() *ContextPool { + p.panicIfInitialized() + p.WithFirstError() + p.WithCancelOnError() + return p +} + // WithMaxGoroutines limits the number of goroutines in a pool. // Defaults to unlimited. Panics if n < 1. func (p *ContextPool) WithMaxGoroutines(n int) *ContextPool { diff --git a/pool/context_pool_test.go b/pool/context_pool_test.go index 326e481..f9254b9 100644 --- a/pool/context_pool_test.go +++ b/pool/context_pool_test.go @@ -187,9 +187,9 @@ func TestContextPool(t *testing.T) { require.NotErrorIs(t, err, err2) }) - t.Run("WithFirstError and WithCancelOnError", func(t *testing.T) { + t.Run("WithFailFast", func(t *testing.T) { t.Parallel() - p := New().WithContext(bgctx).WithFirstError().WithCancelOnError() + p := New().WithContext(bgctx).WithFailFast() p.Go(func(ctx context.Context) error { return err1 }) diff --git a/pool/result_context_pool.go b/pool/result_context_pool.go index 55dc3bc..9cac635 100644 --- a/pool/result_context_pool.go +++ b/pool/result_context_pool.go @@ -62,6 +62,15 @@ func (p *ResultContextPool[T]) WithCancelOnError() *ResultContextPool[T] { return p } +// WithFailFast is an alias for the combination of WithFirstError and +// WithCancelOnError. By default, the errors from all tasks are returned and +// the pool's context is not canceled until the parent context is canceled. +func (p *ResultContextPool[T]) WithFailFast() *ResultContextPool[T] { + p.panicIfInitialized() + p.contextPool.WithFailFast() + return p +} + // WithMaxGoroutines limits the number of goroutines in a pool. // Defaults to unlimited. Panics if n < 1. func (p *ResultContextPool[T]) WithMaxGoroutines(n int) *ResultContextPool[T] { diff --git a/pool/result_context_pool_test.go b/pool/result_context_pool_test.go index a116d93..ad74b20 100644 --- a/pool/result_context_pool_test.go +++ b/pool/result_context_pool_test.go @@ -101,6 +101,22 @@ func TestResultContextPool(t *testing.T) { require.ErrorIs(t, err, err1) }) + t.Run("WithFailFast", func(t *testing.T) { + t.Parallel() + p := NewWithResults[int]().WithContext(context.Background()).WithFailFast() + p.Go(func(ctx context.Context) (int, error) { + return 0, err1 + }) + p.Go(func(ctx context.Context) (int, error) { + <-ctx.Done() + return 1, ctx.Err() + }) + results, err := p.Wait() + require.ErrorIs(t, err, err1) + require.NotErrorIs(t, err, context.Canceled) + require.Empty(t, results) + }) + t.Run("WithCancelOnError and panic", func(t *testing.T) { t.Parallel() p := NewWithResults[int]().