-
Notifications
You must be signed in to change notification settings - Fork 2
/
pool.go
268 lines (231 loc) · 5.07 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 pool
import (
"context"
"errors"
"log"
"runtime"
"sync"
"sync/atomic"
"time"
)
const (
// the default capacity for a default goroutine pool.
defaultMaxGoroutinesSize = 512 * 1024
// the default maximum idle duration of a goroutine.
defaultMaxGoroutineIdleDuration = 10 * time.Second
// the default block polling interval
defaultBlockPollingInterval = 100 * time.Millisecond
)
var (
ErrLack = errors.New("lack of goroutines, because exceeded capacity limit.")
ErrPoolClosed = errors.New("this pool has been closed")
)
type (
sig struct{}
// task execution func
f func()
)
// Pool accept the tasks from client,it limits the total
// of goroutines to a given number by recycling goroutines.
type Pool struct {
mu sync.Mutex
once sync.Once
// capacity of the pool.
capacity int32
// number of tasks running goroutines.
running int32
// set the expired time (second) of every worker.
expire time.Duration
// set the block polling interval.
interval time.Duration
// is a slice that store the available workers.
workers []*Worker
// is used to notice the pool to closed itself.
release chan sig
}
// clear expired workers periodically.
func (p *Pool) clean() {
t := time.NewTicker(p.expire)
for range t.C {
currentTime := time.Now()
p.mu.Lock()
idleWorkers := p.workers
if len(idleWorkers) == 0 && p.Running() == 0 && len(p.release) > 0 {
p.mu.Unlock()
return
}
n := 0
for i, w := range idleWorkers {
if currentTime.Sub(w.recycleTime) <= p.expire {
break
}
n = i
w.task <- nil
idleWorkers[i] = nil
}
n++
if n >= len(idleWorkers) {
// all workers have expired
p.workers = idleWorkers[:0]
} else {
// remove the expire workers
p.workers = idleWorkers[n:]
}
p.mu.Unlock()
}
}
// NewPool creates a new *Pool.
// If size<=0, will use default value.
// If expire<=0, will use default value.
// If interval<=0, will use default value.
func NewPool(size int32, expire, interval time.Duration) *Pool {
p := &Pool{
release: make(chan sig, 1),
}
if size <= 0 {
p.capacity = defaultMaxGoroutinesSize
} else {
p.capacity = size
}
if expire <= 0 {
p.expire = defaultMaxGoroutineIdleDuration
} else {
p.expire = expire
}
if interval <= 0 {
p.interval = defaultBlockPollingInterval
} else {
p.interval = interval
}
go func() {
defer func() {
if err := recover(); err != nil {
log.Printf("clean err:%s\n", err)
}
}()
p.clean()
}()
return p
}
// Create giroutine
func (p *Pool) Go(fn f) error {
return p.submit(fn)
}
// TryGo tries to execute the function via goroutine.
// If there are no concurrent resources, execute it synchronously.
func (p *Pool) TryGo(fn f) {
err := p.Go(fn)
if err != nil {
if err == ErrPoolClosed {
log.Printf("%v\n", err)
}
fn()
}
}
// AnywayGo block until the goroutine is obtained.
func (p *Pool) AnywayGo(fn f, ctx ...context.Context) error {
if len(p.release) > 0 {
return ErrPoolClosed
}
if len(ctx) == 0 {
for p.Go(fn) != nil {
runtime.Gosched()
}
return nil
}
c := ctx[0]
for {
select {
case <-c.Done():
return c.Err()
default:
if p.Go(fn) == nil {
return nil
}
runtime.Gosched()
}
}
}
// Release closes this pool.
func (p *Pool) Release() error {
p.once.Do(func() {
p.release <- sig{}
p.mu.Lock()
idleWorkers := p.workers
for i, w := range idleWorkers {
w.task <- nil
idleWorkers[i] = nil
}
p.workers = nil
p.mu.Unlock()
})
return nil
}
// Running returns the number of the currently running goroutines.
func (p *Pool) Running() int {
return int(atomic.LoadInt32(&p.running))
}
// Free returns the available goroutines to work.
func (p *Pool) Free() int {
return int(atomic.LoadInt32(&p.capacity) - atomic.LoadInt32(&p.running))
}
// Cap returns the capacity of this pool.
func (p *Pool) Cap() int {
return int(atomic.LoadInt32(&p.capacity))
}
// increases the number of the currently running goroutines.
func (p *Pool) incrRunning() {
atomic.AddInt32(&p.running, 1)
}
// decreases the number of the currently running goroutines.
func (p *Pool) decrRunning() {
atomic.AddInt32(&p.running, -1)
}
// submits a task to this pool.
func (p *Pool) submit(fn f) error {
if len(p.release) > 0 {
return ErrPoolClosed
}
w := p.getWorker()
if w == nil {
return ErrLack
}
w.task <- fn
return nil
}
// returns a available worker to run the tasks.
func (p *Pool) getWorker() *Worker {
var w *Worker
// Flag indicating whether the number of currently running
// workers has reached the capacity limit
var isFull bool
p.mu.Lock()
idleWorkers := p.workers
n := len(p.workers) - 1
// no idle workers
if n < 0 {
isFull = p.Running() >= p.Cap()
} else {
w = p.workers[n]
idleWorkers[n] = nil
p.workers = idleWorkers[:n]
}
p.mu.Unlock()
if !isFull && w == nil {
// new worker to run
w = &Worker{
pool: p,
task: make(chan f, 1),
}
w.run()
p.incrRunning()
}
return w
}
// puts a worker back into free pool, recycling the goroutines.
func (p *Pool) putWorker(worker *Worker) {
worker.recycleTime = time.Now()
p.mu.Lock()
p.workers = append(p.workers, worker)
p.mu.Unlock()
}