-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathresult.go
112 lines (87 loc) · 3.83 KB
/
result.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
package pond
import (
"context"
"github.com/alitto/pond/v2/internal/future"
)
// ResultPool is a pool that can be used to submit tasks that return a result.
type ResultPool[R any] interface {
basePool
// Submits a task to the pool and returns a future that can be used to wait for the task to complete and get the result.
// The pool will not accept new tasks after it has been stopped.
// If the pool has been stopped, this method will return ErrPoolStopped.
Submit(task func() R) Result[R]
// Submits a task to the pool and returns a future that can be used to wait for the task to complete and get the result.
// The task function must return a result and an error.
// The pool will not accept new tasks after it has been stopped.
// If the pool has been stopped, this method will return ErrPoolStopped.
SubmitErr(task func() (R, error)) Result[R]
// Attempts to submit a task to the pool and returns a future that can be used to wait for the task to complete
// and a boolean indicating whether the task was submitted successfully.
// The pool will not accept new tasks after it has been stopped.
// If the pool has been stopped, this method will return ErrPoolStopped.
TrySubmit(task func() R) (Result[R], bool)
// Attempts to submit a task to the pool and returns a future that can be used to wait for the task to complete
// and a boolean indicating whether the task was submitted successfully.
// The task function must return a result and an error.
// The pool will not accept new tasks after it has been stopped.
// If the pool has been stopped, this method will return ErrPoolStopped.
TrySubmitErr(task func() (R, error)) (Result[R], bool)
// Creates a new subpool with the specified maximum concurrency and options.
NewSubpool(maxConcurrency int, options ...Option) ResultPool[R]
// Creates a new task group.
NewGroup() ResultTaskGroup[R]
// Creates a new task group with the specified context.
NewGroupContext(ctx context.Context) ResultTaskGroup[R]
}
type resultPool[R any] struct {
*pool
}
func (p *resultPool[R]) NewGroup() ResultTaskGroup[R] {
return newResultTaskGroup[R](p.pool, p.Context())
}
func (p *resultPool[R]) NewGroupContext(ctx context.Context) ResultTaskGroup[R] {
return newResultTaskGroup[R](p.pool, ctx)
}
func (p *resultPool[R]) Submit(task func() R) Result[R] {
future, _ := p.submit(task, p.nonBlocking)
return future
}
func (p *resultPool[R]) SubmitErr(task func() (R, error)) Result[R] {
future, _ := p.submit(task, p.nonBlocking)
return future
}
func (p *resultPool[R]) TrySubmit(task func() R) (Result[R], bool) {
return p.submit(task, true)
}
func (p *resultPool[R]) TrySubmitErr(task func() (R, error)) (Result[R], bool) {
return p.submit(task, true)
}
func (p *resultPool[R]) submit(task any, nonBlocking bool) (Result[R], bool) {
future, resolve := future.NewValueFuture[R](p.Context())
if p.Stopped() {
var zero R
resolve(zero, ErrPoolStopped)
return future, false
}
wrapped := wrapTask[R, func(R, error)](task, resolve)
if err := p.pool.submit(wrapped, nonBlocking); err != nil {
var zero R
resolve(zero, err)
return future, false
}
return future, true
}
func (p *resultPool[R]) NewSubpool(maxConcurrency int, options ...Option) ResultPool[R] {
return newResultPool[R](maxConcurrency, p.pool, options...)
}
func newResultPool[R any](maxConcurrency int, parent *pool, options ...Option) *resultPool[R] {
return &resultPool[R]{
pool: newPool(maxConcurrency, parent, options...),
}
}
// NewResultPool creates a new result pool with the given maximum concurrency and options.
// Result pools are generic pools that can be used to submit tasks that return a result.
// The new maximum concurrency must be greater than or equal to 0 (0 means no limit).
func NewResultPool[R any](maxConcurrency int, options ...Option) ResultPool[R] {
return newResultPool[R](maxConcurrency, nil, options...)
}