-
Notifications
You must be signed in to change notification settings - Fork 0
/
deadpool.go
237 lines (189 loc) · 4.18 KB
/
deadpool.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
package deadpool
import (
"errors"
"sync"
"sync/atomic"
"time"
)
type Task interface {
Run()
}
type Executor interface {
Execute(Task, Pool)
}
type WorkerSpawner interface {
Spawn(chan Task, chan struct{}, func(Task), func(), func())
}
type Pool interface {
Submit(Task)
WaitAll()
Execute(Task)
SpawnWorker()
WorkerSpawned() int32
SubmittedTasks() int32
OnExecuteTask(time.Time)
CummlatedWorkTime() time.Duration
AVGTime() time.Duration
StopWorkers()
}
type DefaultExecutor struct{}
func (e *DefaultExecutor) Execute(task Task, p Pool) {
start := time.Now().UTC()
defer p.OnExecuteTask(start)
task.Run()
}
type DefaultSpawner struct{}
func (s *DefaultSpawner) Spawn(tasks chan Task, stopWorkers chan struct{}, executor func(Task), onStart func(), onEnd func()) {
onStart()
go func() {
defer onEnd()
for {
select {
case task := <-tasks:
if task == nil {
return
}
executor(task)
case <-stopWorkers:
return
}
}
}()
}
type deadpool struct {
executor Executor
spawner WorkerSpawner
recoverer func()
wg *sync.WaitGroup
stopWorkers chan struct{}
tasksStream chan Task
avgTaskTime time.Duration
cummulatedTime time.Duration
mu sync.Mutex
maxWorkers int32
submittedTasksQTY int32
readyTasksQTY int32
currentWorkersQTY int32
cap int8
}
func New(opts ...func(*deadpool)) (*deadpool, error) {
d := &deadpool{
executor: &DefaultExecutor{},
spawner: &DefaultSpawner{},
wg: &sync.WaitGroup{},
mu: sync.Mutex{},
}
for _, f := range opts {
f(d)
}
if d.maxWorkers <= 0 {
return d, ErrInvalidMaxWorkers
}
if d.cap < 0 {
return d, ErrInvalidCap
}
if d.executor == nil {
return d, ErrInvalidExecutor
}
if d.spawner == nil {
return d, ErrInvalidSpawner
}
d.tasksStream = make(chan Task, d.cap)
d.stopWorkers = make(chan struct{})
return d, nil
}
func (d *deadpool) Submit(task Task) {
atomic.AddInt32(&d.readyTasksQTY, 1)
d.SpawnWorker()
d.tasksStream <- task
atomic.AddInt32(&d.submittedTasksQTY, 1)
}
func (d *deadpool) StopWorkers() {
close(d.stopWorkers)
}
func (d *deadpool) WaitAll() {
if d.tasksStream != nil {
close(d.tasksStream)
}
d.wg.Wait()
d.StopWorkers()
}
func (d *deadpool) Execute(task Task) {
d.executor.Execute(task, d)
}
func (d *deadpool) SpawnWorker() {
currentWorkersQTY := atomic.LoadInt32(&d.currentWorkersQTY)
if currentWorkersQTY < atomic.LoadInt32(&d.maxWorkers) {
d.spawner.Spawn(
d.tasksStream,
d.stopWorkers,
d.Execute,
func(workerID int32) func() {
return func() {
d.wg.Add(1)
}
}(currentWorkersQTY),
func(workerID int32) func() {
return func() {
d.wg.Done()
}
}(currentWorkersQTY),
)
atomic.AddInt32(&d.currentWorkersQTY, 1)
}
}
func (d *deadpool) WorkerSpawned() int32 {
return d.currentWorkersQTY
}
func (d *deadpool) SubmittedTasks() int32 {
return d.submittedTasksQTY
}
func (d *deadpool) AVGTime() time.Duration {
return d.avgTaskTime
}
func (d *deadpool) CummlatedWorkTime() time.Duration {
return d.cummulatedTime
}
func (d *deadpool) OnExecuteTask(start time.Time) {
d.mu.Lock()
defer d.mu.Unlock()
ellapsed := time.Since(start)
d.cummulatedTime += ellapsed
readies := time.Duration(atomic.LoadInt32(&d.readyTasksQTY))
d.avgTaskTime = d.cummulatedTime / readies
}
func WithMax(max int32) func(*deadpool) {
return func(d *deadpool) {
d.maxWorkers = max
}
}
func WithCap(cap int8) func(*deadpool) {
return func(d *deadpool) {
d.cap = cap
}
}
func WithExecutor(exe Executor) func(*deadpool) {
return func(d *deadpool) {
d.executor = exe
}
}
func WithSpawner(spn WorkerSpawner) func(*deadpool) {
return func(d *deadpool) {
d.spawner = spn
}
}
func WithRecoverer(f func()) func(*deadpool) {
return func(d *deadpool) {
if f != nil {
d.recoverer = f
return
}
d.recoverer = func() {}
}
}
var (
ErrInvalidMaxWorkers = errors.New("invalid max number of workers")
ErrInvalidCap = errors.New("invalid capacity for tasks stream")
ErrInvalidExecutor = errors.New("expected a valid Executor. Nil received")
ErrInvalidSpawner = errors.New("expected a valid Spawner. Nil received")
)