-
Notifications
You must be signed in to change notification settings - Fork 69
/
receiver.go
349 lines (296 loc) · 9.24 KB
/
receiver.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
package eventhub
// MIT License
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE
import (
"context"
"fmt"
"time"
"github.com/Azure/azure-amqp-common-go/persist"
"github.com/Azure/azure-event-hubs-go/mgmt"
log "github.com/sirupsen/logrus"
"pack.ag/amqp"
)
const (
// DefaultConsumerGroup is the default name for a event stream consumer group
DefaultConsumerGroup = "$Default"
offsetAnnotationName = "x-opt-offset"
amqpAnnotationFormat = "amqp.annotation.%s >%s '%s'"
defaultPrefetchCount = 100
epochKey = mgmt.MsftVendor + ":epoch"
)
// receiver provides session and link handling for a receiving entity path
type (
receiver struct {
hub *Hub
connection *amqp.Client
session *session
receiver *amqp.Receiver
consumerGroup string
partitionID string
prefetchCount uint32
done func()
epoch *int64
lastError error
}
// ReceiveOption provides a structure for configuring receivers
ReceiveOption func(receiver *receiver) error
// ListenerHandle provides the ability to close or listen to the close of a Receiver
ListenerHandle struct {
r *receiver
ctx context.Context
}
)
// ReceiveWithConsumerGroup configures the receiver to listen to a specific consumer group
func ReceiveWithConsumerGroup(consumerGroup string) ReceiveOption {
return func(receiver *receiver) error {
receiver.consumerGroup = consumerGroup
return nil
}
}
// ReceiveWithStartingOffset configures the receiver to start at a given position in the event stream
func ReceiveWithStartingOffset(offset string) ReceiveOption {
return func(receiver *receiver) error {
receiver.storeLastReceivedOffset(persist.NewCheckpoint(offset, 0, time.Time{}))
return nil
}
}
// ReceiveWithLatestOffset configures the receiver to start at a given position in the event stream
func ReceiveWithLatestOffset() ReceiveOption {
return func(receiver *receiver) error {
receiver.storeLastReceivedOffset(persist.NewCheckpointFromEndOfStream())
return nil
}
}
// ReceiveWithPrefetchCount configures the receiver to attempt to fetch as many messages as the prefetch amount
func ReceiveWithPrefetchCount(prefetch uint32) ReceiveOption {
return func(receiver *receiver) error {
receiver.prefetchCount = prefetch
return nil
}
}
// ReceiveWithEpoch configures the receiver to use an epoch -- see https://blogs.msdn.microsoft.com/gyan/2014/09/02/event-hubs-receiver-epoch/
func ReceiveWithEpoch(epoch int64) ReceiveOption {
return func(receiver *receiver) error {
receiver.epoch = &epoch
return nil
}
}
// newReceiver creates a new Service Bus message listener given an AMQP client and an entity path
func (h *Hub) newReceiver(ctx context.Context, partitionID string, opts ...ReceiveOption) (*receiver, error) {
receiver := &receiver{
hub: h,
consumerGroup: DefaultConsumerGroup,
prefetchCount: defaultPrefetchCount,
partitionID: partitionID,
}
for _, opt := range opts {
if err := opt(receiver); err != nil {
return nil, err
}
}
receiver.debugLogf("creating a new receiver")
err := receiver.newSessionAndLink(ctx)
return receiver, err
}
// Close will close the AMQP session and link of the receiver
func (r *receiver) Close() error {
if r.done != nil {
r.done()
}
err := r.receiver.Close()
if err != nil {
_ = r.session.Close()
_ = r.connection.Close()
return err
}
err = r.session.Close()
if err != nil {
_ = r.connection.Close()
return err
}
return r.connection.Close()
}
// Recover will attempt to close the current session and link, then rebuild them
func (r *receiver) Recover(ctx context.Context) error {
_ = r.Close() // we expect the receiver is in an error state
return r.newSessionAndLink(ctx)
}
// Listen start a listener for messages sent to the entity path
func (r *receiver) Listen(handler Handler) *ListenerHandle {
ctx, done := context.WithCancel(context.Background())
r.done = done
messages := make(chan *amqp.Message)
go r.listenForMessages(ctx, messages)
go r.handleMessages(ctx, messages, handler)
return &ListenerHandle{
r: r,
ctx: ctx,
}
}
func (r *receiver) handleMessages(ctx context.Context, messages chan *amqp.Message, handler Handler) {
for {
select {
case <-ctx.Done():
r.debugLogf("done handling messages")
return
case msg := <-messages:
id := messageID(msg)
r.debugLogf("message id: %v is being passed to handler", id)
event := eventFromMsg(msg)
err := handler(ctx, event)
if err != nil {
msg.Reject()
r.debugLogf("message rejected: id: %v", id)
continue
}
msg.Accept()
r.debugLogf("message accepted: id: %v", id)
r.storeLastReceivedOffset(event.GetCheckpoint())
}
}
}
func (r *receiver) listenForMessages(ctx context.Context, msgChan chan *amqp.Message) {
for {
msg, err := r.receiver.Receive(ctx)
if err != nil {
if ctx.Err() != nil {
return
}
r.done()
r.lastError = err
return
}
id := messageID(msg)
r.debugLogf("Message received: %s", id)
select {
case msgChan <- msg:
case <-ctx.Done():
return
}
}
}
// newSessionAndLink will replace the session and link on the receiver
func (r *receiver) newSessionAndLink(ctx context.Context) error {
connection, err := r.hub.namespace.newConnection()
if err != nil {
return err
}
r.connection = connection
address := r.getAddress()
err = r.hub.namespace.negotiateClaim(ctx, connection, address)
if err != nil {
return err
}
amqpSession, err := connection.NewSession()
if err != nil {
return err
}
offsetExpression, err := r.getOffsetExpression()
if err != nil {
return err
}
r.session, err = newSession(amqpSession)
if err != nil {
return err
}
opts := []amqp.LinkOption{
amqp.LinkSourceAddress(address),
amqp.LinkCredit(r.prefetchCount),
amqp.LinkSenderSettle(amqp.ModeUnsettled),
amqp.LinkReceiverSettle(amqp.ModeSecond),
amqp.LinkBatching(true),
amqp.LinkSelectorFilter(offsetExpression),
}
if r.epoch != nil {
opts = append(opts, amqp.LinkPropertyInt64(epochKey, *r.epoch))
}
amqpReceiver, err := amqpSession.NewReceiver(opts...)
if err != nil {
return err
}
r.receiver = amqpReceiver
return nil
}
func (r *receiver) getLastReceivedOffset() (string, error) {
checkpoint, err := r.offsetPersister().Read(r.namespaceName(), r.hubName(), r.consumerGroup, r.partitionID)
return checkpoint.Offset, err
}
func (r *receiver) storeLastReceivedOffset(checkpoint persist.Checkpoint) error {
return r.offsetPersister().Write(r.namespaceName(), r.hubName(), r.consumerGroup, r.partitionID, checkpoint)
}
func (r *receiver) getOffsetExpression() (string, error) {
offset, err := r.getLastReceivedOffset()
if err != nil {
// assume err read is due to not having an offset -- probably want to change this as it's ambiguous
return fmt.Sprintf(amqpAnnotationFormat, offsetAnnotationName, "=", persist.StartOfStream), nil
}
return fmt.Sprintf(amqpAnnotationFormat, offsetAnnotationName, "", offset), nil
}
func (r *receiver) getAddress() string {
return fmt.Sprintf("%s/ConsumerGroups/%s/Partitions/%s", r.hubName(), r.consumerGroup, r.partitionID)
}
func (r *receiver) getIdentifier() string {
if r.epoch != nil {
return fmt.Sprintf("%s/ConsumerGroups/%s/Partitions/%s/epoch/%d", r.hubName(), r.consumerGroup, r.partitionID, *r.epoch)
}
return r.getAddress()
}
func (r *receiver) namespaceName() string {
return r.hub.namespace.name
}
func (r *receiver) hubName() string {
return r.hub.name
}
func (r *receiver) offsetPersister() persist.CheckpointPersister {
return r.hub.offsetPersister
}
func messageID(msg *amqp.Message) interface{} {
var id interface{} = "null"
if msg.Properties != nil {
id = msg.Properties.MessageID
}
return id
}
func (r *receiver) debugLogf(format string, args ...interface{}) {
var msg string
if len(args) > 0 {
msg = fmt.Sprintf(format, args)
} else {
msg = format
}
log.Debugf(msg+" for entity identifier %q", r.getIdentifier())
}
// Close will close the listener
func (lc *ListenerHandle) Close() error {
return lc.r.Close()
}
// Done will close the channel when the listener has stopped
func (lc *ListenerHandle) Done() <-chan struct{} {
return lc.ctx.Done()
}
// Err will return the last error encountered
func (lc *ListenerHandle) Err() error {
if lc.r.lastError != nil {
return lc.r.lastError
}
return lc.ctx.Err()
}