-
Notifications
You must be signed in to change notification settings - Fork 3
/
pool.go
213 lines (183 loc) · 3.03 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
package core
import (
"hash/fnv"
"sync"
"github.com/gammazero/deque"
)
const (
DefaultQueueSize = 100
DefaultCapacity = 10
)
type QueuePool interface {
Submit(key string, job func())
Drain()
Kill()
}
type QueueWorker interface {
Submit(job func())
Drain()
Kill()
}
type QueueWorkerParams struct {
QueueSize int
DropWhenFull bool
OnDropped func()
}
type queuePool struct {
sync.Mutex
capacity int
workers []QueueWorker
params QueueWorkerParams
drain Fuse
kill Fuse
}
func NewQueuePool(maxWorkers int, params QueueWorkerParams) QueuePool {
if maxWorkers <= 0 {
maxWorkers = DefaultCapacity
}
if params.QueueSize == 0 {
params.QueueSize = DefaultQueueSize
}
return &queuePool{
capacity: maxWorkers,
workers: make([]QueueWorker, maxWorkers),
params: params,
}
}
func (p *queuePool) hash(key string) int {
h := fnv.New32a()
_, _ = h.Write([]byte(key))
return int(h.Sum32()) % p.capacity
}
func (p *queuePool) Submit(key string, job func()) {
p.Lock()
if p.kill.IsBroken() {
p.Unlock()
return
}
idx := p.hash(key)
w := p.workers[idx]
if w == nil {
w = NewQueueWorker(p.params)
p.workers[idx] = w
}
p.Unlock()
w.Submit(job)
}
func (p *queuePool) Drain() {
p.drain.Once(func() {
p.Lock()
var wg sync.WaitGroup
for _, w := range p.workers {
if w != nil {
wg.Add(1)
w := w
go func() {
w.Drain()
wg.Done()
}()
}
}
wg.Wait()
p.Unlock()
})
}
func (p *queuePool) Kill() {
p.kill.Once(func() {
p.Lock()
for _, w := range p.workers {
if w != nil {
w.Kill()
}
}
p.Unlock()
})
}
type worker struct {
sync.Mutex
QueueWorkerParams
active bool
next chan func()
deque *deque.Deque[func()]
draining Fuse
done Fuse
kill Fuse
}
func NewQueueWorker(params QueueWorkerParams) QueueWorker {
if params.QueueSize == 0 {
params.QueueSize = DefaultQueueSize
}
w := &worker{
QueueWorkerParams: params,
next: make(chan func(), 1),
deque: deque.New[func()](params.QueueSize),
}
go w.run()
return w
}
func (w *worker) run() {
kill := w.kill.Watch()
draining := w.draining.Watch()
for {
select {
case <-kill:
return
case <-draining:
w.drain()
return
case job := <-w.next:
if job != nil {
job()
}
w.Lock()
if w.deque.Len() > 0 {
w.next <- w.deque.PopFront()
} else {
w.active = false
}
w.Unlock()
}
}
}
func (w *worker) Submit(job func()) {
w.Lock()
if w.active {
if w.DropWhenFull && w.deque.Len() == w.QueueSize {
if w.OnDropped != nil {
w.OnDropped()
}
} else {
w.deque.PushBack(job)
}
} else {
w.active = true
w.next <- job
}
w.Unlock()
}
func (w *worker) Drain() {
w.draining.Break()
select {
case <-w.done.Watch():
case <-w.kill.Watch():
}
}
func (w *worker) drain() {
w.Lock()
select {
case job := <-w.next:
if job != nil {
job()
}
default:
}
count := w.deque.Len()
for i := 0; i < count; i++ {
w.deque.PopFront()()
}
w.done.Break()
w.Unlock()
}
func (w *worker) Kill() {
w.kill.Break()
}