-
Notifications
You must be signed in to change notification settings - Fork 108
/
queues.go
469 lines (397 loc) · 14.1 KB
/
queues.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package rabbithole
import (
"encoding/json"
"net/http"
"net/url"
)
// BackingQueueStatus exposes backing queue (queue storage engine) metrics.
// They can change in a future version of RabbitMQ.
type BackingQueueStatus struct {
Q1 int `json:"q1,omitempty"`
Q2 int `json:"q2,omitempty"`
Q3 int `json:"q3,omitempty"`
Q4 int `json:"q4,omitempty"`
// Total queue length
Length int64 `json:"len,omitempty"`
// Number of pending acks from consumers
PendingAcks int64 `json:"pending_acks,omitempty"`
// Number of messages held in RAM
RAMMessageCount int64 `json:"ram_msg_count,omitempty"`
// Number of outstanding acks held in RAM
RAMAckCount int64 `json:"ram_ack_count,omitempty"`
// Number of persistent messages in the store
PersistentCount int64 `json:"persistent_count,omitempty"`
// Average ingress (inbound) rate, not including messages
// that straight through to auto-acking consumers.
AverageIngressRate float64 `json:"avg_ingress_rate,omitempty"`
// Average egress (outbound) rate, not including messages
// that straight through to auto-acking consumers.
AverageEgressRate float64 `json:"avg_egress_rate,omitempty"`
// rate at which unacknowledged message records enter RAM,
// e.g. because messages are delivered requiring acknowledgement
AverageAckIngressRate float32 `json:"avg_ack_ingress_rate,omitempty"`
// rate at which unacknowledged message records leave RAM,
// e.g. because acks arrive or unacked messages are paged out
AverageAckEgressRate float32 `json:"avg_ack_egress_rate,omitempty"`
}
// OwnerPidDetails describes an exclusive queue owner (connection).
type OwnerPidDetails struct {
Name string `json:"name,omitempty"`
PeerPort Port `json:"peer_port,omitempty"`
PeerHost string `json:"peer_host,omitempty"`
}
// ConsumerDetail describe consumer information with a queue
type ConsumerDetail struct {
Arguments map[string]interface{} `json:"arguments"`
ChannelDetails ChannelDetails `json:"channel_details"`
AckRequired bool `json:"ack_required"`
Active bool `json:"active"`
ActiveStatus string `json:"active_status"`
ConsumerTag string `json:"consumer_tag"`
Exclusive bool `json:"exclusive,omitempty"`
PrefetchCount uint `json:"prefetch_count"`
Queue QueueDetail `json:"queue"`
}
// ChannelDetails describe channel information with a consumer
type ChannelDetails struct {
ConnectionName string `json:"connection_name"`
Name string `json:"name"`
Node string `json:"node"`
Number uint `json:"number"`
PeerHost string `json:"peer_host"`
PeerPort uint `json:"peer_port"`
User string `json:"user"`
}
// QueueDetail describe queue information with a consumer
type QueueDetail struct {
Name string `json:"name"`
Vhost string `json:"vhost,omitempty"`
}
// GarbageCollectionDetail describe queue garbage collection information
type GarbageCollectionDetails struct {
FullSweepAfter int `json:"fullsweep_after"`
MaxHeapSize int `json:"max_heap_size"`
MinBinVheapSize int `json:"min_bin_vheap_size"`
MinHeapSize int `json:"min_heap_size"`
MinorGCs int `json:"minor_gcs"`
}
// QueueInfo represents a queue, its properties and key metrics.
type QueueInfo struct {
// Queue name
Name string `json:"name"`
// Queue type
Type string `json:"type,omitempty"`
// Virtual host this queue belongs to
Vhost string `json:"vhost,omitempty"`
// Is this queue durable?
Durable bool `json:"durable"`
// Is this queue auto-deleted?
AutoDelete AutoDelete `json:"auto_delete"`
// Is this queue exclusive?
Exclusive bool `json:"exclusive,omitempty"`
// Extra queue arguments
Arguments map[string]interface{} `json:"arguments"`
// RabbitMQ node that hosts master for this queue
Node string `json:"node,omitempty"`
// Queue status
Status string `json:"state,omitempty"`
// Queue leader when it is quorum queue
Leader string `json:"leader,omitempty"`
// Queue members when it is quorum queue
Members []string `json:"members,omitempty"`
// Queue online members when it is quorum queue
Online []string `json:"online,omitempty"`
// Total amount of RAM used by this queue
Memory int64 `json:"memory,omitempty"`
// How many consumers this queue has
Consumers int `json:"consumers,omitempty"`
// Detail information of consumers
ConsumerDetails *[]ConsumerDetail `json:"consumer_details,omitempty"`
// Utilisation of all the consumers
ConsumerUtilisation float64 `json:"consumer_utilisation,omitempty"`
// If there is an exclusive consumer, its consumer tag
ExclusiveConsumerTag string `json:"exclusive_consumer_tag,omitempty"`
// GarbageCollection metrics
GarbageCollection *GarbageCollectionDetails `json:"garbage_collection,omitempty"`
// Policy applied to this queue, if any
Policy string `json:"policy,omitempty"`
// Total bytes of messages in this queues
MessagesBytes int64 `json:"message_bytes,omitempty"`
MessagesBytesPersistent int64 `json:"message_bytes_persistent,omitempty"`
MessagesBytesRAM int64 `json:"message_bytes_ram,omitempty"`
MessagesBytesReady int64 `json:"message_bytes_ready,omitempty"`
MessagesBytesUnacknowledged int64 `json:"message_bytes_unacknowledged,omitempty"`
// Total number of messages in this queue
Messages int `json:"messages,omitempty"`
MessagesDetails *RateDetails `json:"messages_details,omitempty"`
MessagesPersistent int `json:"messages_persistent,omitempty"`
MessagesRAM int `json:"messages_ram,omitempty"`
// Number of messages ready to be delivered
MessagesReady int `json:"messages_ready,omitempty"`
MessagesReadyDetails *RateDetails `json:"messages_ready_details,omitempty"`
// Number of messages delivered and pending acknowledgements from consumers
MessagesUnacknowledged int `json:"messages_unacknowledged,omitempty"`
MessagesUnacknowledgedDetails *RateDetails `json:"messages_unacknowledged_details,omitempty"`
MessageStats *MessageStats `json:"message_stats,omitempty"`
OwnerPidDetails *OwnerPidDetails `json:"owner_pid_details,omitempty"`
BackingQueueStatus *BackingQueueStatus `json:"backing_queue_status,omitempty"`
ActiveConsumers int64 `json:"active_consumers,omitempty"`
}
// PagedQueueInfo is additional context returned for paginated requests.
type PagedQueueInfo struct {
Page int `json:"page"`
PageCount int `json:"page_count"`
PageSize int `json:"page_size"`
FilteredCount int `json:"filtered_count"`
ItemCount int `json:"item_count"`
TotalCount int `json:"total_count"`
Items []QueueInfo `json:"items"`
}
// DetailedQueueInfo is an alias for QueueInfo
type DetailedQueueInfo QueueInfo
//
// GET /api/queues
//
// [
// {
// "owner_pid_details": {
// "name": "127.0.0.1:46928 -> 127.0.0.1:5672",
// "peer_port": 46928,
// "peer_host": "127.0.0.1"
// },
// "message_stats": {
// "publish": 19830,
// "publish_details": {
// "rate": 5
// }
// },
// "messages": 15,
// "messages_details": {
// "rate": 0
// },
// "messages_ready": 15,
// "messages_ready_details": {
// "rate": 0
// },
// "messages_unacknowledged": 0,
// "messages_unacknowledged_details": {
// "rate": 0
// },
// "policy": "",
// "exclusive_consumer_tag": "",
// "consumers": 0,
// "memory": 143112,
// "backing_queue_status": {
// "q1": 0,
// "q2": 0,
// "delta": [
// "delta",
// "undefined",
// 0,
// "undefined"
// ],
// "q3": 0,
// "q4": 15,
// "len": 15,
// "pending_acks": 0,
// "target_ram_count": "infinity",
// "ram_msg_count": 15,
// "ram_ack_count": 0,
// "next_seq_id": 19830,
// "persistent_count": 0,
// "avg_ingress_rate": 4.9920127795527,
// "avg_egress_rate": 4.9920127795527,
// "avg_ack_ingress_rate": 0,
// "avg_ack_egress_rate": 0
// },
// "status": "running",
// "name": "amq.gen-QLEaT5Rn_ogbN3O8ZOQt3Q",
// "vhost": "rabbit\/hole",
// "durable": false,
// "auto_delete": false,
// "arguments": {
// "x-message-ttl": 5000
// },
// "node": "rabbit@marzo"
// }
// ]
// ListQueues lists all queues in the cluster. This only includes queues in the
// virtual hosts accessible to the user.
func (c *Client) ListQueues() (rec []QueueInfo, err error) {
req, err := newGETRequest(c, "queues")
if err != nil {
return []QueueInfo{}, err
}
if err = executeAndParseRequest(c, req, &rec); err != nil {
return []QueueInfo{}, err
}
return rec, nil
}
// ListQueuesWithParameters lists queues with a list of query string values.
func (c *Client) ListQueuesWithParameters(params url.Values) (rec []QueueInfo, err error) {
req, err := newGETRequestWithParameters(c, "queues", params)
if err != nil {
return []QueueInfo{}, err
}
if err = executeAndParseRequest(c, req, &rec); err != nil {
return []QueueInfo{}, err
}
return rec, nil
}
// PagedListQueuesWithParameters lists queues with pagination.
func (c *Client) PagedListQueuesWithParameters(params url.Values) (rec PagedQueueInfo, err error) {
req, err := newGETRequestWithParameters(c, "queues", params)
if err != nil {
return PagedQueueInfo{}, err
}
if err = executeAndParseRequest(c, req, &rec); err != nil {
return PagedQueueInfo{}, err
}
return rec, nil
}
//
// GET /api/queues/{vhost}
//
// ListQueuesIn lists all queues in a virtual host.
func (c *Client) ListQueuesIn(vhost string) (rec []QueueInfo, err error) {
req, err := newGETRequest(c, "queues/"+url.PathEscape(vhost))
if err != nil {
return []QueueInfo{}, err
}
if err = executeAndParseRequest(c, req, &rec); err != nil {
return []QueueInfo{}, err
}
return rec, nil
}
//
// GET /api/queues/{vhost}/{name}
//
// GetQueue returns information about a queue.
func (c *Client) GetQueue(vhost, queue string) (rec *DetailedQueueInfo, err error) {
req, err := newGETRequest(c, "queues/"+url.PathEscape(vhost)+"/"+url.PathEscape(queue))
if err != nil {
return nil, err
}
if err = executeAndParseRequest(c, req, &rec); err != nil {
return nil, err
}
return rec, nil
}
//
// GET /api/queues/{vhost}/{name}?{query}
// GetQueueWithParameters returns information about a queue. Compared to the regular GetQueue function,
// this one accepts additional query string values.
func (c *Client) GetQueueWithParameters(vhost, queue string, qs url.Values) (rec *DetailedQueueInfo, err error) {
req, err := newGETRequestWithParameters(c, "queues/"+url.PathEscape(vhost)+"/"+url.PathEscape(queue), qs)
if err != nil {
return nil, err
}
if err = executeAndParseRequest(c, req, &rec); err != nil {
return nil, err
}
return rec, nil
}
//
// PUT /api/exchanges/{vhost}/{exchange}
//
// QueueSettings represents queue properties. Use it to declare a queue.
type QueueSettings struct {
Type string `json:"type"`
Durable bool `json:"durable"`
AutoDelete bool `json:"auto_delete,omitempty"`
Arguments map[string]interface{} `json:"arguments,omitempty"`
}
// DeclareQueue declares a queue.
func (c *Client) DeclareQueue(vhost, queue string, info QueueSettings) (res *http.Response, err error) {
if info.Arguments == nil {
info.Arguments = make(map[string]interface{})
}
if info.Type != "" {
info.Arguments["x-queue-type"] = info.Type
}
body, err := json.Marshal(info)
if err != nil {
return nil, err
}
req, err := newRequestWithBody(c, "PUT", "queues/"+url.PathEscape(vhost)+"/"+url.PathEscape(queue), body)
if err != nil {
return nil, err
}
if res, err = executeRequest(c, req); err != nil {
return nil, err
}
return res, nil
}
//
// DELETE /api/queues/{vhost}/{name}
//
// Options for deleting a queue. Use it with DeleteQueue.
type QueueDeleteOptions struct {
// Only delete the queue if there are no messages.
IfEmpty bool
// Only delete the queue if there are no consumers.
IfUnused bool
}
// DeleteQueue deletes a queue.
func (c *Client) DeleteQueue(vhost, queue string, opts ...QueueDeleteOptions) (res *http.Response, err error) {
query := url.Values{}
for _, o := range opts {
if o.IfEmpty {
query["if-empty"] = []string{"true"}
}
if o.IfUnused {
query["if-unused"] = []string{"true"}
}
}
req, err := newRequestWithBody(c, "DELETE", "queues/"+url.PathEscape(vhost)+"/"+url.PathEscape(queue)+"?"+query.Encode(), nil)
if err != nil {
return nil, err
}
if res, err = executeRequest(c, req); err != nil {
return nil, err
}
return res, nil
}
//
// DELETE /api/queues/{vhost}/{name}/contents
//
// PurgeQueue purges a queue (deletes all messages ready for delivery in it).
func (c *Client) PurgeQueue(vhost, queue string) (res *http.Response, err error) {
req, err := newRequestWithBody(c, "DELETE", "queues/"+url.PathEscape(vhost)+"/"+url.PathEscape(queue)+"/contents", nil)
if err != nil {
return nil, err
}
if res, err = executeRequest(c, req); err != nil {
return nil, err
}
return res, nil
}
// queueAction represents an action that can be performed on a queue (sync/cancel_sync)
type queueAction struct {
Action string `json:"action"`
}
// SyncQueue synchronises queue contents with the mirrors remaining in the cluster.
func (c *Client) SyncQueue(vhost, queue string) (res *http.Response, err error) {
return c.sendQueueAction(vhost, queue, queueAction{"sync"})
}
// CancelSyncQueue cancels queue synchronisation process.
func (c *Client) CancelSyncQueue(vhost, queue string) (res *http.Response, err error) {
return c.sendQueueAction(vhost, queue, queueAction{"cancel_sync"})
}
//
// POST /api/queues/{vhost}/{name}/actions
//
func (c *Client) sendQueueAction(vhost string, queue string, action queueAction) (res *http.Response, err error) {
body, err := json.Marshal(action)
if err != nil {
return nil, err
}
req, err := newRequestWithBody(c, "POST", "queues/"+url.PathEscape(vhost)+"/"+url.PathEscape(queue)+"/actions", body)
if err != nil {
return nil, err
}
if res, err = executeRequest(c, req); err != nil {
return nil, err
}
return res, nil
}