-
Notifications
You must be signed in to change notification settings - Fork 1
/
concurrent_executor.go
289 lines (248 loc) · 9.4 KB
/
concurrent_executor.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
package conexec
import (
"context"
"sync"
"github.com/pkg/errors"
)
/*
If `ExecutorArgs` is valid, it will be passed into `Executor` func.
{
...
task.Executor(ctx, task.ExecutorArgs...)
}
Then in `Executor` function, we can get args and parse type to use to call another func (avoid Captured Closure):
{
...
task.Executor = func(ctx context.Context, args ...interface{}) (interface{}, error) {
firstArg, ok := args[0].(type)
if !ok {
return "", errors.Errorf("parse error")
}
// Do something with firstArg ...
return firstArg, nil
}
}
*/
type Task struct {
ID string
Executor TaskExecutor
ExecutorArgs []interface{}
}
// `TaskExecutor` is a function signature representing an executable task.
// It takes a context and variable arguments (args) and returns the result
// of the task execution along with any encountered error.
// The context can be used for cancellation and deadline propagation.
type TaskExecutor func(ctx context.Context, args ...interface{}) (interface{}, error)
// `TaskResponse` represents the result of a task execution.
// It includes the `TaskID` to uniquely identify the corresponding task,
// the `Value` holding the result of the task execution, and the `Error` containing
// any error encountered during execution.
type TaskResponse struct {
TaskID string
// `Value` holds the result of the task execution.
Value interface{}
// `Error` contains any error encountered during the task execution.
Error error
}
/*
`ConcurrentExecutor` struct facilitates the concurrent execution of multiple tasks by managing its own task queue.
It follows specific rules for task execution:
- If there are currently N tasks running concurrently, incoming tasks are not processed.
- If the number of concurrently running tasks is less than N, the executor initiates the execution of the task.
Upon completion of a task from the queue:
- If additional tasks are available, they are dequeued and executed.
- If no tasks are currently running, and the queue is empty, a message indicates that all tasks have been completed.
*/
type ConcurrentExecutor struct {
maxTaskQueueSize int
maxConcurrentTasks int
waitgroup sync.WaitGroup
mutex sync.Mutex
taskQueueChan chan Task
responseChan chan *TaskResponse
closeTaskQueueChanOnce sync.Once
closeResponseChanOnce sync.Once
isTaskQueueChanClosed bool
closed bool
}
// `runTask` executes tasks concurrently. It continuously dequeues tasks from the `taskQueueChan`
// and processes them in separate goroutines. It handles panics during task execution and
// reports errors to the `responseChan`.
func (concurrentExecutor *ConcurrentExecutor) runTask(ctx context.Context) {
defer concurrentExecutor.waitgroup.Done()
for {
select {
case <-ctx.Done():
return
default:
task, ok := <-concurrentExecutor.taskQueueChan
if !ok {
return
}
defer func() {
// Recover from panics and report errors to `responseChan`
if r := recover(); r != nil {
// Handle panic by reporting an error to responseChan
concurrentExecutor.responseChan <- &TaskResponse{
TaskID: task.ID,
Value: nil,
Error: errors.Errorf("got panic, recover: %v", r),
}
// This goroutine is destroyed because panic -> Create a new goroutine to replace the one that panicked,
// supporting handling of another task
concurrentExecutor.waitgroup.Add(1)
go concurrentExecutor.runTask(ctx)
}
}()
// Execute the task with provided arguments or without arguments
var resp interface{}
var err error
if task.ExecutorArgs != nil && len(task.ExecutorArgs) > 0 {
resp, err = task.Executor(ctx, task.ExecutorArgs...)
} else {
resp, err = task.Executor(ctx)
}
// Send the response to the `responseChan`
concurrentExecutor.responseChan <- &TaskResponse{
TaskID: task.ID,
Value: resp,
Error: err,
}
}
}
}
// `NewConcurrentExecutor` creates a new `ConcurrentExecutor` with the specified maximum concurrent tasks
// and maximum task queue size.
func NewConcurrentExecutor(maxConcurrentTasks, maxTaskQueueSize int) *ConcurrentExecutor {
return &ConcurrentExecutor{
maxTaskQueueSize: maxTaskQueueSize,
maxConcurrentTasks: maxConcurrentTasks,
taskQueueChan: make(chan Task, maxTaskQueueSize),
responseChan: make(chan *TaskResponse, maxTaskQueueSize),
isTaskQueueChanClosed: false,
closed: false,
}
}
// `EnqueueTask` adds a task to the concurrent executor's task queue. It panics with `ClosedPanicMsg`
// if `EnqueueTask` is called on a closed executor. It returns an error if the task cannot be added,
// for example, if the task queue is full (`FullQueueErr`).
func (concurrentExecutor *ConcurrentExecutor) EnqueueTask(task Task) error {
concurrentExecutor.mutex.Lock()
if concurrentExecutor.closed {
concurrentExecutor.mutex.Unlock()
panic(ClosedPanicMsg)
}
if concurrentExecutor.isTaskQueueChanClosed {
// Waiting for lastest running is finished and create a new queue (unlock mutex before waiting)
concurrentExecutor.mutex.Unlock()
concurrentExecutor.waitgroup.Wait()
concurrentExecutor.mutex.Lock()
concurrentExecutor.taskQueueChan = make(chan Task, concurrentExecutor.maxTaskQueueSize)
// Init response channel
concurrentExecutor.responseChan = make(chan *TaskResponse, concurrentExecutor.maxTaskQueueSize)
concurrentExecutor.isTaskQueueChanClosed = false
}
concurrentExecutor.mutex.Unlock()
select {
case concurrentExecutor.taskQueueChan <- task:
return nil
default:
return errors.Errorf(FullQueueErr)
}
}
/*
`StartExecution` initiates the concurrent execution of tasks. Will panic if we call `StartExecution` of a closed executor.
It takes a context as an argument to support cancellation of the task execution.
{
...
ctx, cancel := context.WithCancel(context.Background())
task.StartExecution(ctx)
time.Sleep(time.Duration(1) * time.Second)
cancel() // All executions will be canceled after this is called
...
}
*/
func (concurrentExecutor *ConcurrentExecutor) StartExecution(ctx context.Context) {
concurrentExecutor.mutex.Lock()
if concurrentExecutor.closed {
concurrentExecutor.mutex.Unlock()
panic(ClosedPanicMsg)
}
// If it already run, return
if concurrentExecutor.isTaskQueueChanClosed {
concurrentExecutor.mutex.Unlock()
return
}
concurrentExecutor.mutex.Unlock()
// Reset internal state (Waiting for lastest running is finished before reset)
concurrentExecutor.waitgroup.Wait()
concurrentExecutor.mutex.Lock()
concurrentExecutor.waitgroup = sync.WaitGroup{}
concurrentExecutor.isTaskQueueChanClosed = false
concurrentExecutor.closeResponseChanOnce = sync.Once{}
concurrentExecutor.closeTaskQueueChanOnce = sync.Once{}
concurrentExecutor.mutex.Unlock()
// Run task
for i := 0; i < concurrentExecutor.maxConcurrentTasks; i++ {
concurrentExecutor.waitgroup.Add(1)
go concurrentExecutor.runTask(ctx)
}
// After run all tasks in goroutine, `taskQueueChan`` channel should be closed to block publish new task into channel
// and avoid deadlock on goroutine (when consume all of tasks in queue)
concurrentExecutor.closeTaskQueueChanOnce.Do(func() {
concurrentExecutor.mutex.Lock()
concurrentExecutor.isTaskQueueChanClosed = true
concurrentExecutor.mutex.Unlock()
close(concurrentExecutor.taskQueueChan)
})
}
// `WaitForCompletionAndGetResponse` waits for the concurrent execution of tasks to complete
// and retrieves the responses for each task. It returns a map where the keys are task IDs,
// and the values are `TaskResponse` structures containing the results or errors of the tasks.
// If the executor is closed before calling this method, a panic with `ClosedPanicMsg` is triggered.
// If the executor has not been run yet, an empty map is returned.
// This method blocks until all tasks have completed their execution.
func (concurrentExecutor *ConcurrentExecutor) WaitForCompletionAndGetResponse() map[string]*TaskResponse {
concurrentExecutor.mutex.Lock()
// Haven't ran yet
if !concurrentExecutor.isTaskQueueChanClosed {
concurrentExecutor.mutex.Unlock()
return make(map[string]*TaskResponse, 0)
}
if concurrentExecutor.closed {
concurrentExecutor.mutex.Unlock()
panic(ClosedPanicMsg)
}
concurrentExecutor.mutex.Unlock()
concurrentExecutor.waitgroup.Wait()
// After wait all running tasks in goroutine finished, `responseChan` channel should be closed to block publish new resp into channel
// and avoid deadlock on goroutine (when reading all of responses in channel)
concurrentExecutor.closeResponseChanOnce.Do(func() {
close(concurrentExecutor.responseChan)
})
resp := make(map[string]*TaskResponse, 0)
for {
r, ok := <-concurrentExecutor.responseChan
if !ok {
break
}
resp[r.TaskID] = r
}
return resp
}
// `Close` should be called after `EnqueueTask`/`StartExecution`/`WaitForCompletionAndGetResponse`. If not, a panic with `ClosedPanicMsg` is triggered.
// It is suggested to use `defer Close()` to ensure proper cleanup.
func (concurrentExecutor *ConcurrentExecutor) Close() {
concurrentExecutor.closeTaskQueueChanOnce.Do(func() {
concurrentExecutor.mutex.Lock()
concurrentExecutor.isTaskQueueChanClosed = true
concurrentExecutor.mutex.Unlock()
close(concurrentExecutor.taskQueueChan)
})
concurrentExecutor.closeResponseChanOnce.Do(func() {
close(concurrentExecutor.responseChan)
})
concurrentExecutor.mutex.Lock()
concurrentExecutor.closed = true
concurrentExecutor.mutex.Unlock()
}