...
1 package pool
2
3 import (
4 "context"
5 )
6
7
8
9
10
11
12
13
14 type ResultContextPool[T any] struct {
15 contextPool ContextPool
16 agg resultAggregator[T]
17 collectErrored bool
18 }
19
20
21
22 func (p *ResultContextPool[T]) Go(f func(context.Context) (T, error)) {
23 p.contextPool.Go(func(ctx context.Context) error {
24 res, err := f(ctx)
25 if err == nil || p.collectErrored {
26 p.agg.add(res)
27 }
28 return err
29 })
30 }
31
32
33
34 func (p *ResultContextPool[T]) Wait() ([]T, error) {
35 err := p.contextPool.Wait()
36 return p.agg.results, err
37 }
38
39
40
41
42 func (p *ResultContextPool[T]) WithCollectErrored() *ResultContextPool[T] {
43 p.panicIfInitialized()
44 p.collectErrored = true
45 return p
46 }
47
48
49
50 func (p *ResultContextPool[T]) WithFirstError() *ResultContextPool[T] {
51 p.panicIfInitialized()
52 p.contextPool.WithFirstError()
53 return p
54 }
55
56
57
58
59 func (p *ResultContextPool[T]) WithCancelOnError() *ResultContextPool[T] {
60 p.panicIfInitialized()
61 p.contextPool.WithCancelOnError()
62 return p
63 }
64
65
66
67 func (p *ResultContextPool[T]) WithMaxGoroutines(n int) *ResultContextPool[T] {
68 p.panicIfInitialized()
69 p.contextPool.WithMaxGoroutines(n)
70 return p
71 }
72
73 func (p *ResultContextPool[T]) panicIfInitialized() {
74 p.contextPool.panicIfInitialized()
75 }
76
View as plain text