-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpool.go
193 lines (168 loc) · 3.11 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
package grpool
import (
"errors"
"log"
"sync"
"time"
)
const NO_WAIT = false
const WAIT = true
type WorkerPool struct {
maxWorkerNumber int
workerCount int
workerQueue chan *Worker
cachePool sync.Pool
cond *sync.Cond
mustStop bool
}
func NewWorkerPool(maxWorkerNumber int) (*WorkerPool, error) {
if maxWorkerNumber < 1 {
return nil, errors.New("maxWorkerNumber must be larger than 0")
}
wp := &WorkerPool{
maxWorkerNumber: maxWorkerNumber,
workerCount: 0,
workerQueue: make(chan *Worker, maxWorkerNumber),
cachePool: sync.Pool{
New: func() interface{} {
return &Worker{
ch: make(chan WorkUnit, 1),
}
},
},
cond: sync.NewCond(&sync.Mutex{}),
}
return wp, nil
}
func (wp *WorkerPool) Start() {
wp.mustStop = false
}
func (wp *WorkerPool) Stop() {
wp.cond.L.Lock()
wp.mustStop = true
wp.clearWorkerQueue()
wp.cond.L.Unlock()
}
func (wp *WorkerPool) StopAndWait() {
wp.Stop()
wp.Wait()
}
func (wp *WorkerPool) Wait() {
wp.cond.L.Lock()
for wp.workerCount != 0 {
wp.cond.Wait()
}
wp.cond.L.Unlock()
}
func (wp *WorkerPool) clearWorkerQueue() {
for {
select {
case w := <-wp.workerQueue:
w.ch <- nil
default:
return
}
}
}
func (wp *WorkerPool) Queue(unit WorkUnit) error {
go wp.queue(unit)
return nil
}
func (wp *WorkerPool) QueueAndWait(unit WorkUnit) error {
return wp.queue(unit)
}
func (wp *WorkerPool) queue(unit WorkUnit) error {
w, err := wp.getWorker()
if err != nil {
return err
}
w.ch <- unit
return nil
}
func (wp *WorkerPool) getWorker() (*Worker, error) {
createWorker := false
wp.cond.L.Lock()
if wp.mustStop == true {
err := errors.New("WorkerPool is stopped")
wp.cond.L.Unlock()
return nil, err
}
if wp.workerCount < wp.maxWorkerNumber {
createWorker = true
wp.workerCount += 1
}
wp.cond.L.Unlock()
if createWorker == true {
w := wp.newWorker()
return w, nil
}
tick := time.Tick(10 * time.Millisecond)
for {
select {
case w := <-wp.workerQueue:
return w, nil
case <-tick:
wp.cond.L.Lock()
if wp.mustStop {
wp.cond.L.Unlock()
return nil, errors.New("Stopped WorkerPool")
}
wp.cond.L.Unlock()
}
}
}
func (wp *WorkerPool) newWorker() *Worker {
vw := wp.cachePool.Get()
w := vw.(*Worker)
go wp.runWorker(w)
return w
}
func (wp *WorkerPool) runWorker(w *Worker) {
defer wp.endWorker(w)
var unit WorkUnit
for unit = range w.ch {
if unit == nil {
break
}
wp.runUnit(unit)
wp.cond.L.Lock()
if wp.isRelease(w) {
wp.cond.L.Unlock()
break
}
wp.cond.L.Unlock()
}
}
func (wp *WorkerPool) endWorker(w *Worker) {
wp.cond.L.Lock()
wp.workerCount -= 1
wp.cond.L.Unlock()
wp.cond.Broadcast()
wp.cachePool.Put(w)
}
func (wp *WorkerPool) isRelease(w *Worker) bool {
if wp.mustStop {
return true
}
select {
case wp.workerQueue <- w:
return false
default:
return true
}
}
func (wp *WorkerPool) runUnit(unit WorkUnit) {
defer func() {
if err := recover(); err != nil {
log.Println("Unit Panic:", err)
}
}()
unit.Run()
}
type WorkUnit interface {
Run()
}
type WorkUnitChannel chan WorkUnit
type Worker struct {
ch WorkUnitChannel
}