/
pool.go
172 lines (144 loc) · 3.04 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
package gopool
import (
"errors"
"time"
"go.uber.org/atomic"
)
var (
ErrSubmitTimeout = errors.New("submit error: timed out")
ErrOptionsDeadQueue = errors.New("options error:dead queue configuration")
ErrOptionsSpawn = errors.New("options error:spawn > workers")
)
// Pool goroutine pool
type Pool struct {
sem chan struct{}
work chan func()
maxIdleTimeout time.Duration
panicHandler func(err interface{})
pendingTaskNum atomic.Int64
successTotal atomic.Uint64
failedTotal atomic.Uint64
}
func NewPool(maxWorkerNum int, opts ...Option) (*Pool, error) {
options := loadOptions(opts...)
options.size = maxWorkerNum
if options.spawn <= 0 && options.queue > 0 {
return nil, ErrOptionsDeadQueue
}
if options.spawn > options.size {
return nil, ErrOptionsSpawn
}
p := &Pool{
sem: make(chan struct{}, options.size),
work: make(chan func(), options.queue),
panicHandler: options.panicHandler,
maxIdleTimeout: time.Second * time.Duration(options.maxIdleTimeoutSeconds),
}
for i := 0; i < options.spawn; i++ {
p.sem <- struct{}{}
go p.worker(nil)
}
return p, nil
}
// Submit _
func (p *Pool) Submit(task func()) error {
return p.schedule(task, nil)
}
// SubmitTimeout _
func (p *Pool) SubmitTimeout(timeout time.Duration, task func()) error {
return p.schedule(task, time.After(timeout))
}
// ActiveWorkerNum get active worker number
func (p *Pool) ActiveWorkerNum() int64 {
return int64(len(p.sem))
}
// QueueLength get queue item number
func (p *Pool) QueueLength() int64 {
return int64(len(p.work))
}
// PendingTaskTotal get pending task num
func (p *Pool) PendingTaskTotal() int64 {
return p.pendingTaskNum.Load() + p.QueueLength()
}
// SuccessTotal _
func (p *Pool) SuccessTotal() uint64 {
return p.successTotal.Load()
}
// FailedTotal _
func (p *Pool) FailedTotal() uint64 {
return p.failedTotal.Load()
}
// HandleTotal _
func (p *Pool) HandleTotal() uint64 {
return p.SuccessTotal() + p.FailedTotal()
}
func (p *Pool) schedule(task func(), timeout <-chan time.Time) error {
select {
case <-timeout:
return ErrSubmitTimeout
case p.work <- task:
p.pendingTaskNum.Inc()
return nil
case p.sem <- struct{}{}:
p.pendingTaskNum.Inc()
go p.worker(task)
return nil
}
}
func (p *Pool) worker(task func()) {
defer func() {
<-p.sem
}()
p.runTask(task)
if p.maxIdleTimeout < time.Second {
for {
select {
case t, ok := <-p.work:
p.runTask(t)
if !ok {
return
}
default:
}
}
}
var (
ticker = time.NewTicker(p.maxIdleTimeout)
working = false
)
defer func() {
ticker.Stop()
ticker = nil
}()
for {
select {
case t, ok := <-p.work:
working = true
p.runTask(t)
if !ok {
return
}
case <-ticker.C:
if !working {
return
}
working = false
default:
}
}
}
func (p *Pool) runTask(task func()) {
if task == nil {
return
}
defer func() {
p.pendingTaskNum.Dec()
if err := recover(); err != nil {
p.failedTotal.Inc()
p.panicHandler(err)
} else {
p.successTotal.Inc()
}
}()
task()
}