-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
222 lines (185 loc) · 4.38 KB
/
task.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
package task
import (
"context"
"errors"
"sync"
"time"
)
var (
ErrRunnerClosed = errors.New("runner is closed")
ErrRunnerAlreadyClosed = errors.New("runner is already closed")
)
// Future represents a task that will be executed in the future.
// The Await function will block until the task is completed.
// If the context passed to the Await function is canceled, the Await function will return but the task will still be executed.
// if the task needs to be canceled, the context passed to the Submit function should be canceled.
type Future interface {
Await(ctx context.Context) error
}
type futureFunc func(ctx context.Context) error
func (f futureFunc) Await(ctx context.Context) error {
return f(ctx)
}
type task struct {
ctx context.Context
fn func(ctx context.Context) error
err chan error
}
type Runner interface {
// Submit a task to be executed by the runner's workers. the context passed to the Submit function will be merged with the runner's context.
// and the merged context will be passed to the submitted function. This ensures that if the runner is closed, the submitted function will
// be canceled.
Submit(ctx context.Context, fn func(context.Context) error) Future
// Close the runner and wait for all the submitted tasks to be completed.
// Calling this function again after closure will return ErrRunnerAlreadyClosed.
Close(ctx context.Context) error
}
type runner struct {
workerSize int
bufferSize int
tasks chan *task
closed chan struct{}
wg sync.WaitGroup
}
var _ Runner = (*runner)(nil)
// The given context will be passed to the submitted function.
func (r *runner) Submit(ctx context.Context, fn func(context.Context) error) Future {
err := make(chan error, 1)
futureErr := make(chan error, 1)
select {
case <-r.closed:
err <- ErrRunnerClosed
default:
r.tasks <- &task{
ctx: ctx,
fn: fn,
err: err,
}
}
go func() {
for {
select {
case <-r.closed:
futureErr <- ErrRunnerClosed
return
case <-ctx.Done():
futureErr <- ctx.Err()
return
case e := <-err:
if yeild, ok := e.(*yeild); ok {
if yeild.delay > 0 {
select {
case <-ctx.Done():
futureErr <- ctx.Err()
return
case <-time.After(yeild.delay):
}
}
r.tasks <- &task{
ctx: yeild.ctx,
fn: fn,
err: err,
}
} else {
futureErr <- e
return
}
}
}
}()
return futureFunc(func(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case e := <-futureErr:
return e
}
})
}
func (r *runner) Close(ctx context.Context) error {
select {
case <-r.closed:
return ErrRunnerAlreadyClosed
default:
close(r.tasks)
close(r.closed)
}
r.wg.Wait()
return nil
}
type runnerOpt func(*runner)
// WithWorkerSize sets the number of workers that will be used to execute the submitted tasks.
// if the worker size is less than or equal to 0, it will be set to 1.
func WithWorkerSize(worker int) runnerOpt {
return func(r *runner) {
if worker < 0 {
worker = 1
}
r.workerSize = worker
}
}
// WithBufferSize sets the buffer size of the tasks channel.
// if the buffer size is less than 0, it will be set to 1.
func WithBufferSize(bufferSize int) runnerOpt {
return func(r *runner) {
if bufferSize < 0 {
bufferSize = 1
}
r.bufferSize = bufferSize
}
}
// NewRunner creates a new runner with the given options.
// The default worker size is 10 and the default buffer size is 100.
func NewRunner(opts ...runnerOpt) Runner {
r := &runner{
workerSize: 10,
bufferSize: 100,
closed: make(chan struct{}),
}
for _, opt := range opts {
opt(r)
}
r.tasks = make(chan *task, r.bufferSize)
r.wg.Add(r.workerSize)
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-r.closed
cancel()
}()
for range r.workerSize {
go func() {
defer r.wg.Done()
for {
task, ok := <-r.tasks
if !ok {
return
}
task.err <- task.fn(NewMergedContext(task.ctx, ctx))
}
}()
}
return r
}
type yeild struct {
ctx context.Context
delay time.Duration
}
var _ error = (*yeild)(nil)
func (y *yeild) Error() string {
return ""
}
type yeildOpt func(*yeild)
func WithDelay(delay time.Duration) yeildOpt {
return func(y *yeild) {
y.delay = delay
}
}
func Yeild(ctx context.Context, opts ...yeildOpt) *yeild {
y := &yeild{
ctx: ctx,
}
for _, opt := range opts {
opt(y)
}
return y
}