-
Notifications
You must be signed in to change notification settings - Fork 18
/
pool.go
268 lines (226 loc) · 6.17 KB
/
pool.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
package main
import (
"fmt"
"log"
"os"
"sync"
"time"
"github.com/pborman/uuid"
)
// default average speed interval
var SpeedInterval time.Duration = 10
// payload type(job type)
type PayloadType string
type fnType func(payload PayloadType)
// --------------------------------------- //
// Job
type Job struct {
ID string //job ID
fn func(payload PayloadType) //job function
Payload PayloadType //job payload
}
// Worker
type Worker struct {
ID int32 //job ID
jobCacheQueue chan Job //job channl
die bool
wait bool //wait for done
wg *sync.WaitGroup
}
func (w *Worker) Run() {
go func() {
for {
// shutdown
if w.die {
return
}
job := <-w.jobCacheQueue
job.fn(job.Payload)
// wait for done
if w.wait {
w.wg.Done()
}
}
}()
}
type GoroutinePool struct {
maxWorkers int32 //max workers
doneJobs int32
jobCacheQueue chan Job
jobCacheQueueLen int32
workers []*Worker
wg *sync.WaitGroup
wait bool
feedback bool
}
// new GoroutinePool
func NewGoroutinePool(maxWorkers int32, jobCacheQueueLen int32, feedback bool) *GoroutinePool {
jobCacheQueue := make(chan Job, jobCacheQueueLen)
workers := make([]*Worker, jobCacheQueueLen)
if maxWorkers > jobCacheQueueLen {
panic("maxWorkers must <= jobCacheQueueLen")
}
var wg sync.WaitGroup
pool := &GoroutinePool{
maxWorkers: maxWorkers,
jobCacheQueueLen: jobCacheQueueLen,
jobCacheQueue: jobCacheQueue,
wait: true,
feedback: feedback,
wg: &wg,
workers: workers,
doneJobs: 0,
}
// start worker
pool.Start()
// start monitor
pool.Monitor()
return pool
}
func (pool *GoroutinePool) AddJob(fn func(payload PayloadType), payload PayloadType) {
ID := uuid.NewUUID()
job := Job{
ID: ID.String(),
fn: fn,
Payload: payload,
}
if pool.wait {
pool.wg.Add(1)
}
// bad
pool.doneJobs++
pool.jobCacheQueue <- job
}
func (pool *GoroutinePool) Monitor() {
var lastDone int32
// bad
lastDone = pool.jobCacheQueueLen
var speed int32
interval := time.NewTicker(SpeedInterval * time.Second)
// real-time speed
var realtimeLastDone int32
// bad
realtimeLastDone = pool.jobCacheQueueLen
var realtimeSpeed int32
// interval monintor
ticker := time.NewTicker(1 * time.Second)
quit := make(chan struct{})
// time cost
tStart := time.Now()
var costDuration time.Duration
var feedbackDoubleCheck bool = false
initWorkers := pool.maxWorkers
go func() {
for {
select {
case <-ticker.C:
tCurrent := time.Now()
costDuration = tCurrent.Sub(tStart)
// real-time speed
tmpRealtimeSpeed := (pool.doneJobs - realtimeLastDone)
realtimeSpeed = tmpRealtimeSpeed
realtimeLastDone = pool.doneJobs
fmt.Printf("\r Start at: %s, time cost: %s, average speed: %d, read-time speed: %d, current workers: %d, done jobs: %d ", tStart.Format("15:04:05.000"), costDuration.String(), speed, realtimeSpeed, pool.maxWorkers, pool.doneJobs)
// feedback mechanism!
case <-interval.C:
// average speed
tmpSpeed := (pool.doneJobs - lastDone) / int32(SpeedInterval)
if pool.feedback {
var feedbackMaxWorkers int32
feedbackMaxWorkers = pool.maxWorkers
if tmpSpeed < speed {
if feedbackDoubleCheck == true {
//feedbackMaxWorkers = pool.maxWorkers - pool.maxWorkers*(speed-tmpSpeed)/speed
feedbackMaxWorkers -= feedbackMaxWorkers / 10
fmt.Printf("DoubleCheck Feedback \n")
feedbackDoubleCheck = false
} else {
feedbackDoubleCheck = true
}
} else {
if speed > 0 {
//feedbackMaxWorkers = pool.maxWorkers + pool.maxWorkers*(tmpSpeed-speed)/speed
feedbackMaxWorkers += feedbackMaxWorkers / 10
}
feedbackDoubleCheck = false
}
if feedbackMaxWorkers < initWorkers {
feedbackMaxWorkers = initWorkers
}
if feedbackMaxWorkers > pool.jobCacheQueueLen {
feedbackMaxWorkers = pool.jobCacheQueueLen
}
if feedbackMaxWorkers > 2*pool.maxWorkers {
feedbackMaxWorkers = 2 * pool.maxWorkers
}
// feedback worker numbers
fmt.Printf("\r Start at: %s, time cost: %s, average speed: %d, read-time speed: %d, current workers: %d, done jobs: %d ", tStart.Format("15:04:05.000"), costDuration.String(), speed, realtimeSpeed, feedbackMaxWorkers, pool.doneJobs)
pool.feedbackWorkers(feedbackMaxWorkers)
}
speed = tmpSpeed
lastDone = pool.doneJobs
case <-quit:
ticker.Stop()
return
}
}
}()
}
func (pool *GoroutinePool) MapRun(fn func(payload PayloadType), payloads []PayloadType) {
// maybe reflect
// http://blog.burntsushi.net/type-parametric-functions-golang/
for _, payload := range payloads {
pool.AddJob(fn, payload)
}
}
func (pool *GoroutinePool) MapRunChan(fn func(payload PayloadType), fnfetch func() PayloadType) {
for {
payload := fnfetch()
pool.AddJob(fn, payload)
}
}
func (pool *GoroutinePool) Start() {
for i := int32(0); i < pool.maxWorkers; i++ {
worker := &Worker{
ID: int32(i),
jobCacheQueue: pool.jobCacheQueue,
wait: pool.wait,
die: false,
wg: pool.wg,
}
worker.Run()
pool.workers[i] = worker
}
}
func (pool *GoroutinePool) feedbackWorkers(feedbackMaxWorkers int32) {
if feedbackMaxWorkers > pool.maxWorkers {
for i := pool.maxWorkers; i < feedbackMaxWorkers; i++ {
worker := &Worker{
ID: int32(i),
jobCacheQueue: pool.jobCacheQueue,
wait: pool.wait,
die: false,
wg: pool.wg,
}
worker.Run()
pool.workers[i] = worker
}
} else {
for i := feedbackMaxWorkers; i < pool.maxWorkers; i++ {
pool.workers[i].die = true
}
}
pool.maxWorkers = feedbackMaxWorkers
}
func (pool *GoroutinePool) Wait() {
pool.wg.Wait()
}
func CustomLogger(fileName string) *log.Logger {
// f, err := os.OpenFile(fileName, os.O_APPEND | os.O_CREATE | os.O_RDWR, 0666)
f, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
fmt.Printf("error opening file: %v", err)
}
logger = log.New(f, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
return logger
}