-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpool.go
196 lines (175 loc) · 3.42 KB
/
grpool.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
package g2cache
// thank https://github.com/ivpusic/grpool
import (
"runtime"
"sync"
"time"
)
// Gorouting instance which can accept client jobs
type worker struct {
id int64
pool *Pool
}
func (w *worker) start() {
go func() {
if CacheDebug {
LogDebugF("Pool [%d] worker start\n", w.id)
}
defer func() {
w.pool.wg.Done()
}()
for {
select {
case <-w.pool.stopped:
if CacheDebug {
LogDebugF("Pool [%d] worker <-stop\n", w.id)
}
if len(w.pool.jobQueue) != 0 {
for job := range w.pool.jobQueue {
runJob(w.id, job)
}
}
if CacheDebug {
LogDebugF("Pool [%d] worker exit\n", w.id)
}
return
case job, ok := <-w.pool.jobQueue:
if ok {
runJob(w.id, job)
}
}
}
}()
}
func runJob(id int64, f func()) {
defer func() {
if err := recover(); err != nil {
if CacheDebug {
LogErrF("Pool [%d] Job panic err: %v, stack: %v\n", id, err,string(outputStackErr()))
}
}
}()
f()
}
func outputStackErr() []byte {
var (
buf [4096]byte
)
n := runtime.Stack(buf[:], false)
return buf[:n]
}
func newWorker(id int64, pool *Pool) *worker {
w := &worker{
id: id,
pool: pool,
}
w.start()
return w
}
// Represents user request, function which should be executed in some worker.
type Job func()
type Pool struct {
jobQueue chan Job
workers []*worker
stopOne sync.Once
stopped chan struct{}
wg sync.WaitGroup
}
// Will make pool of gorouting workers.
// numWorkers - how many workers will be created for this pool
// queueLen - how many jobs can we accept until we block
//
// Returned object contains JobQueue reference, which you can use to send job to pool.
func NewPool(numWorkers int, jobQueueLen int) *Pool {
pool := &Pool{
jobQueue: make(chan Job, jobQueueLen),
workers: make([]*worker, numWorkers),
stopped: make(chan struct{}),
}
for i := 0; i < numWorkers; i++ {
pool.wg.Add(1)
pool.workers[i] = newWorker(int64(i), pool)
}
if CacheMonitor {
pool.wg.Add(1)
go pool.monitor()
}
return pool
}
func (p *Pool) wrapJob(job func()) func() {
return job
}
func (p *Pool) SendJobWithTimeout(job func(), t time.Duration) bool {
select {
case <-p.stopped:
return false
case <-time.After(t):
return false
case p.jobQueue <- p.wrapJob(job):
return true
}
}
func (p *Pool) SendJobWithDeadline(job func(), t time.Time) bool {
s := t.Sub(time.Now())
if s <= 0 {
s = time.Second // timeout
}
select {
case <-p.stopped:
return false
case <-time.After(s):
return false
case p.jobQueue <- p.wrapJob(job):
return true
}
}
func (p *Pool) SendJob(job func()) {
select {
case p.jobQueue <- p.wrapJob(job):
case <-p.stopped:
return
}
}
func (p *Pool) monitor() {
t := time.NewTicker(time.Duration(CacheMonitorSecond) * time.Second)
for {
select {
case <-p.stopped:
t.Stop()
return
case <-t.C:
LogDebug("Pool jobQueue current len ", len(p.jobQueue))
}
}
}
func (p *Pool) release() {
close(p.stopped)
force := make(chan struct{})
forceOne := sync.Once{}
go func() {
for {
select {
case <-force:
return
default:
p.wg.Wait() // why always some goroutine not exit,who found bug
forceOne.Do(func() {
close(force)
})
return
}
}
}()
// forceExit
time.AfterFunc(5*time.Second, func() {
forceOne.Do(func() {
close(force)
})
})
<-force
close(p.jobQueue)
}
// Will release resources used by pool
func (p *Pool) Release() {
p.stopOne.Do(p.release)
}