-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner_impl.go
executable file
·225 lines (204 loc) · 5.54 KB
/
runner_impl.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
/*
* Copyright 2022 Monime Ltd, licensed under the
* Apache License, Version 2.0 (the "License");
*/
package gotasks
import (
"context"
"errors"
"github.com/monime-lab/gotries"
"sync"
)
var (
_ TaskRunner = &taskRunner{}
ErrNoTasks = errors.New("no tasks in runner")
)
type (
RunnerOption interface {
applyRunnerOption(r *taskRunner)
}
runnerOptionFunc func(r *taskRunner)
)
func (f runnerOptionFunc) applyRunnerOption(r *taskRunner) {
f(r)
}
// WithEagerFail is a fail fast switch useful
// when calling runner.RunAndWaitAll()
// The call will return on the first failure
func WithEagerFail(enable bool) RunnerOption {
return runnerOptionFunc(func(r *taskRunner) {
r.eagerFail = enable
})
}
// WithRetryOptions sets the default retry options for all the added tasks
func WithRetryOptions(options ...gotries.Option) RunnerOption {
return runnerOptionFunc(func(r *taskRunner) {
r.retryOptions = append(r.retryOptions, options...)
})
}
// WithSequentialParallelism is a syntactic sugar to WithMaxParallelism(1).
// Useful for executing multiple tasks serially
func WithSequentialParallelism() RunnerOption {
return WithMaxParallelism(1)
}
// WithMaxParallelism sets the maximum parallelism for the runner;
// if max is less than 1, then no parallelism limit is set
// This is a concurrency rate-limiter for when the number of tasks
// can be high. At any point, there are at most `max`
// task (goroutines) running concurrently.
func WithMaxParallelism(max int) RunnerOption {
var permit Permits = noopPermit{}
if max >= 1 {
permit = newSemaphorePermit(max)
}
return runnerOptionFunc(func(r *taskRunner) {
r.permits = permit
})
}
func NewTaskRunner(options ...RunnerOption) TaskRunner {
runner := &taskRunner{
eagerFail: false,
permits: noopPermit{},
tasks: make([]RunnerTask, 0),
taskRetryOptions: make([][]gotries.Option, 0),
}
for _, option := range options {
option.applyRunnerOption(runner)
}
return runner
}
type (
taskRunner struct {
lock sync.Mutex
eagerFail bool
permits Permits
tasks []RunnerTask
retryOptions []gotries.Option
taskRetryOptions [][]gotries.Option
}
)
func (r *taskRunner) AddRunnableTask(runnable Runnable, options ...gotries.Option) TaskRunner {
return r.AddTask(runnable, options...)
}
func (r *taskRunner) AddCallableTask(callable Callable, options ...gotries.Option) TaskRunner {
return r.AddTask(callable, options...)
}
func (r *taskRunner) AddTask(task RunnerTask, options ...gotries.Option) TaskRunner {
r.lock.Lock()
defer r.lock.Unlock()
r.tasks = append(r.tasks, task)
options = r.defaultRetry(options)
r.taskRetryOptions = append(r.taskRetryOptions, options)
return r
}
func (r *taskRunner) RunAndWaitAll(ctx context.Context) ([]interface{}, error) {
r.lock.Lock()
defer r.lock.Unlock()
taskCount := len(r.tasks)
if taskCount == 0 {
return nil, ErrNoTasks
}
var err error
cancel, resultChan, errChan := r.runTasks(ctx)
defer cancel()
results := make([]interface{}, taskCount)
for i := 0; i < taskCount; i++ {
select {
case res := <-resultChan:
results[res.position] = res.value
case err2 := <-errChan:
err = errors.Join(err, err2)
if r.eagerFail {
return nil, err
}
case <-ctx.Done():
err = errors.Join(err, ctx.Err())
}
}
return results, err
}
func (r *taskRunner) RunAndWaitAny(ctx context.Context) (interface{}, error) {
r.lock.Lock()
defer r.lock.Unlock()
taskCount := len(r.tasks)
if taskCount == 0 {
return nil, ErrNoTasks
}
var err error
cancel, resultChan, errChan := r.runTasks(ctx)
defer cancel()
for i := 0; i < taskCount; i++ {
select {
case res := <-resultChan:
return res.value, nil
case err2 := <-errChan:
err = errors.Join(err, err2)
case <-ctx.Done():
err = errors.Join(err, ctx.Err())
}
}
return nil, err
}
func (r *taskRunner) runTasks(ctx context.Context) (context.CancelFunc, chan result, chan error) {
wg := &sync.WaitGroup{}
errChan := make(chan error, len(r.tasks))
resultChan := make(chan result, len(r.tasks))
runCtx, cancel := context.WithCancel(ctx)
for pos, t := range r.tasks {
options := r.taskRetryOptions[pos]
wrapper := &taskWrapper{
pos: pos, task: t,
retryOptions: options,
}
// limits goroutine creation with the permit
if err := r.permits.Acquire(runCtx); err != nil {
errChan <- err
continue
}
wg.Add(1)
go r.runTask(runCtx, wg, wrapper, resultChan, errChan)
}
go func() {
wg.Wait()
close(errChan)
close(resultChan)
}()
return cancel, resultChan, errChan
}
func (r *taskRunner) runTask(
ctx context.Context, wg *sync.WaitGroup,
wrapper *taskWrapper, resultChan chan result, errChan chan error,
) {
defer func(c context.Context) {
r.permits.Release(c)
wg.Done()
}(ctx)
res, err := gotries.Call(ctx,
func(state gotries.State) (interface{}, error) {
return wrapper.task.Run(state.Context())
}, wrapper.retryOptions...)
// Don't look for nil result as an indication of success,
// AddRunnableTask tasks are wrapped to return nil result on success
if err != nil {
errChan <- err
} else {
resultChan <- result{position: wrapper.pos, value: res}
}
}
func (r *taskRunner) defaultRetry(options []gotries.Option) []gotries.Option {
options = append(r.retryOptions, options...)
if len(options) == 0 {
// if no retry options where set, disable retry
options = append(options, gotries.WithMaxAttempts(0))
}
return options
}
type taskWrapper struct {
pos int
task RunnerTask
retryOptions []gotries.Option
}
type result struct {
position int
value interface{}
}