-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer.go
172 lines (147 loc) · 3.86 KB
/
consumer.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
package events
import (
"fmt"
"sync"
"time"
"github.com/pkg/errors"
"github.com/streadway/amqp"
"sync/atomic"
"github.com/w1ck3dg0ph3r/rabbit-events/pkg/channel"
"github.com/w1ck3dg0ph3r/rabbit-events/pkg/resequencer"
)
// consumer is an event consuming worker
//
// Encapsulates AMQP channel that is configured for consuming messages. When message is
// received on the channel, consumer will call back the Bus to handle that message.
type consumer struct {
ID int
Connection ConnectionOpenCloser
QueueName string
MaxEventsInFlight int
AcksBatchSize int
AcksMaxDelay time.Duration
EventRouter EventRouter
ShouldQuit chan error
Logger Logger
ch channel.Channel
err chan *amqp.Error
msgs <-chan amqp.Delivery
acknowledgements *resequencer.Resequencer
lastacked uint64
lastsequenced uint64
}
// Run loops over received messages until quit is closed or unrecoverable
// protocol error occurs
func (c *consumer) Run(wg *sync.WaitGroup, quit <-chan struct{}) {
defer wg.Done()
c.debugf("running event consumer %d", c.ID)
err := c.createChannel()
if err != nil {
err = errors.Wrap(err, "cant create consumer channel")
c.ShouldQuit <- err
return
}
ackTicker := time.NewTicker(c.AcksMaxDelay)
for {
select {
case msg, ok := <-c.msgs:
if ok {
e := eventFromDelivery(&msg, c)
c.EventRouter(e)
}
case tag := <-c.acknowledgements.Out:
atomic.StoreUint64(&c.lastsequenced, tag)
if tag-atomic.LoadUint64(&c.lastacked) >= uint64(c.AcksBatchSize) {
c.ackSequenced()
ackTicker = time.NewTicker(c.AcksMaxDelay)
}
case <-ackTicker.C:
c.ackSequenced()
c.ackUnsequenced()
case amqpErr, ok := <-c.err:
if ok {
if err := c.handleProtocolError(amqpErr); err != nil {
return
}
}
case <-quit:
c.debugf("stopping event consumer %d", c.ID)
c.ackSequenced()
c.ackUnsequenced()
return
}
}
}
func (c *consumer) Ack(tag uint64) {
if tag < atomic.LoadUint64(&c.lastacked) {
ignoreError(c.ch.Ack(tag, false))
return
}
c.acknowledgements.Sequence(tag)
}
func (c *consumer) Nack(tag uint64, requeue bool) {
if tag < atomic.LoadUint64(&c.lastsequenced) {
c.ackSequenced()
c.acknowledgements.Reset(tag, func(n uint64) {
ignoreError(c.ch.Ack(n, false))
})
atomic.StoreUint64(&c.lastacked, tag)
} else {
c.ackSequenced()
c.ackUnsequenced()
c.acknowledgements.StartAt(tag)
atomic.StoreUint64(&c.lastacked, tag)
}
ignoreError(c.ch.Nack(tag, false, requeue))
}
func (c *consumer) ackSequenced() {
lastsequenced := atomic.LoadUint64(&c.lastsequenced)
lastacked := atomic.LoadUint64(&c.lastacked)
if lastsequenced > lastacked {
ignoreError(c.ch.Ack(lastsequenced, true))
atomic.StoreUint64(&c.lastacked, lastsequenced)
}
}
func (c *consumer) ackUnsequenced() {
c.acknowledgements.DumpUnsequenced(func(n uint64) {
ignoreError(c.ch.Ack(n, false))
if n > atomic.LoadUint64(&c.lastacked) {
atomic.StoreUint64(&c.lastacked, n)
}
})
}
// createChannel opens and configures for consuming AMQP channel
func (c *consumer) createChannel() (err error) {
c.ch, err = c.Connection.Open()
if err != nil {
return
}
err = c.ch.Qos(c.MaxEventsInFlight, 0, false)
if err != nil {
return
}
c.err = make(chan *amqp.Error, 1)
c.err = c.ch.NotifyClose(c.err)
c.acknowledgements = resequencer.New(c.MaxEventsInFlight)
consumer := fmt.Sprintf("%s_%d", c.QueueName, c.ID)
c.msgs, err = c.ch.Consume(c.QueueName, consumer, false, false, false, false, nil)
return
}
func (c *consumer) handleProtocolError(amqpErr error) (err error) {
c.debugf("consumer %d error: %s", c.ID, amqpErr)
err = c.createChannel()
if err != nil {
c.debugf("cant recreate consumer channel %d: %s", c.ID, err)
c.ShouldQuit <- err
return
}
return
}
func ignoreError(err error) {
_ = err
}
func (c *consumer) debugf(f string, a ...interface{}) {
if c.Logger != nil {
c.Logger.Debugf(f, a...)
}
}