-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer.go
More file actions
272 lines (217 loc) · 6.53 KB
/
consumer.go
File metadata and controls
272 lines (217 loc) · 6.53 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
268
269
270
271
272
package bunny
import (
"errors"
"fmt"
"sync"
"sync/atomic"
"github.com/google/uuid"
"github.com/streadway/amqp"
"github.com/Shimmur/bunny/adapter"
)
type Consumer interface {
Consume(consumeFunc ConsumeFunc, opts ConsumeOptions, errs chan<- error) error
Cancel(noWait bool) error
}
// Function that is run against evey item delivered from rabbit
// Author of this function is responsible for calling Delivery.Ack()
type ConsumeFunc func(msg *amqp.Delivery) error
type ConsumeOptions struct {
QueueName string
AutoAck bool
Exclusive bool
NoWait bool
QoSOptions QoSOptions
}
type QoSOptions struct {
PrefetchCount int
PrefetchSize int
Global bool
}
type status uint32
const (
statusCreated status = iota
statusActive
statusCancelled
)
func (s status) String() string {
switch s {
case statusCreated:
return "created"
case statusActive:
return "active"
case statusCancelled:
return "cancelled"
default:
return "unknown"
}
}
type consumer struct {
id string
status *status
amqpChan adapter.AMQPChannel
chanSetupFuncs []SetupFunc
consumeFunc ConsumeFunc
consumerTag string
opts *ConsumeOptions
deliveryChan <-chan amqp.Delivery
errorChan chan<- error
deliveryMux rwLocker
rmCallback func(string)
}
func (b *bunny) NewConsumerChannel(setupFuncs ...SetupFunc) (Consumer, error) {
if b.connections == nil {
return nil, errors.New("no connection! Must call Connect()")
}
id, err := uuid.NewRandom()
if err != nil {
return nil, fmt.Errorf("failed to generate ID for consumer")
}
// create our representation of the consumer chan to store
c := &consumer{
id: id.String(),
chanSetupFuncs: setupFuncs,
deliveryMux: &sync.RWMutex{},
}
c.setStatus(statusCreated)
if err := b.connections.establishConsumerChan(c); err != nil {
return nil, err
}
log.Debugf("new consumer created with ID %s", c.id)
return c, nil
}
// exported version of consume for the user to kick of consumption
func (c *consumer) Consume(consumeFunc ConsumeFunc, opts ConsumeOptions, errs chan<- error) error {
// Enforce one consumer per channel, and also prevent consuming on cancelled
// This also prevents reuse of cancelled consumers to avoid any unexpected issues
// which may be hard to debug
if c.getStatus() != statusCreated {
return fmt.Errorf("Consume() can not be called on consumer in %q state", c.status)
}
c.consumeFunc = consumeFunc
c.opts = &opts
c.errorChan = errs
err := c.amqpChan.Qos(c.opts.QoSOptions.PrefetchCount, c.opts.QoSOptions.PrefetchSize, c.opts.QoSOptions.Global)
if err != nil {
return fmt.Errorf("failed to set qos options: %v", err)
}
log.Debugf("Starting consumer %s...", c.id)
if err := c.consume(); err != nil {
return err
}
return nil
}
// internal consume that is reusable for restarts
func (c *consumer) consume() error {
// generate a consumer tag to ensure uniqueness
id, err := uuid.NewRandom()
if err != nil {
return fmt.Errorf("failed to generate consumer tag: %v", err)
}
// the consumer tag is kept separate from the consumer ID because the consumer ID
// will survive restarts, but the consumer tag must not.
c.consumerTag = id.String()
log.Debugf("setting up to consume from queue %s with consumer tag %s...", c.opts.QueueName, c.consumerTag)
deliveries, err := c.amqpChan.Consume(
c.opts.QueueName,
c.consumerTag,
c.opts.AutoAck,
c.opts.Exclusive,
false, // noLocal is not supported by Rabbit
c.opts.NoWait,
nil,
)
if err != nil {
return fmt.Errorf("failed to begin consuming from channel: %v consumer tag: %s", err, c.consumerTag)
}
c.setStatus(statusActive)
log.Debugf("kicking off consumer func for consumer tag %s", c.consumerTag)
// must lock here because in the restart case, we may still be consuming from it
c.deliveryMux.Lock()
c.deliveryChan = deliveries
c.deliveryMux.Unlock()
go c.consumeLoop()
log.Debugf("consuming from queue %s with consumer tag %s", c.opts.QueueName, c.consumerTag)
return nil
}
// returned bool is for testing purposes
func (c *consumer) consumeLoop() bool {
// TODO: take ctx code from Rabbit lib for cancels
for {
// TODO: this should not continue to consume if we restarted. Will there be two?
// should use a select here and have a close/cancel chan as well
item, ok := <-c.deliveries()
if !ok {
log.Debugf("got delivery channel close! consumer tag: %s", c.consumerTag)
// just exit this goroutine and allow another one to be
// spawned on a new delivery chan
return true
}
if err := c.consumeFunc(&item); err != nil {
log.Debugf("error during consume: %s", err)
if c.errorChan != nil {
c.errorChan <- err
}
}
}
}
func (c *consumer) Cancel(noWait bool) error {
closed := false
// This will trigger a close of the delivery channel and stop the consumer loop as well
if err := c.amqpChan.Cancel(c.consumerTag, noWait); err != nil {
if err != amqp.ErrClosed {
return err
}
// we got an ErrClosed so its already closed
closed = true
}
c.setStatus(statusCancelled)
// remove itself from consumers
c.rmCallback(c.id)
// cleanup the channel if not closed already
if !closed {
if err := c.amqpChan.Close(); err != nil {
return fmt.Errorf("failed to close channel for consumer ID %s: %v", c.consumerTag, err)
}
}
return nil
}
// helper wrapper for Consume in the restart case
func (c *consumer) restart(ch adapter.AMQPChannel) error {
log.Debugf("Restarting consumer %s...", c.id)
// save the new channel
c.amqpChan = ch
// TODO: first kill the previous consumer to make sure that we do not leave it dangling
log.Debugf("calling consumer setup function on restart for consumerID: %s", c.id)
// run the setup func if it's not nil
for _, setupFunc := range c.chanSetupFuncs {
if err := setupFunc(adapter.UnwrapChannel(ch)); err != nil {
return fmt.Errorf("failed to setup channel topology on restart: %v", err)
}
}
if err := c.consume(); err != nil {
return fmt.Errorf("failed to begin consuming from channel on consumer restart: %v", err)
}
return nil
}
func (c *consumer) setStatus(st status) {
if c.status == nil {
c.status = &st
return
}
atomic.StoreUint32((*uint32)(c.status), uint32(st))
}
func (c *consumer) getStatus() status {
if c.status == nil {
return statusCreated // default
}
return status(atomic.LoadUint32((*uint32)(c.status)))
}
// required to meet the restartable interface
func (c *consumer) getID() string {
return c.id
}
func (c *consumer) deliveries() <-chan amqp.Delivery {
c.deliveryMux.RLock()
defer c.deliveryMux.RUnlock()
return c.deliveryChan
}