File tree Expand file tree Collapse file tree 4 files changed +37
-2
lines changed Expand file tree Collapse file tree 4 files changed +37
-2
lines changed Original file line number Diff line number Diff line change @@ -81,6 +81,16 @@ func (p *ContextPool) WithCancelOnError() *ContextPool {
81
81
return p
82
82
}
83
83
84
+ // WithFailFast is an alias for the combination of WithFirstError and
85
+ // WithCancelOnError. By default, the errors from all tasks are returned and
86
+ // the pool's context is not canceled until the parent context is canceled.
87
+ func (p * ContextPool ) WithFailFast () * ContextPool {
88
+ p .panicIfInitialized ()
89
+ p .WithFirstError ()
90
+ p .WithCancelOnError ()
91
+ return p
92
+ }
93
+
84
94
// WithMaxGoroutines limits the number of goroutines in a pool.
85
95
// Defaults to unlimited. Panics if n < 1.
86
96
func (p * ContextPool ) WithMaxGoroutines (n int ) * ContextPool {
Original file line number Diff line number Diff line change @@ -187,9 +187,9 @@ func TestContextPool(t *testing.T) {
187
187
require .NotErrorIs (t , err , err2 )
188
188
})
189
189
190
- t .Run ("WithFirstError and WithCancelOnError " , func (t * testing.T ) {
190
+ t .Run ("WithFailFast " , func (t * testing.T ) {
191
191
t .Parallel ()
192
- p := New ().WithContext (bgctx ).WithFirstError (). WithCancelOnError ()
192
+ p := New ().WithContext (bgctx ).WithFailFast ()
193
193
p .Go (func (ctx context.Context ) error {
194
194
return err1
195
195
})
Original file line number Diff line number Diff line change @@ -62,6 +62,15 @@ func (p *ResultContextPool[T]) WithCancelOnError() *ResultContextPool[T] {
62
62
return p
63
63
}
64
64
65
+ // WithFailFast is an alias for the combination of WithFirstError and
66
+ // WithCancelOnError. By default, the errors from all tasks are returned and
67
+ // the pool's context is not canceled until the parent context is canceled.
68
+ func (p * ResultContextPool [T ]) WithFailFast () * ResultContextPool [T ] {
69
+ p .panicIfInitialized ()
70
+ p .contextPool .WithFailFast ()
71
+ return p
72
+ }
73
+
65
74
// WithMaxGoroutines limits the number of goroutines in a pool.
66
75
// Defaults to unlimited. Panics if n < 1.
67
76
func (p * ResultContextPool [T ]) WithMaxGoroutines (n int ) * ResultContextPool [T ] {
Original file line number Diff line number Diff line change @@ -101,6 +101,22 @@ func TestResultContextPool(t *testing.T) {
101
101
require .ErrorIs (t , err , err1 )
102
102
})
103
103
104
+ t .Run ("WithFailFast" , func (t * testing.T ) {
105
+ t .Parallel ()
106
+ p := NewWithResults [int ]().WithContext (context .Background ()).WithFailFast ()
107
+ p .Go (func (ctx context.Context ) (int , error ) {
108
+ return 0 , err1
109
+ })
110
+ p .Go (func (ctx context.Context ) (int , error ) {
111
+ <- ctx .Done ()
112
+ return 1 , ctx .Err ()
113
+ })
114
+ results , err := p .Wait ()
115
+ require .ErrorIs (t , err , err1 )
116
+ require .NotErrorIs (t , err , context .Canceled )
117
+ require .Empty (t , results )
118
+ })
119
+
104
120
t .Run ("WithCancelOnError and panic" , func (t * testing.T ) {
105
121
t .Parallel ()
106
122
p := NewWithResults [int ]().
You can’t perform that action at this time.
0 commit comments