@@ -2,9 +2,8 @@ package rabbitmq
2
2
3
3
import (
4
4
"fmt"
5
- "time"
6
-
7
5
"github.com/streadway/amqp"
6
+ "time"
8
7
)
9
8
10
9
// Consumer allows you to create and connect to queues for data consumption.
@@ -107,13 +106,32 @@ func (consumer Consumer) StartConsuming(
107
106
return nil
108
107
}
109
108
110
- // StopConsuming stops the consumption of messages.
111
- // The consumer should be discarded as it's not safe for re-use
112
- func (consumer Consumer ) StopConsuming () {
109
+ // Disconnect disconnects both the channel and the connection.
110
+ // This method doesn't throw a reconnect, and should be used when finishing a program.
111
+ // IMPORTANT: If this method is executed before StopConsuming, it could cause unexpected behavior
112
+ // such as messages being processed, but not being acknowledged, thus being requeued by the broker
113
+ func (consumer Consumer ) Disconnect () {
113
114
consumer .chManager .channel .Close ()
114
115
consumer .chManager .connection .Close ()
115
116
}
116
117
118
+ // StopConsuming stops the consumption of messages.
119
+ // The consumer should be discarded as it's not safe for re-use.
120
+ // This method sends a basic.cancel notification.
121
+ // The consumerName is the name or delivery tag of the amqp consumer we want to cancel.
122
+ // When noWait is true, do not wait for the server to acknowledge the cancel.
123
+ // Only use this when you are certain there are no deliveries in flight that
124
+ // require an acknowledgment, otherwise they will arrive and be dropped in the
125
+ // client without an ack, and will not be redelivered to other consumers.
126
+ // IMPORTANT: Since the streadway library doesn't provide a way to retrieve the consumer's tag after the creation
127
+ // it's imperative for you to set the name when creating the consumer, if you want to use this function later
128
+ // a simple uuid4 should do the trick, since it should be unique.
129
+ // If you start many consumers, you should store the name of the consumers when creating them, such that you can
130
+ // use them in a for to stop all the consumers.
131
+ func (consumer Consumer ) StopConsuming (consumerName string , noWait bool ) {
132
+ consumer .chManager .channel .Cancel (consumerName , noWait )
133
+ }
134
+
117
135
// startGoroutinesWithRetries attempts to start consuming on a channel
118
136
// with an exponential backoff
119
137
func (consumer Consumer ) startGoroutinesWithRetries (
0 commit comments