-
Notifications
You must be signed in to change notification settings - Fork 39
/
pool.go
157 lines (128 loc) · 2.4 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
package mortar
import (
"errors"
"log"
"sync"
"sync/atomic"
"time"
)
// errors
var (
// return if pool size <= 0
ErrInvalidPoolCap = errors.New("invalid pool cap")
// put task but pool already closed
ErrPoolAlreadyClosed = errors.New("pool already closed")
)
// running status
const (
RUNNING = 1
STOPED = 0
)
// Task task to-do
type Task struct {
Handler func(v ...interface{})
Params []interface{}
}
// Pool task pool
type Pool struct {
capacity uint64
runningWorkers uint64
status int64
chTask chan *Task
PanicHandler func(interface{})
sync.Mutex
}
// NewPool init pool
func NewPool(capacity uint64) (*Pool, error) {
if capacity <= 0 {
return nil, ErrInvalidPoolCap
}
p := &Pool{
capacity: capacity,
status: RUNNING,
chTask: make(chan *Task, capacity),
}
return p, nil
}
func (p *Pool) checkWorker() {
p.Lock()
defer p.Unlock()
if p.runningWorkers == 0 && len(p.chTask) > 0 {
p.run()
}
}
// GetCap get capacity
func (p *Pool) GetCap() uint64 {
return p.capacity
}
// GetRunningWorkers get running workers
func (p *Pool) GetRunningWorkers() uint64 {
return atomic.LoadUint64(&p.runningWorkers)
}
func (p *Pool) incRunning() {
atomic.AddUint64(&p.runningWorkers, 1)
}
func (p *Pool) decRunning() {
atomic.AddUint64(&p.runningWorkers, ^uint64(0))
}
// Put put a task to pool
func (p *Pool) Put(task *Task) error {
p.Lock()
defer p.Unlock()
if p.status == STOPED {
return ErrPoolAlreadyClosed
}
// run worker
if p.GetRunningWorkers() < p.GetCap() {
p.run()
}
// send task
if p.status == RUNNING {
p.chTask <- task
}
return nil
}
func (p *Pool) run() {
p.incRunning()
go func() {
defer func() {
p.decRunning()
if r := recover(); r != nil {
if p.PanicHandler != nil {
p.PanicHandler(r)
} else {
log.Printf("Worker panic: %s\n", r)
}
}
p.checkWorker() // check worker avoid no worker running
}()
for {
select {
case task, ok := <-p.chTask:
if !ok {
return
}
task.Handler(task.Params...)
}
}
}()
}
func (p *Pool) setStatus(status int64) bool {
p.Lock()
defer p.Unlock()
if p.status == status {
return false
}
p.status = status
return true
}
// Close close pool graceful
func (p *Pool) Close() {
if !p.setStatus(STOPED) { // stop put task
return
}
for len(p.chTask) > 0 { // wait all task be consumed
time.Sleep(1e6) // reduce CPU load
}
close(p.chTask)
}