-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathqueue.go
More file actions
267 lines (215 loc) · 6.89 KB
/
Copy pathqueue.go
File metadata and controls
267 lines (215 loc) · 6.89 KB
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
package varmq
import (
"io"
)
// queue is the base implementation of the Queue interface
// It contains an externalBaseQueue for worker management and an internalQueue for job storage
type queue[T any] struct {
*externalBaseQueue
internalQueue IQueue
}
type Queue[T any] interface {
IExternalBaseQueue
// Worker returns the bound worker.
Worker() Worker
// Add adds a new Job to the queue and returns a EnqueuedJob to handle the job.
// Time complexity: O(1)
Add(data T, configs ...JobConfigFunc) (EnqueuedJob, bool)
// AddAll adds multiple Jobs to the queue and returns a EnqueuedGroupJob to handle the job.
// Time complexity: O(n) where n is the number of Jobs added
AddAll(data []Item[T]) EnqueuedGroupJob
}
// Item represents a data item to be processed by a worker
// It combines a unique identifier with a value of any generic type
// and a optional priority for priority queues
// This only used for AddAll operations
type Item[T any] struct {
ID string
Data T
Priority int
}
// newQueue creates a new queue with the given worker and internal queue implementation
// It sets the worker's queue to the provided queue and creates a new external queue for job management
func newQueue[T any](w *worker[T, iJob[T]], q IQueue, configs ...QueueConfigFunc) *queue[T] {
c := loadQueueConfigs(configs...)
w.queues.Register(q, c.priority)
return &queue[T]{
externalBaseQueue: newExternalQueue(q, w, c),
internalQueue: q,
}
}
func (q *queue[T]) Add(data T, configs ...JobConfigFunc) (EnqueuedJob, bool) {
j := newJob(data, loadJobConfigs(q.w.configs(), configs...))
if ok := q.internalQueue.Enqueue(j); !ok {
j.Close()
return nil, false
}
q.w.Metrics().incSubmitted()
j.changeStatus(queued)
q.w.notifyToPullNextJobs()
return j, true
}
func (q *queue[T]) AddAll(items []Item[T]) EnqueuedGroupJob {
groupJob := newGroupJob[T](len(items))
for _, item := range items {
j := groupJob.newJob(item.Data, loadJobConfigs(q.w.configs(), WithJobId(item.ID)))
if ok := q.internalQueue.Enqueue(j); !ok {
j.Close()
continue
}
q.w.Metrics().incSubmitted()
j.changeStatus(queued)
q.w.notifyToPullNextJobs()
}
return groupJob
}
type errorQueue[T any] struct {
*externalBaseQueue
internalQueue IQueue
}
type ErrQueue[T any] interface {
IExternalBaseQueue
// Worker returns the bound worker.
Worker() Worker
// Add adds a new Job to the queue and returns a EnqueuedErrJob to handle the job with error receiving.
// Time complexity: O(1)
Add(data T, configs ...JobConfigFunc) (EnqueuedErrJob, bool)
// AddAll adds multiple Jobs to the queue and returns a EnqueuedErrGroupJob to handle the job with error receiving.
// Time complexity: O(n) where n is the number of Jobs added
AddAll(data []Item[T]) EnqueuedErrGroupJob
}
func newErrorQueue[T any](w *worker[T, iErrorJob[T]], q IQueue, configs ...QueueConfigFunc) *errorQueue[T] {
c := loadQueueConfigs(configs...)
w.queues.Register(q, c.priority)
return &errorQueue[T]{
externalBaseQueue: newExternalQueue(q, w, c),
internalQueue: q,
}
}
func (q *errorQueue[T]) Add(data T, configs ...JobConfigFunc) (EnqueuedErrJob, bool) {
j := newErrorJob(data, loadJobConfigs(q.w.configs(), configs...))
if ok := q.internalQueue.Enqueue(j); !ok {
j.Close()
return nil, false
}
q.w.Metrics().incSubmitted()
j.changeStatus(queued)
q.w.notifyToPullNextJobs()
return j, true
}
func (q *errorQueue[T]) AddAll(items []Item[T]) EnqueuedErrGroupJob {
groupJob := newErrorGroupJob[T](len(items))
for _, item := range items {
j := groupJob.newJob(item.Data, loadJobConfigs(q.w.configs(), WithJobId(item.ID)))
if ok := q.internalQueue.Enqueue(j); !ok {
j.Close()
continue
}
q.w.Metrics().incSubmitted()
j.changeStatus(queued)
q.w.notifyToPullNextJobs()
}
return groupJob
}
type resultQueue[T, R any] struct {
*externalBaseQueue
internalQueue IQueue
}
type ResultQueue[T, R any] interface {
IExternalBaseQueue
// Worker returns the bound worker.
Worker() Worker
// Add adds a new Job to the queue and returns a EnqueuedResultJob to handle the job with result receiving.
// Time complexity: O(1)
Add(data T, configs ...JobConfigFunc) (EnqueuedResultJob[R], bool)
// AddAll adds multiple Jobs to the queue and returns a EnqueuedResultGroupJob to handle the job with result receiving.
// Time complexity: O(n) where n is the number of Jobs added
AddAll(data []Item[T]) EnqueuedResultGroupJob[R]
}
func newResultQueue[T, R any](w *worker[T, iResultJob[T, R]], q IQueue, configs ...QueueConfigFunc) *resultQueue[T, R] {
c := loadQueueConfigs(configs...)
w.queues.Register(q, c.priority)
return &resultQueue[T, R]{
externalBaseQueue: newExternalQueue(q, w, c),
internalQueue: q,
}
}
func (q *resultQueue[T, R]) Add(data T, configs ...JobConfigFunc) (EnqueuedResultJob[R], bool) {
j := newResultJob[T, R](data, loadJobConfigs(q.w.configs(), configs...))
if ok := q.internalQueue.Enqueue(j); !ok {
j.Close()
return nil, false
}
q.w.Metrics().incSubmitted()
j.changeStatus(queued)
q.w.notifyToPullNextJobs()
return j, true
}
func (q *resultQueue[T, R]) AddAll(items []Item[T]) EnqueuedResultGroupJob[R] {
groupJob := newResultGroupJob[T, R](len(items))
for _, item := range items {
j := groupJob.newJob(item.Data, loadJobConfigs(q.w.configs(), WithJobId(item.ID)))
if ok := q.internalQueue.Enqueue(j); !ok {
j.Close()
continue
}
q.w.Metrics().incSubmitted()
j.changeStatus(queued)
q.w.notifyToPullNextJobs()
}
return groupJob
}
type externalBaseQueue struct {
w Worker
q IBaseQueue
config queueConfig
}
type IExternalBaseQueue interface {
// Deprecated: Use Queue's Len() method instead for a more idiomatic approach.
// This method will be removed in an upcoming release.
NumPending() int
// Len returns the number of jobs in the queue.
Len() int
// IsFull returns true if the queue has reached its configured capacity.
// If no capacity is set, it always returns false.
IsFull() bool
// Purge removes all pending Jobs from the queue.
Purge()
// Close closes the queue, no more jobs can be added to the queue after closing the queue.
Close() error
}
func newExternalQueue(q IBaseQueue, worker Worker, config queueConfig) *externalBaseQueue {
if cap, ok := q.(CapacitySetter); ok {
cap.SetCapacity(config.capacity)
}
return &externalBaseQueue{
w: worker,
q: q,
config: config,
}
}
func (eq *externalBaseQueue) IsFull() bool {
return eq.config.capacity > 0 && eq.q.Len() >= eq.config.capacity
}
func (eq *externalBaseQueue) NumPending() int {
return eq.q.Len()
}
func (eq *externalBaseQueue) Len() int {
return eq.q.Len()
}
func (eq *externalBaseQueue) Worker() Worker {
return eq.w
}
func (eq *externalBaseQueue) Purge() {
prevValues := eq.q.Values()
eq.q.Purge()
// close all pending channels to avoid routine leaks
for _, val := range prevValues {
if j, ok := val.(io.Closer); ok {
j.Close()
}
}
}
func (eq *externalBaseQueue) Close() error {
return eq.q.Close()
}