This repository has been archived by the owner on Feb 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gworker.go
234 lines (190 loc) · 3.81 KB
/
gworker.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
package gworker
import (
"sync"
"sync/atomic"
"time"
)
const (
defaultWorkerCount = 3
maxJobCount = 100000
)
type (
// Dispatcher managements worker
Dispatcher struct {
wg *sync.WaitGroup
workers []*worker
sflg int32
runnig bool
scaling bool
observing bool
start chan bool
jobs chan func() error
joberr chan error
finish chan struct{}
}
worker struct {
dispatcher *Dispatcher
runnig bool
stop chan bool
}
)
// NewDispatcher returns Dispatcher object
func NewDispatcher(workerCount int) *Dispatcher {
if workerCount < 1 {
workerCount = defaultWorkerCount
}
d := &Dispatcher{
wg: new(sync.WaitGroup),
workers: make([]*worker, workerCount),
sflg: 0,
runnig: false,
scaling: false,
observing: false,
start: make(chan bool, 1),
jobs: make(chan func() error, maxJobCount),
joberr: make(chan error),
finish: make(chan struct{}, 1),
}
for i := 0; i < workerCount; i++ {
worker := &worker{
dispatcher: d,
runnig: false,
stop: make(chan bool),
}
d.workers[i] = worker
}
return d
}
// Start starts workers
func (d *Dispatcher) Start() *Dispatcher {
if d.runnig {
return d
}
d.start <- true
d.runnig = true
for _, worker := range d.workers {
worker.start()
}
return d
}
// Stop stops workers.
func (d *Dispatcher) Stop() *Dispatcher {
if !d.runnig {
return d
}
for _, worker := range d.workers {
// sends stop event of worker
worker.stop <- true
worker.runnig = false
}
// delays until goroutine is collected in GC
time.Sleep(1 * time.Millisecond)
d.runnig = false
return d
}
// Add adds job
func (d *Dispatcher) Add(job func() error) {
d.wg.Add(1)
d.jobs <- job
}
// StartJobObserver monitors jobs. Then, when all the jobs are completed, the notification is transmitted
func (d *Dispatcher) StartJobObserver() {
if d.observing {
return
}
d.observing = true
go func() {
<-d.start
d.wg.Wait()
d.finish <- struct{}{}
return
}()
}
// GetWorkerCount returns the number of workers
func (d *Dispatcher) GetWorkerCount() int {
for {
if !d.scaling {
return len(d.workers)
}
}
}
// UpScale scales up the numer of worker
func (d *Dispatcher) UpScale(workerCount int) *Dispatcher {
if !d.runnig {
return d
}
if workerCount < 1 {
return d
}
for {
if d.sflg == 0 && atomic.CompareAndSwapInt32(&d.sflg, 0, 1) {
break
}
}
d.scaling = true
for i := 0; i < workerCount; i++ {
worker := &worker{
dispatcher: d,
runnig: false,
stop: make(chan bool),
}
worker.start()
d.workers = append(d.workers, worker)
}
d.sflg = 0
d.scaling = false
return d
}
// DownScale scales down the numer of worker
func (d *Dispatcher) DownScale(workerCount int) *Dispatcher {
if !d.runnig {
return d
}
if workerCount < 1 || workerCount > len(d.workers) {
return d
}
for {
if d.sflg == 0 && atomic.CompareAndSwapInt32(&d.sflg, 0, 1) {
break
}
}
d.scaling = true
for i := len(d.workers) - 1; i >= len(d.workers)-workerCount; i-- {
// send stop event of worker
d.workers[i].stop <- true
d.workers[i].runnig = false
}
// delay until goroutine is collected in GC
time.Sleep(1 * time.Millisecond)
d.workers = d.workers[:len(d.workers)-workerCount]
d.sflg = 0
d.scaling = false
return d
}
// JobError returns error of job
func (d *Dispatcher) JobError() <-chan error {
return d.joberr
}
// Finish notifies when all jobs are finished
func (d *Dispatcher) Finish() <-chan struct{} {
return d.finish
}
func (w *worker) start() {
w.runnig = true
go func() {
for {
select {
case job, _ := <-w.dispatcher.jobs:
w.run(job)
case _ = <-w.stop:
return
}
}
}()
}
func (w *worker) run(job func() error) {
go func(err error) {
w.dispatcher.joberr <- err
w.dispatcher.wg.Done()
}(job())
}