forked from streadway/amqp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.go
1589 lines (1277 loc) · 46.7 KB
/
channel.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012, Sean Treadway, SoundCloud Ltd.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Source code and contact info at http://github.com/streadway/amqp
package amqp
import (
"container/heap"
"reflect"
"sync"
)
// 0 1 3 7 size+7 size+8
// +------+---------+-------------+ +------------+ +-----------+
// | type | channel | size | | payload | | frame-end |
// +------+---------+-------------+ +------------+ +-----------+
// octet short long size octets octet
const frameHeaderSize = 1 + 2 + 4 + 1
/*
Channel represents an AMQP channel. Used as a context for valid message
exchange. Errors on methods with this Channel as a receiver means this channel
should be discarded and a new channel established.
*/
type Channel struct {
destructor sync.Once
sendM sync.Mutex // sequence channel frames
m sync.Mutex // struct field mutex
connection *Connection
rpc chan message
consumers consumers
id uint16
// true when we will never notify again
noNotify bool
// Channel and Connection exceptions will be broadcast on these listeners.
closes []chan *Error
// Listeners for active=true flow control. When true is sent to a listener,
// publishing should pause until false is sent to listeners.
flows []chan bool
// Listeners for returned publishings for unroutable messages on mandatory
// publishings or undeliverable messages on immediate publishings.
returns []chan Return
// Listeners for when the server notifies the client that
// a consumer has been cancelled.
cancels []chan string
// Listeners for Acks/Nacks when the channel is in Confirm mode
// the value is the sequentially increasing delivery tag
// starting at 1 immediately after the Confirm
acks []chan uint64
nacks []chan uint64
// When in confirm mode, track publish counter and order confirms
confirms tagSet
publishCounter uint64
// Selects on any errors from shutdown during RPC
errors chan *Error
// State machine that manages frame order, must only be mutated by the connection
recv func(*Channel, frame) error
// State that manages the send behavior after before and after shutdown, must
// only be mutated in shutdown()
send func(*Channel, message) error
// Current state for frame re-assembly, only mutated from recv
message messageWithContent
header *headerFrame
body []byte
}
// Constructs a new channel with the given framing rules
func newChannel(c *Connection, id uint16) *Channel {
return &Channel{
connection: c,
id: id,
rpc: make(chan message),
consumers: makeConsumers(),
recv: (*Channel).recvMethod,
send: (*Channel).sendOpen,
errors: make(chan *Error, 1),
}
}
func (me *Channel) shutdown(e *Error) {
me.destructor.Do(func() {
me.m.Lock()
defer me.m.Unlock()
// Broadcast abnormal shutdown
if e != nil {
for _, c := range me.closes {
c <- e
}
}
me.send = (*Channel).sendClosed
// Notify RPC if we're selecting
if e != nil {
me.errors <- e
}
me.consumers.closeAll()
for _, c := range me.closes {
close(c)
}
for _, c := range me.flows {
close(c)
}
for _, c := range me.returns {
close(c)
}
for _, c := range me.cancels {
close(c)
}
// A seen map to keep from double closing the ack and nacks. the other
// channels are different types and are not shared
seen := make(map[chan uint64]bool)
for _, c := range me.acks {
if !seen[c] {
close(c)
seen[c] = true
}
}
for _, c := range me.nacks {
if !seen[c] {
close(c)
seen[c] = true
}
}
me.noNotify = true
me.connection.channels.remove(me.id)
})
}
func (me *Channel) open() (err error) {
return me.call(&channelOpen{}, &channelOpenOk{})
}
// Performs a request/response call for when the message is not NoWait and is
// specified as Synchronous.
func (me *Channel) call(req message, res ...message) error {
if err := me.send(me, req); err != nil {
return err
}
if req.wait() {
select {
case e := <-me.errors:
return e
case msg := <-me.rpc:
if msg != nil {
for _, try := range res {
if reflect.TypeOf(msg) == reflect.TypeOf(try) {
// *res = *msg
vres := reflect.ValueOf(try).Elem()
vmsg := reflect.ValueOf(msg).Elem()
vres.Set(vmsg)
return nil
}
}
return ErrCommandInvalid
} else {
// RPC channel has been closed without an error, likely due to a hard
// error on the Connection. This indicates we have already been
// shutdown and if were waiting, will have returned from the errors chan.
return ErrClosed
}
}
}
return nil
}
func (me *Channel) sendClosed(msg message) (err error) {
me.sendM.Lock()
defer me.sendM.Unlock()
// After a 'channel.close' is sent or received the only valid response is
// channel.close-ok
if _, ok := msg.(*channelCloseOk); ok {
return me.connection.send(&methodFrame{
ChannelId: me.id,
Method: msg,
})
}
return ErrClosed
}
func (me *Channel) sendOpen(msg message) (err error) {
me.sendM.Lock()
defer me.sendM.Unlock()
if content, ok := msg.(messageWithContent); ok {
props, body := content.getContent()
class, _ := content.id()
size := me.connection.Config.FrameSize - frameHeaderSize
if err = me.connection.send(&methodFrame{
ChannelId: me.id,
Method: content,
}); err != nil {
return
}
if err = me.connection.send(&headerFrame{
ChannelId: me.id,
ClassId: class,
Size: uint64(len(body)),
Properties: props,
}); err != nil {
return
}
for i, j := 0, size; i < len(body); i, j = j, j+size {
if j > len(body) {
j = len(body)
}
if err = me.connection.send(&bodyFrame{
ChannelId: me.id,
Body: body[i:j],
}); err != nil {
return
}
}
} else {
err = me.connection.send(&methodFrame{
ChannelId: me.id,
Method: msg,
})
}
return
}
// Eventually called via the state machine from the connection's reader
// goroutine, so assumes serialized access.
func (me *Channel) dispatch(msg message) {
switch m := msg.(type) {
case *channelClose:
me.shutdown(newError(m.ReplyCode, m.ReplyText))
me.send(me, &channelCloseOk{})
case *channelFlow:
for _, c := range me.flows {
c <- m.Active
}
me.send(me, &channelFlowOk{Active: m.Active})
case *basicCancel:
for _, c := range me.cancels {
c <- m.ConsumerTag
}
me.send(me, &basicCancelOk{ConsumerTag: m.ConsumerTag})
case *basicReturn:
ret := newReturn(*m)
for _, c := range me.returns {
c <- *ret
}
case *basicAck:
if m.Multiple {
me.confimMultiple(m.DeliveryTag, me.acks)
} else {
me.confimOne(m.DeliveryTag, me.acks)
}
case *basicNack:
if m.Multiple {
me.confimMultiple(m.DeliveryTag, me.nacks)
} else {
me.confimOne(m.DeliveryTag, me.nacks)
}
case *basicDeliver:
me.consumers.send(m.ConsumerTag, newDelivery(me, m))
// TODO log failed consumer and close channel, this can happen when
// deliveries are in flight and a no-wait cancel has happened
default:
me.rpc <- msg
}
}
func (me *Channel) transition(f func(*Channel, frame) error) error {
me.recv = f
return nil
}
func (me *Channel) recvMethod(f frame) error {
switch frame := f.(type) {
case *methodFrame:
if msg, ok := frame.Method.(messageWithContent); ok {
me.body = make([]byte, 0)
me.message = msg
return me.transition((*Channel).recvHeader)
}
me.dispatch(frame.Method) // termination state
return me.transition((*Channel).recvMethod)
case *headerFrame:
// drop
return me.transition((*Channel).recvMethod)
case *bodyFrame:
// drop
return me.transition((*Channel).recvMethod)
default:
panic("unexpected frame type")
}
panic("unreachable")
}
func (me *Channel) recvHeader(f frame) error {
switch frame := f.(type) {
case *methodFrame:
// interrupt content and handle method
return me.recvMethod(f)
case *headerFrame:
// start collecting if we expect body frames
me.header = frame
if frame.Size == 0 {
me.message.setContent(me.header.Properties, me.body)
me.dispatch(me.message) // termination state
return me.transition((*Channel).recvMethod)
} else {
return me.transition((*Channel).recvContent)
}
case *bodyFrame:
// drop and reset
return me.transition((*Channel).recvMethod)
default:
panic("unexpected frame type")
}
panic("unreachable")
}
// state after method + header and before the length
// defined by the header has been reached
func (me *Channel) recvContent(f frame) error {
switch frame := f.(type) {
case *methodFrame:
// interrupt content and handle method
return me.recvMethod(f)
case *headerFrame:
// drop and reset
return me.transition((*Channel).recvMethod)
case *bodyFrame:
me.body = append(me.body, frame.Body...)
if uint64(len(me.body)) >= me.header.Size {
me.message.setContent(me.header.Properties, me.body)
me.dispatch(me.message) // termination state
return me.transition((*Channel).recvMethod)
}
return me.transition((*Channel).recvContent)
default:
panic("unexpected frame type")
}
panic("unreachable")
}
/*
Close initiate a clean channel closure by sending a close message with the error
code set to '200'.
It is safe to call this method multiple times.
*/
func (me *Channel) Close() error {
defer me.shutdown(nil)
return me.call(
&channelClose{ReplyCode: replySuccess},
&channelCloseOk{},
)
}
/*
NotifyClose registers a listener for when the server sends a channel or
connection exception in the form of a Connection.Close or Channel.Close method.
Connection exceptions will be broadcast to all open channels and all channels
will be closed, where channel exceptions will only be broadcast to listeners to
this channel.
The chan provided will be closed when the Channel is closed and on a
graceful close, no error will be sent.
*/
func (me *Channel) NotifyClose(c chan *Error) chan *Error {
me.m.Lock()
defer me.m.Unlock()
if me.noNotify {
close(c)
} else {
me.closes = append(me.closes, c)
}
return c
}
/*
NotifyFlow registers a listener for basic.flow methods sent by the server.
When `true` is sent on one of the listener channels, all publishers should
pause until a `false` is sent.
The server may ask the producer to pause or restart the flow of Publishings
sent by on a channel. This is a simple flow-control mechanism that a server can
use to avoid overflowing its queues or otherwise finding itself receiving more
messages than it can process. Note that this method is not intended for window
control. It does not affect contents returned by basic.get-ok methods.
When a new channel is opened, it is active (flow is active). Some
applications assume that channels are inactive until started. To emulate
this behavior a client MAY open the channel, then pause it.
Publishers should respond to a flow messages as rapidly as possible and the
server may disconnect over producing channels that do not respect these
messages.
basic.flow-ok methods will always be returned to the server regardless of
the number of listeners there are.
To control the flow of deliveries from the server. Use the Channel.Flow()
method instead.
Note: RabbitMQ will rather use TCP pushback on the network connection instead
of sending basic.flow. This means that if a single channel is producing too
much on the same connection, all channels using that connection will suffer,
including acknowledgments from deliveries. Use different Connections if you
desire to interleave consumers and producers in the same process to avoid your
basic.ack messages from getting rate limited with your basic.publish messages.
*/
func (me *Channel) NotifyFlow(c chan bool) chan bool {
me.m.Lock()
defer me.m.Unlock()
if me.noNotify {
close(c)
} else {
me.flows = append(me.flows, c)
}
return c
}
/*
NotifyReturn registers a listener for basic.return methods. These can be sent
from the server when a publish is undeliverable either from the mandatory or
immediate flags.
A return struct has a copy of the Publishing along with some error
information about why the publishing failed.
*/
func (me *Channel) NotifyReturn(c chan Return) chan Return {
me.m.Lock()
defer me.m.Unlock()
if me.noNotify {
close(c)
} else {
me.returns = append(me.returns, c)
}
return c
}
/*
NotifyCancel registers a listener for basic.cancel methods. These can be sent
from the server when a queue is deleted or when consuming from a mirrored queue
where the master has just failed (and was moved to another node)
The subscription tag is returned to the listener.
*/
func (me *Channel) NotifyCancel(c chan string) chan string {
me.m.Lock()
defer me.m.Unlock()
if me.noNotify {
close(c)
} else {
me.cancels = append(me.cancels, c)
}
return c
}
/*
NotifyConfirm registers a listener chan for reliable publishing to receive
basic.ack and basic.nack messages. These messages will be sent by the server
for every publish after Channel.Confirm has been called. The value sent on
these channels is the sequence number of the publishing. It is up to client of
this channel to maintain the sequence number of each publishing and handle
resends on basic.nack.
There will be either at most one Ack or Nack delivered for every Publishing.
Acknowledgments will be received in the order of delivery from the
NotifyConfirm channels even if the server acknowledges them out of order.
The capacity of the ack and nack channels must be at least as large as the
number of outstanding publishings. Not having enough buffered chans will
create a deadlock if you attempt to perform other operations on the Connection
or Channel while confirms are in-flight.
It's advisable to wait for all acks or nacks to arrive before calling
Channel.Close().
*/
func (me *Channel) NotifyConfirm(ack, nack chan uint64) (chan uint64, chan uint64) {
me.m.Lock()
defer me.m.Unlock()
if me.noNotify {
close(ack)
close(nack)
} else {
me.acks = append(me.acks, ack)
me.nacks = append(me.nacks, nack)
}
return ack, nack
}
// Since the acknowledgments may come out of order, scan the heap
// until found. In most cases, only the head will be found.
func (me *Channel) confimOne(tag uint64, ch []chan uint64) {
me.m.Lock()
defer me.m.Unlock()
if me.confirms != nil {
var unacked []uint64
for {
// We expect once and only once delivery
next := heap.Pop(&me.confirms).(uint64)
if next != tag {
unacked = append(unacked, next)
} else {
for _, c := range ch {
c <- tag
}
break
}
}
for _, pending := range unacked {
heap.Push(&me.confirms, pending)
}
}
}
// Instead of pushing the pending acknowledgments, deliver them as we should ack
// all up until this tag.
func (me *Channel) confimMultiple(tag uint64, ch []chan uint64) {
me.m.Lock()
defer me.m.Unlock()
if me.confirms != nil {
for {
// We expect once and only once delivery
next := heap.Pop(&me.confirms).(uint64)
for _, c := range ch {
c <- next
}
if next == tag {
break
}
}
}
}
/*
Qos controls how many messages or how many bytes the server will try to keep on
the network for consumers before receiving delivery acks. The intent of Qos is
to make sure the network buffers stay full between the server and client.
With a prefetch count greater than zero, the server will deliver that many
messages to consumers before acknowledgments are received. The server ignores
this option when consumers are started with noAck because no acknowledgments
are expected or sent.
With a prefetch size greater than zero, the server will try to keep at least
that many bytes of deliveries flushed to the network before receiving
acknowledgments from the consumers. This option is ignored when consumers are
started with noAck.
When global is true, these Qos settings apply to all existing and future
consumers on all channels on the same connection. When false, the Channel.Qos
settings will apply to all existing and future consumers on this channel.
RabbitMQ does not implement the global flag.
To get round-robin behavior between consumers consuming from the same queue on
different connections, set the prefetch count to 1, and the next available
message on the server will be delivered to the next available consumer.
If your consumer work time is reasonably is consistent and not much greater
than two times your network round trip time, you will see significant
throughput improvements starting with a prefetch count of 2 or slightly
greater as described by benchmarks on RabbitMQ.
http://www.rabbitmq.com/blog/2012/04/25/rabbitmq-performance-measurements-part-2/
*/
func (me *Channel) Qos(prefetchCount, prefetchSize int, global bool) error {
return me.call(
&basicQos{
PrefetchCount: uint16(prefetchCount),
PrefetchSize: uint32(prefetchSize),
Global: global,
},
&basicQosOk{},
)
}
/*
Cancel stops deliveries to the consumer chan established in Channel.Consume and
identified by consumer.
Only use this method to cleanly stop receiving deliveries from the server and
cleanly shut down the consumer chan identified by this tag. Using this method
and waiting for remaining messages to flush from the consumer chan will ensure
all messages received on the network will be delivered to the receiver of your
consumer chan.
Continue consuming from the chan Delivery provided by Channel.Consume until the
chan closes.
When noWait is true, do not wait for the server to acknowledge the cancel.
Only use this when you are certain there are no deliveries requiring
acknowledgment are in-flight otherwise they will arrive and be dropped in the
client without an ack and will not be redelivered to other consumers.
*/
func (me *Channel) Cancel(consumer string, noWait bool) error {
req := &basicCancel{
ConsumerTag: consumer,
NoWait: noWait,
}
res := &basicCancelOk{}
if err := me.call(req, res); err != nil {
return err
}
if req.wait() {
me.consumers.close(res.ConsumerTag)
} else {
// Potentially could drop deliveries in flight
me.consumers.close(consumer)
}
return nil
}
/*
QueueDeclare declares a queue to hold messages and deliver to consumers.
Declaring creates a queue if it doesn't already exist, or ensures that an
existing queue matches the same parameters.
Every queue declared gets a default binding to the empty exchange "" which has
the type "direct" with the routing key matching the queue's name. With this
default binding, it is possible to publish messages that route directly to
this queue by publishing to "" with the routing key of the queue name.
QueueDeclare("alerts", true, false, false false, false, nil)
Publish("", "alerts", false, false, Publishing{Body: []byte("...")})
Delivery Exchange Key Queue
-----------------------------------------------
key: alerts -> "" -> alerts -> alerts
The queue name may be empty, in which the server will generate a unique name
which will be returned in the Name field of Queue struct.
Durable and Non-Auto-Deleted queues will survive server restarts and remain
when there are no remaining consumers or bindings. Persistent publishings will
be restored in this queue on server restart. These queues are only able to be
bound to durable exchanges.
Non-Durable and Auto-Deleted queues will not be redeclared on server restart
and will be deleted by the server after a short time when the last consumer is
canceled or the last consumer's channel is closed. Queues with this lifetime
can also be deleted normally with QueueDelete. These durable queues can only
be bound to non-durable exchanges.
Non-Durable and Non-Auto-Deleted queues will remain declared as long as the
server is running regardless of how many consumers. This lifetime is useful
for temporary topologies that may have long delays between consumer activity.
These queues can only be bound to non-durable exchanges.
Durable and Auto-Deleted queues will be restored on server restart, but without
active consumers, will not survive and be removed. This Lifetime is unlikely
to be useful.
Exclusive queues are only accessible by the connection that declares them and
will be deleted when the connection closes. Channels on other connections
will receive an error when attempting declare, bind, consume, purge or delete a
queue with the same name.
When noWait is true, the queue will assume to be declared on the server. A
channel exception will arrive if the conditions are met for existing queues
or attempting to modify an existing queue from a different connection.
When the error return value is not nil, you can assume the queue could not be
declared with these parameters and the channel will be closed.
*/
func (me *Channel) QueueDeclare(name string, durable, autoDelete, exclusive, noWait bool, args Table) (Queue, error) {
if err := args.Validate(); err != nil {
return Queue{}, err
}
req := &queueDeclare{
Queue: name,
Passive: false,
Durable: durable,
AutoDelete: autoDelete,
Exclusive: exclusive,
NoWait: noWait,
Arguments: args,
}
res := &queueDeclareOk{}
if err := me.call(req, res); err != nil {
return Queue{}, err
}
if req.wait() {
return Queue{
Name: res.Queue,
Messages: int(res.MessageCount),
Consumers: int(res.ConsumerCount),
}, nil
}
return Queue{
Name: name,
}, nil
panic("unreachable")
}
/*
QueueDeclarePassive is functionally and parametrically equivalent to
QueueDeclare, except that it sets the "passive" attribute to true. A passive
queue is assumed by RabbitMQ to already exist, and attempting to connect to a
non-existent queue will cause RabbitMQ to throw an exception. This function
can be used to test for the existence of a queue.
*/
func (me *Channel) QueueDeclarePassive(name string, durable, autoDelete, exclusive, noWait bool, args Table) (Queue, error) {
if err := args.Validate(); err != nil {
return Queue{}, err
}
req := &queueDeclare{
Queue: name,
Passive: true,
Durable: durable,
AutoDelete: autoDelete,
Exclusive: exclusive,
NoWait: noWait,
Arguments: args,
}
res := &queueDeclareOk{}
if err := me.call(req, res); err != nil {
return Queue{}, err
}
if req.wait() {
return Queue{
Name: res.Queue,
Messages: int(res.MessageCount),
Consumers: int(res.ConsumerCount),
}, nil
}
return Queue{
Name: name,
}, nil
panic("unreachable")
}
/*
QueueInspect passively declares a queue by name to inspect the current message
count, consumer count.
Use this method to check how many unacknowledged messages reside in the queue
and how many consumers are receiving deliveries and whether a queue by this
name already exists.
If the queue by this name exists, use Channel.QueueDeclare check if it is
declared with specific parameters.
If a queue by this name does not exist, an error will be returned and the
channel will be closed.
*/
func (me *Channel) QueueInspect(name string) (Queue, error) {
req := &queueDeclare{
Queue: name,
Passive: true,
}
res := &queueDeclareOk{}
err := me.call(req, res)
state := Queue{
Name: name,
Messages: int(res.MessageCount),
Consumers: int(res.ConsumerCount),
}
return state, err
}
/*
QueueBind binds an exchange to a queue so that publishings to the exchange will
be routed to the queue when the publishing routing key matches the binding
routing key.
QueueBind("pagers", "alert", "log", false, nil)
QueueBind("emails", "info", "log", false, nil)
Delivery Exchange Key Queue
-----------------------------------------------
key: alert --> log ----> alert --> pagers
key: info ---> log ----> info ---> emails
key: debug --> log (none) (dropped)
If a binding with the same key and arguments already exists between the
exchange and queue, the attempt to rebind will be ignored and the existing
binding will be retained.
In the case that multiple bindings may cause the message to be routed to the
same queue, the server will only route the publishing once. This is possible
with topic exchanges.
QueueBind("pagers", "alert", "amq.topic", false, nil)
QueueBind("emails", "info", "amq.topic", false, nil)
QueueBind("emails", "#", "amq.topic", false, nil) // match everything
Delivery Exchange Key Queue
-----------------------------------------------
key: alert --> amq.topic ----> alert --> pagers
key: info ---> amq.topic ----> # ------> emails
\---> info ---/
key: debug --> amq.topic ----> # ------> emails
It is only possible to bind a durable queue to a durable exchange regardless of
whether the queue or exchange is auto-deleted. Bindings between durable queues
and exchanges will also be restored on server restart.
If the binding could not complete, an error will be returned and the channel
will be closed.
When noWait is true and the queue could not be bound, the channel will be
closed with an error.
*/
func (me *Channel) QueueBind(name, key, exchange string, noWait bool, args Table) error {
if err := args.Validate(); err != nil {
return err
}
return me.call(
&queueBind{
Queue: name,
Exchange: exchange,
RoutingKey: key,
NoWait: noWait,
Arguments: args,
},
&queueBindOk{},
)
}
/*
QueueUnbind removes a binding between an exchange and queue matching the key and
arguments.
It is possible to send and empty string for the exchange name which means to
unbind the queue from the default exchange.
*/
func (me *Channel) QueueUnbind(name, key, exchange string, args Table) error {
if err := args.Validate(); err != nil {
return err
}
return me.call(
&queueUnbind{
Queue: name,
Exchange: exchange,
RoutingKey: key,
Arguments: args,
},
&queueUnbindOk{},
)
}
/*
QueuePurge removes all messages from the named queue which are not waiting to
be acknowledged. Messages that have been delivered but have not yet been
acknowledged will not be removed.
When successful, returns the number of messages purged.
If noWait is true, do not wait for the server response and the number of
messages purged will not be meaningful.
*/
func (me *Channel) QueuePurge(name string, noWait bool) (int, error) {
req := &queuePurge{
Queue: name,
NoWait: noWait,
}
res := &queuePurgeOk{}
err := me.call(req, res)
return int(res.MessageCount), err
}
/*
QueueDelete removes the queue from the server including all bindings then
purges the messages based on server configuration, returning the number of
messages purged.
When ifUnused is true, the queue will not be deleted if there are any
consumers on the queue. If there are consumers, an error will be returned and
the channel will be closed.
When ifEmpty is true, the queue will not be deleted if there are any messages
remaining on the queue. If there are messages, an error will be returned and
the channel will be closed.
When noWait is true, the queue will be deleted without waiting for a response
from the server. The purged message count will not be meaningful. If the queue
could not be deleted, a channel exception will be raised and the channel will
be closed.
*/
func (me *Channel) QueueDelete(name string, ifUnused, ifEmpty, noWait bool) (int, error) {
req := &queueDelete{
Queue: name,
IfUnused: ifUnused,
IfEmpty: ifEmpty,
NoWait: noWait,
}
res := &queueDeleteOk{}
err := me.call(req, res)