-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpool.go
86 lines (69 loc) · 1.48 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
package running
import (
"sync"
)
type _WorkerPool struct {
sync.Pool
bufQueue []*_Worker
qFront int
qRear int
mu sync.Mutex
}
func (pool *_WorkerPool) GetWorker() (*_Worker, error) {
if len(pool.bufQueue) > 0 {
if worker := pool.qGet(); worker != nil {
return worker, nil
}
}
got := pool.Get()
if worker, ok := got.(*_Worker); ok {
// return a worker from pool
return worker, nil
} else {
// return error while building worker
return nil, got.(error)
}
}
func (pool *_WorkerPool) PutWorker(worker *_Worker) {
if len(pool.bufQueue) > 0 && pool.qPut(worker) {
return
}
pool.Put(worker)
}
func (pool *_WorkerPool) Warmup(size int) {
if size <= 0 {
return
}
pool.mu.Lock()
defer pool.mu.Unlock()
pool.bufQueue = make([]*_Worker, size)
for i := 0; i < size; i++ {
got := pool.Get()
if worker, ok := got.(*_Worker); ok {
pool.bufQueue[i] = worker
}
}
pool.qFront = 0
pool.qRear = size - 1
}
func (pool *_WorkerPool) qGet() (worker *_Worker) {
pool.mu.Lock()
defer pool.mu.Unlock()
if pool.qFront == pool.qRear {
return
}
worker = pool.bufQueue[pool.qFront]
pool.bufQueue[pool.qFront] = nil
pool.qFront = (pool.qFront + 1) % len(pool.bufQueue)
return
}
func (pool *_WorkerPool) qPut(worker *_Worker) (success bool) {
pool.mu.Lock()
defer pool.mu.Unlock()
if (pool.qRear+1)%len(pool.bufQueue) == pool.qFront {
return
}
pool.bufQueue[pool.qRear] = worker
pool.qRear = (pool.qRear + 1) % len(pool.bufQueue)
return true
}