/
batchworkerpool.go
206 lines (155 loc) · 3.63 KB
/
batchworkerpool.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
package batchworkerpool
import (
"sync"
"time"
"github.com/loveandpeople/hive.go/syncutils"
)
type BatchWorkerPool struct {
workerFnc func([]Task)
options *Options
calls chan Task
batchedCalls chan []Task
terminate chan struct{}
terminateBatchWorkers chan struct{}
running bool
shutdown bool
mutex syncutils.RWMutex
wait sync.WaitGroup
}
func New(workerFnc func([]Task), optionalOptions ...Option) (result *BatchWorkerPool) {
options := DEFAULT_OPTIONS.Override(optionalOptions...)
result = &BatchWorkerPool{
workerFnc: workerFnc,
options: options,
calls: make(chan Task, options.QueueSize),
batchedCalls: make(chan []Task, 2*options.WorkerCount),
terminate: make(chan struct{}),
terminateBatchWorkers: make(chan struct{}),
}
return
}
func (wp *BatchWorkerPool) Submit(params ...interface{}) (result chan interface{}, added bool) {
wp.mutex.RLock()
defer wp.mutex.RUnlock()
if !wp.shutdown {
result = make(chan interface{}, 1)
wp.calls <- Task{
params: params,
resultChan: result,
}
return result, true
}
return nil, false
}
func (wp *BatchWorkerPool) Start() {
wp.mutex.Lock()
if !wp.running {
if wp.shutdown {
panic("BatchWorker was already used before")
}
wp.running = true
wp.startBatchDispatcher()
wp.startBatchWorkers()
}
wp.mutex.Unlock()
}
func (wp *BatchWorkerPool) Run() {
wp.Start()
wp.wait.Wait()
}
func (wp *BatchWorkerPool) Stop() {
wp.mutex.Lock()
if wp.running {
wp.shutdown = true
wp.running = false
close(wp.terminate)
}
wp.mutex.Unlock()
}
func (wp *BatchWorkerPool) StopAndWait() {
wp.Stop()
wp.wait.Wait()
}
func (wp *BatchWorkerPool) GetWorkerCount() int {
return wp.options.WorkerCount
}
func (wp *BatchWorkerPool) GetBatchSize() int {
return wp.options.BatchSize
}
func (wp *BatchWorkerPool) GetPendingQueueSize() int {
return len(wp.batchedCalls)
}
func (wp *BatchWorkerPool) dispatchTasks(task Task) {
batchTask := append(make([]Task, 0), task)
collectionTimeout := time.After(wp.options.BatchCollectionTimeout)
// collect additional requests that arrive within the timeout
CollectAdditionalCalls:
for {
select {
case <-collectionTimeout:
break CollectAdditionalCalls
case call := <-wp.calls:
batchTask = append(batchTask, call)
if len(batchTask) == wp.options.BatchSize {
break CollectAdditionalCalls
}
}
}
wp.batchedCalls <- batchTask
}
func (wp *BatchWorkerPool) startBatchDispatcher() {
wp.wait.Add(1)
go func() {
for {
select {
case <-wp.terminate:
if wp.options.FlushTasksAtShutdown {
terminateLoop:
// process all waiting tasks after shutdown signal
for {
select {
case firstCall := <-wp.calls:
wp.dispatchTasks(firstCall)
default:
break terminateLoop
}
}
}
close(wp.terminateBatchWorkers)
wp.wait.Done()
return
case firstCall := <-wp.calls:
wp.dispatchTasks(firstCall)
}
}
}()
}
func (wp *BatchWorkerPool) startBatchWorkers() {
for i := 0; i < wp.options.WorkerCount; i++ {
wp.wait.Add(1)
go func() {
aborted := false
for !aborted {
select {
case <-wp.terminateBatchWorkers:
aborted = true
if wp.options.FlushTasksAtShutdown {
terminateLoop:
// process all waiting tasks after shutdown signal
for {
select {
case batchTask := <-wp.batchedCalls:
wp.workerFnc(batchTask)
default:
break terminateLoop
}
}
}
case batchTask := <-wp.batchedCalls:
wp.workerFnc(batchTask)
}
}
wp.wait.Done()
}()
}
}