@@ -37,22 +37,37 @@ type ManagedQueue struct {
37
37
Name string
38
38
Configuration interface {}
39
39
ExemplarType string
40
- Pool ManagedPool
40
+ Managed interface {}
41
41
counter int64
42
42
PoolWorkers map [int64 ]* PoolWorkers
43
43
}
44
44
45
+ // Flushable represents a pool or queue that is flushable
46
+ type Flushable interface {
47
+ // Flush will add a flush worker to the pool
48
+ Flush (time.Duration ) error
49
+ // IsEmpty will return if the managed pool is empty and has no work
50
+ IsEmpty () bool
51
+ }
52
+
45
53
// ManagedPool is a simple interface to get certain details from a worker pool
46
54
type ManagedPool interface {
55
+ // AddWorkers adds a number of worker as group to the pool with the provided timeout. A CancelFunc is provided to cancel the group
47
56
AddWorkers (number int , timeout time.Duration ) context.CancelFunc
57
+ // NumberOfWorkers returns the total number of workers in the pool
48
58
NumberOfWorkers () int
59
+ // MaxNumberOfWorkers returns the maximum number of workers the pool can dynamically grow to
49
60
MaxNumberOfWorkers () int
61
+ // SetMaxNumberOfWorkers sets the maximum number of workers the pool can dynamically grow to
50
62
SetMaxNumberOfWorkers (int )
63
+ // BoostTimeout returns the current timeout for worker groups created during a boost
51
64
BoostTimeout () time.Duration
65
+ // BlockTimeout returns the timeout the internal channel can block for before a boost would occur
52
66
BlockTimeout () time.Duration
67
+ // BoostWorkers sets the number of workers to be created during a boost
53
68
BoostWorkers () int
54
- SetSettings ( maxNumberOfWorkers , boostWorkers int , timeout time. Duration )
55
- Flush ( time.Duration ) error
69
+ // SetPoolSettings sets the user updatable settings for the pool
70
+ SetPoolSettings ( maxNumberOfWorkers , boostWorkers int , timeout time.Duration )
56
71
}
57
72
58
73
// ManagedQueueList implements the sort.Interface
@@ -66,7 +81,7 @@ type PoolWorkers struct {
66
81
Timeout time.Time
67
82
HasTimeout bool
68
83
Cancel context.CancelFunc
69
- IsFlush bool
84
+ IsFlusher bool
70
85
}
71
86
72
87
// PoolWorkersList implements the sort.Interface for PoolWorkers
@@ -87,26 +102,28 @@ func GetManager() *Manager {
87
102
}
88
103
89
104
// Add adds a queue to this manager
90
- func (m * Manager ) Add (name string ,
105
+ func (m * Manager ) Add (managed interface {} ,
91
106
t Type ,
92
107
configuration ,
93
- exemplar interface {},
94
- pool ManagedPool ) int64 {
108
+ exemplar interface {}) int64 {
95
109
96
110
cfg , _ := json .Marshal (configuration )
97
111
mq := & ManagedQueue {
98
112
Type : t ,
99
113
Configuration : string (cfg ),
100
114
ExemplarType : reflect .TypeOf (exemplar ).String (),
101
115
PoolWorkers : make (map [int64 ]* PoolWorkers ),
102
- Pool : pool ,
116
+ Managed : managed ,
103
117
}
104
118
m .mutex .Lock ()
105
119
m .counter ++
106
120
mq .QID = m .counter
107
121
mq .Name = fmt .Sprintf ("queue-%d" , mq .QID )
108
- if len (name ) > 0 {
109
- mq .Name = name
122
+ if named , ok := managed .(Named ); ok {
123
+ name := named .Name ()
124
+ if len (name ) > 0 {
125
+ mq .Name = name
126
+ }
110
127
}
111
128
m .Queues [mq .QID ] = mq
112
129
m .mutex .Unlock ()
@@ -155,7 +172,7 @@ func (q *ManagedQueue) Workers() []*PoolWorkers {
155
172
}
156
173
157
174
// RegisterWorkers registers workers to this queue
158
- func (q * ManagedQueue ) RegisterWorkers (number int , start time.Time , hasTimeout bool , timeout time.Time , cancel context.CancelFunc , isFlush bool ) int64 {
175
+ func (q * ManagedQueue ) RegisterWorkers (number int , start time.Time , hasTimeout bool , timeout time.Time , cancel context.CancelFunc , isFlusher bool ) int64 {
159
176
q .mutex .Lock ()
160
177
defer q .mutex .Unlock ()
161
178
q .counter ++
@@ -166,7 +183,7 @@ func (q *ManagedQueue) RegisterWorkers(number int, start time.Time, hasTimeout b
166
183
Timeout : timeout ,
167
184
HasTimeout : hasTimeout ,
168
185
Cancel : cancel ,
169
- IsFlush : isFlush ,
186
+ IsFlusher : isFlusher ,
170
187
}
171
188
return q .counter
172
189
}
@@ -195,65 +212,74 @@ func (q *ManagedQueue) RemoveWorkers(pid int64) {
195
212
196
213
// AddWorkers adds workers to the queue if it has registered an add worker function
197
214
func (q * ManagedQueue ) AddWorkers (number int , timeout time.Duration ) context.CancelFunc {
198
- if q . Pool != nil {
215
+ if pool , ok := q . Managed .( ManagedPool ); ok {
199
216
// the cancel will be added to the pool workers description above
200
- return q . Pool .AddWorkers (number , timeout )
217
+ return pool .AddWorkers (number , timeout )
201
218
}
202
219
return nil
203
220
}
204
221
205
222
// Flush flushes the queue with a timeout
206
223
func (q * ManagedQueue ) Flush (timeout time.Duration ) error {
207
- if q .Pool != nil {
208
- return q .Pool .Flush (timeout )
224
+ if flushable , ok := q .Managed .(Flushable ); ok {
225
+ // the cancel will be added to the pool workers description above
226
+ return flushable .Flush (timeout )
209
227
}
210
228
return nil
211
229
}
212
230
231
+ // IsEmpty returns if the queue is empty
232
+ func (q * ManagedQueue ) IsEmpty () bool {
233
+ if flushable , ok := q .Managed .(Flushable ); ok {
234
+ return flushable .IsEmpty ()
235
+ }
236
+ return true
237
+ }
238
+
213
239
// NumberOfWorkers returns the number of workers in the queue
214
240
func (q * ManagedQueue ) NumberOfWorkers () int {
215
- if q . Pool != nil {
216
- return q . Pool .NumberOfWorkers ()
241
+ if pool , ok := q . Managed .( ManagedPool ); ok {
242
+ return pool .NumberOfWorkers ()
217
243
}
218
244
return - 1
219
245
}
220
246
221
247
// MaxNumberOfWorkers returns the maximum number of workers for the pool
222
248
func (q * ManagedQueue ) MaxNumberOfWorkers () int {
223
- if q . Pool != nil {
224
- return q . Pool .MaxNumberOfWorkers ()
249
+ if pool , ok := q . Managed .( ManagedPool ); ok {
250
+ return pool .MaxNumberOfWorkers ()
225
251
}
226
252
return 0
227
253
}
228
254
229
255
// BoostWorkers returns the number of workers for a boost
230
256
func (q * ManagedQueue ) BoostWorkers () int {
231
- if q . Pool != nil {
232
- return q . Pool .BoostWorkers ()
257
+ if pool , ok := q . Managed .( ManagedPool ); ok {
258
+ return pool .BoostWorkers ()
233
259
}
234
260
return - 1
235
261
}
236
262
237
263
// BoostTimeout returns the timeout of the next boost
238
264
func (q * ManagedQueue ) BoostTimeout () time.Duration {
239
- if q . Pool != nil {
240
- return q . Pool .BoostTimeout ()
265
+ if pool , ok := q . Managed .( ManagedPool ); ok {
266
+ return pool .BoostTimeout ()
241
267
}
242
268
return 0
243
269
}
244
270
245
271
// BlockTimeout returns the timeout til the next boost
246
272
func (q * ManagedQueue ) BlockTimeout () time.Duration {
247
- if q . Pool != nil {
248
- return q . Pool .BlockTimeout ()
273
+ if pool , ok := q . Managed .( ManagedPool ); ok {
274
+ return pool .BlockTimeout ()
249
275
}
250
276
return 0
251
277
}
252
278
253
- // SetSettings sets the setable boost values
254
- func (q * ManagedQueue ) SetSettings (maxNumberOfWorkers , boostWorkers int , timeout time.Duration ) {
255
- if q . Pool != nil {
256
- q . Pool . SetSettings (maxNumberOfWorkers , boostWorkers , timeout )
279
+ // SetPoolSettings sets the setable boost values
280
+ func (q * ManagedQueue ) SetPoolSettings (maxNumberOfWorkers , boostWorkers int , timeout time.Duration ) {
281
+ if pool , ok := q . Managed .( ManagedPool ); ok {
282
+ pool . SetPoolSettings (maxNumberOfWorkers , boostWorkers , timeout )
257
283
}
258
284
}
259
285
0 commit comments