-
Notifications
You must be signed in to change notification settings - Fork 2
/
collect.go
469 lines (424 loc) · 15.3 KB
/
collect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package parallel
import (
"context"
"runtime"
"sync/atomic"
)
// Executor that runs until the first error encountered
type ErrGroupExecutor interface {
// Go submits a task to the Executor, to be run at some point in the future.
//
// Panics if Wait() has already been called.
// May panic if any submitted task has already panicked.
Go(func(context.Context) error)
// Wait waits until all submitted tasks have completed, then returns one
// error if any errors were returned by submitted tasks (or nil).
//
// After waiting, panics if any submitted task panicked.
Wait() error
}
// Executor that collects all the return values of the operations, then returns
// the resulting slice or an error if any occurred.
type CollectingExecutor[T any] interface {
// Go submits a task to the Executor, to be run at some point in the future.
//
// Panics if Wait() has already been called.
// May panic if any submitted task has already panicked.
Go(func(context.Context) (T, error))
// Wait waits until all submitted tasks have completed, then returns a slice
// of the returned values from non-erring tasks or an error if any occurred.
//
// After waiting, panics if any submitted task panicked.
Wait() ([]T, error)
}
// Executor that feeds all the return values of the operations to a user
// function.
type FeedingExecutor[T any] interface {
// Go submits a task to the Executor, to be run at some point in the future.
//
// Panics if Wait() has already been called.
// May panic if any submitted task has already panicked.
Go(func(context.Context) (T, error))
// Wait waits until all running tasks have completed and all returned values
// from non-erring tasks have been processed by the receiver function, then
// returns an error if any occurred.
//
// After waiting, panics if any submitted task panicked.
Wait() error
}
// Executor that collects every error from the operations.
type AllErrsExecutor interface {
// Go submits a task to the Executor, to be run at some point in the future.
//
// Panics if Wait() has already been called.
// May panic if any submitted task has already panicked.
Go(func(context.Context) error)
// Wait waits until all submitted tasks have completed, then returns a
// MultiError of any errors that were returned by submitted tasks (or nil).
//
// After waiting, panics if any submitted task panicked.
Wait() MultiError
}
// Executor that collects the returned values and errors of the operations.
type CollectingAllErrsExecutor[T any] interface {
// Go submits a task to the Executor, to be run at some point in the future.
//
// Panics if Wait() has already been called.
// May panic if any submitted task has already panicked.
Go(func(context.Context) (T, error))
// Wait waits until all submitted tasks have completed, then returns a slice
// of the returned values from non-erring tasks and a MultiError of any
// errors returned (or nil).
//
// After waiting, panics if any submitted task panicked.
Wait() ([]T, MultiError)
}
// Executor that feeds all the return values of the operations to a user
// function and collects returned errors.
type FeedingAllErrsExecutor[T any] interface {
// Go submits a task to the Executor, to be run at some point in the future.
//
// Panics if Wait() has already been called.
// May panic if any submitted task has already panicked.
Go(func(context.Context) (T, error))
// Wait waits until all submitted tasks have completed and all returned
// values from non-erring tasks have been processed by the receiver
// function, then returns a MultiError of any errors that were returned by
// the tasks (or nil).
//
// After waiting, panics if any submitted task panicked.
Wait() MultiError
}
// Returns an executor that halts if any submitted task returns an error, and
// returns one error from Wait() if any occurred.
func ErrGroup(executor Executor) ErrGroupExecutor {
return &errGroup{executor}
}
// Returns an executor that collects all the return values from the functions
// provided, returning them (in no guaranteed order!) in a slice at the end.
//
// These executors are even best-effort safe against misuse: if the owner panics
// or otherwise forgets to call Wait(), the goroutines started by this executor
// should still be cleaned up.
func Collect[T any](executor Executor) CollectingExecutor[T] {
making := collectingGroup[T]{makePipeGroup[T, *[]T](executor)}
var outOfLineResults []T
making.res = &outOfLineResults
pipe := making.pipe // Don't capture a pointer to the executor
making.pipeWorkers.Go(func(context.Context) {
for item := range pipe {
outOfLineResults = append(outOfLineResults, item)
}
})
return making
}
// Returns an executor that collects all the return values from the functions
// provided, passing them all (in no guaranteed order!) to the provided
// receiver, which runs in a single goroutine by itself. In the event of an
// error from either the work functions or the receiver function, execution
// halts and the first error is returned.
//
// These executors are even best-effort safe against misuse: if the owner panics
// or otherwise forgets to call Wait(), the goroutines started by this executor
// should still be cleaned up.
func Feed[T any](executor Executor, receiver func(context.Context, T) error) FeedingExecutor[T] {
making := feedingGroup[T]{makePipeGroup[T, struct{}](executor)}
pipe := making.pipe // Don't capture a pointer to the executor
_, cancel := making.g.getContext()
making.pipeWorkers.Go(func(ctx context.Context) {
for val := range pipe {
if err := receiver(ctx, val); err != nil {
cancel(err)
for range pipe {
// Discard all future values
}
return
}
}
})
return making
}
// Returns an executor similar to parallel.ErrGroup, except instead of only
// returning the first error encountered it returns a MultiError of any & all
// errors encountered (or nil if none).
//
// These executors are even best-effort safe against misuse: if the owner panics
// or otherwise forgets to call Wait(), the goroutines started by this executor
// should still be cleaned up.
func GatherErrs(executor Executor) AllErrsExecutor {
making := multiErrGroup{makePipeGroup[error, *[]error](executor)}
var outOfLineErrs []error
making.res = &outOfLineErrs
pipe := making.pipe // Don't capture a pointer to the executor
making.pipeWorkers.Go(func(context.Context) {
for err := range pipe {
outOfLineErrs = append(outOfLineErrs, err)
}
})
return making
}
// Returns an executor that collects both values and a MultiError of any & all
// errors (or nil if none). Return values are not included in the results if
// that invocation returned an error. Execution does not stop if errors are
// encountered, only if there is a panic.
//
// These executors are even best-effort safe against misuse: if the owner panics
// or otherwise forgets to call Wait(), the goroutines started by this executor
// should still be cleaned up.
func CollectWithErrs[T any](executor Executor) CollectingAllErrsExecutor[T] {
making := collectingMultiErrGroup[T]{
makePipeGroup[withErr[T], *collectedResultWithErrs[T]](executor),
}
var outOfLineResults collectedResultWithErrs[T]
making.res = &outOfLineResults
pipe := making.pipe // Don't capture a pointer to the executor
making.pipeWorkers.Go(func(context.Context) {
for item := range pipe {
if item.err != nil {
outOfLineResults.errs = append(outOfLineResults.errs, item.err)
} else {
outOfLineResults.values = append(outOfLineResults.values, item.value)
}
}
})
return making
}
// Returns an executor that collects all the return values from the functions
// provided, passing them all (in no guaranteed order!) to the provided
// receiver, which runs in a single goroutine by itself. Execution does not stop
// if errors are encountered from either the work functions or the receiver
// function; those errors are all combined into the MultiError returned by
// Wait().
//
// These executors are even best-effort safe against misuse: if the owner panics
// or otherwise forgets to call Wait(), the goroutines started by this executor
// should still be cleaned up.
func FeedWithErrs[T any](executor Executor, receiver func(context.Context, T) error) FeedingAllErrsExecutor[T] {
making := feedingMultiErrGroup[T]{makePipeGroup[withErr[T], *[]error](executor)}
var outOfLineResults []error
making.res = &outOfLineResults
pipe := making.pipe // Don't capture a pointer to the executor
making.pipeWorkers.Go(func(ctx context.Context) {
for pair := range pipe {
if pair.err != nil {
outOfLineResults = append(outOfLineResults, pair.err)
} else if processErr := receiver(ctx, pair.value); processErr != nil {
outOfLineResults = append(outOfLineResults, processErr)
}
}
})
return making
}
// groupError returns the error associated with a group's context; if the error
// was errGroupDone, that doesn't count as an error and nil is returned instead.
func groupError(ctx context.Context) error {
err := context.Cause(ctx)
// We are explicitly using == here to check for the exact value of our
// sentinel error, not using errors.Is(), because we don't actually want to
// find it if it's in wrapped errors. We *only* want to know whether the
// cancelation error is *exactly* errGroupDone.
if err == errGroupDone {
return nil
}
return err
}
var _ ErrGroupExecutor = &errGroup{}
type errGroup struct {
g Executor
}
func (eg *errGroup) Go(op func(context.Context) error) {
_, cancel := eg.g.getContext() // Don't capture a pointer to the group
eg.g.Go(func(ctx context.Context) {
err := op(ctx)
if err != nil {
cancel(err)
}
})
}
func (eg *errGroup) Wait() error {
eg.g.Wait()
ctx, _ := eg.g.getContext()
return groupError(ctx)
}
func makePipeGroup[T any, R any](executor Executor) *pipeGroup[T, R] {
making := &pipeGroup[T, R]{
g: executor,
pipeWorkers: makeGroup(executor.getContext()), // use the same context for the pipe group
pipe: make(chan T, bufferSize),
}
runtime.SetFinalizer(making, func(doomed *pipeGroup[T, R]) {
close(doomed.pipe)
})
return making
}
// Underlying implementation for executors that handle results.
//
// T is the type that goes through the pipe, and R is the return value field we
// are collecting into
type pipeGroup[T any, R any] struct {
// All the constituent parts of this struct are out-of-line so that none of
// the goroutines doing work for it need to hold a reference to any of this
// memory. Thus, if the user forgets to call Wait(), we can hook the GC
// finalizer and ensure that the channels are closed and the goroutines we
// were running get cleaned up.
g Executor
pipeWorkers *group
pipe chan T
awaited atomic.Bool
res R
}
func sendToPipe[T any](pipe chan T, val T) {
defer func() {
if recover() != nil {
panic("parallel executor pipe error: a collector using this " +
"same executor was probably not awaited")
}
}()
pipe <- val
}
func (pg *pipeGroup[T, R]) doWait() {
// This function sucks to look at because go has no concept of scoped
// lifetime other than function-scope. You can only ensure something happens
// even in case of a panic by deferring it, and that always only happens at
// the end of the function... so, we just put an inner function here to make
// it happen "early."
// Runs last: We must make completely certain that we cancel the context
// owned by the pipeGroup. This context is shared between the executor and
// the pipeWorkers; we take charge of making sure this cancelation happens
// as soon as possible here, and we want it to happen at the very end after
// everything else in case something else wanted to set the cancel cause of
// the context to an actual error instead of our "no error" sentinel value.
defer pg.pipeWorkers.cancel(errGroupDone)
func() {
// Runs second: Close the results chan and unblock the pipe worker.
// Because we're deferring this, it will happen even if there is a panic
defer func() {
if !pg.awaited.Swap(true) {
close(pg.pipe)
// Don't try to close this chan again :)
runtime.SetFinalizer(pg, nil)
}
}()
// Runs first: Wait for inputs. Wait "quietly", not canceling the
// context yet so if there is an error later we can still see it
pg.g.waitWithoutCanceling()
}()
// Runs third: Wait for outputs to be done
pg.pipeWorkers.waitWithoutCanceling()
}
var _ CollectingExecutor[int] = collectingGroup[int]{}
type collectingGroup[T any] struct {
*pipeGroup[T, *[]T]
}
func (cg collectingGroup[T]) Go(op func(context.Context) (T, error)) {
pipe := cg.pipe // Don't capture a pointer to the group
_, cancel := cg.g.getContext()
cg.g.Go(func(ctx context.Context) {
val, err := op(ctx)
if err != nil {
cancel(err)
return
}
sendToPipe(pipe, val)
})
}
func (cg collectingGroup[T]) Wait() ([]T, error) {
cg.doWait()
ctx, _ := cg.g.getContext()
if err := groupError(ctx); err != nil {
// We have an error; return it
return nil, err
}
return *cg.res, nil
}
var _ FeedingExecutor[int] = feedingGroup[int]{}
type feedingGroup[T any] struct {
*pipeGroup[T, struct{}]
}
func (fg feedingGroup[T]) Go(op func(context.Context) (T, error)) {
pipe := fg.pipe // Don't capture a pointer to the group
_, cancel := fg.g.getContext()
fg.g.Go(func(ctx context.Context) {
val, err := op(ctx)
if err != nil {
cancel(err)
return
}
sendToPipe(pipe, val)
})
}
func (fg feedingGroup[T]) Wait() error {
fg.doWait()
ctx, _ := fg.g.getContext()
return groupError(ctx)
}
var _ AllErrsExecutor = multiErrGroup{}
type multiErrGroup struct {
*pipeGroup[error, *[]error]
}
func (meg multiErrGroup) Go(op func(context.Context) error) {
pipe := meg.pipe // Don't capture a pointer to the group
meg.g.Go(func(ctx context.Context) {
// Only send non-nil errors to the results pipe
if err := op(ctx); err != nil {
sendToPipe(pipe, err)
}
})
}
func (meg multiErrGroup) Wait() MultiError {
meg.doWait()
err := CombineErrors(*meg.res...)
ctx, _ := meg.g.getContext()
if cause := groupError(ctx); cause != nil {
return CombineErrors(cause, err)
}
return err
}
var _ CollectingAllErrsExecutor[int] = collectingMultiErrGroup[int]{}
type withErr[T any] struct {
value T
err error
}
type collectedResultWithErrs[T any] struct {
values []T
errs []error
}
type collectingMultiErrGroup[T any] struct {
*pipeGroup[withErr[T], *collectedResultWithErrs[T]]
}
func (ceg collectingMultiErrGroup[T]) Go(op func(context.Context) (T, error)) {
pipe := ceg.pipe // Don't capture a pointer to the group
ceg.g.Go(func(ctx context.Context) {
value, err := op(ctx)
sendToPipe(pipe, withErr[T]{value, err})
})
}
func (ceg collectingMultiErrGroup[T]) Wait() ([]T, MultiError) {
ceg.doWait()
res, err := ceg.res.values, CombineErrors(ceg.res.errs...)
ctx, _ := ceg.g.getContext()
if cause := groupError(ctx); cause != nil {
return res, CombineErrors(cause, err)
}
return res, err
}
var _ FeedingAllErrsExecutor[int] = feedingMultiErrGroup[int]{}
type feedingMultiErrGroup[T any] struct {
*pipeGroup[withErr[T], *[]error]
}
func (feg feedingMultiErrGroup[T]) Go(op func(context.Context) (T, error)) {
pipe := feg.pipe // Don't capture a pointer to the group
feg.g.Go(func(ctx context.Context) {
value, err := op(ctx)
sendToPipe(pipe, withErr[T]{value, err})
})
}
func (feg feedingMultiErrGroup[T]) Wait() MultiError {
feg.doWait()
err := CombineErrors(*feg.res...)
ctx, _ := feg.g.getContext()
if cause := groupError(ctx); cause != nil {
return CombineErrors(cause, err)
}
return err
}