-
Notifications
You must be signed in to change notification settings - Fork 467
/
subscription.go
231 lines (202 loc) · 7.49 KB
/
subscription.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
package servicebus
import (
"context"
"fmt"
"sync"
"time"
azservicebus "github.com/Azure/azure-service-bus-go"
"github.com/dapr/components-contrib/pubsub"
"github.com/dapr/dapr/pkg/logger"
)
type subscription struct {
topic string
mu sync.RWMutex
activeMessages map[string]*azservicebus.Message
entity *azservicebus.Subscription
limitConcurrentHandlers bool
handlerChan chan handler
logger logger.Logger
}
func newSubscription(topic string, sub *azservicebus.Subscription, maxConcurrentHandlers *int, logger logger.Logger) *subscription {
s := &subscription{
topic: topic,
activeMessages: make(map[string]*azservicebus.Message),
entity: sub,
logger: logger,
}
if maxConcurrentHandlers != nil {
s.logger.Debugf("Subscription to topic %s is limited to %d message handler(s)", topic, *maxConcurrentHandlers)
s.limitConcurrentHandlers = true
s.handlerChan = make(chan handler, *maxConcurrentHandlers)
for i := 0; i < *maxConcurrentHandlers; i++ {
s.handlerChan <- handler{}
}
}
return s
}
// ReceiveAndBlock is a blocking call to receive messages on an Azure Service Bus subscription from a topic
func (s *subscription) ReceiveAndBlock(ctx context.Context, appHandler func(msg *pubsub.NewMessage) error, lockRenewalInSec int, handlerTimeoutInSec int, timeoutInSec int, maxActiveMessages int, maxActiveMessagesRecoveryInSec int) error {
// Close subscription
defer func() {
closeCtx, closeCancel := context.WithTimeout(context.Background(), time.Second*time.Duration(timeoutInSec))
defer closeCancel()
s.close(closeCtx)
}()
// Lock renewal loop
go func() {
shouldRenewLocks := lockRenewalInSec > 0
if !shouldRenewLocks {
s.logger.Debugf("Lock renewal for topic %s disabled", s.topic)
return
}
for {
select {
case <-ctx.Done():
s.logger.Debugf("Lock renewal context for topic %s done", s.topic)
return
case <-time.After(time.Second * time.Duration(lockRenewalInSec)):
s.tryRenewLocks()
}
}
}()
asyncHandler := s.asyncWrapper(s.getHandlerFunc(appHandler, handlerTimeoutInSec, timeoutInSec))
// Receiver loop
for {
s.mu.RLock()
activeMessageLen := len(s.activeMessages)
s.mu.RUnlock()
if activeMessageLen >= maxActiveMessages {
// Max active messages reached, sleep to allow the current active messages to be processed before getting more
s.logger.Debugf("Max active messages %d reached for topic %s, recovering for %d seconds", maxActiveMessages, s.topic, maxActiveMessagesRecoveryInSec)
select {
case <-ctx.Done():
s.logger.Debugf("Receive context for topic %s done", s.topic)
return ctx.Err()
case <-time.After(time.Second * time.Duration(maxActiveMessagesRecoveryInSec)):
continue
}
}
if err := s.receiveMessage(ctx, asyncHandler); err != nil {
return err
}
}
}
func (s *subscription) close(ctx context.Context) {
s.logger.Debugf("Closing subscription to topic %s", s.topic)
// Ensure subscription entity is closed
if err := s.entity.Close(ctx); err != nil {
s.logger.Errorf("%s closing subscription entity for topic %s: %+v", errorMessagePrefix, s.topic, err)
}
}
func (s *subscription) getHandlerFunc(appHandler func(msg *pubsub.NewMessage) error, handlerTimeoutInSec int, timeoutInSec int) azservicebus.HandlerFunc {
return func(ctx context.Context, message *azservicebus.Message) error {
msg := &pubsub.NewMessage{
Data: message.Data,
Topic: s.topic,
}
// TODO(#1721): Context should be propogated to the app handler call for timeout and cancellation.
//
// handleCtx, handleCancel := context.WithTimeout(ctx, time.Second*time.Duration(handlerTimeoutInSec))
// defer handleCancel()
s.logger.Debugf("Calling app's handler for message %s on topic %s", message.ID, s.topic)
err := appHandler(msg)
// The context isn't handled downstream so we time out these
// operations here to avoid getting stuck waiting for a message
// to finalize (abandon/complete) if the connection has dropped.
errs := make(chan error, 1)
if err != nil {
s.logger.Warnf("Error in app's handler: %+v", err)
go func() {
errs <- s.abandonMessage(ctx, message)
}()
}
go func() {
errs <- s.completeMessage(ctx, message)
}()
select {
case err := <-errs:
return err
case <-time.After(time.Second * time.Duration(timeoutInSec)):
return fmt.Errorf("%s call to finalize message %s has timedout", errorMessagePrefix, message.ID)
case <-ctx.Done():
return ctx.Err()
}
}
}
func (s *subscription) asyncWrapper(handlerFunc azservicebus.HandlerFunc) azservicebus.HandlerFunc {
return func(ctx context.Context, msg *azservicebus.Message) error {
go func() {
s.addActiveMessage(msg)
defer s.removeActiveMessage(msg.ID)
if s.limitConcurrentHandlers {
s.logger.Debugf("Taking message handler for %s on topic %s", msg.ID, s.topic)
select {
case <-ctx.Done():
s.logger.Debugf("Message context done for %s on topic %s", msg.ID, s.topic)
return
case <-s.handlerChan: // Take or wait on a free handler before getting a new message
s.logger.Debugf("Taken message handler for %s on topic %s", msg.ID, s.topic)
}
defer func() {
s.logger.Debugf("Releasing message handler for %s on topic %s", msg.ID, s.topic)
s.handlerChan <- handler{} // Release a handler when complete
s.logger.Debugf("Released message handler for %s on topic %s", msg.ID, s.topic)
}()
}
err := handlerFunc(ctx, msg)
if err != nil {
s.logger.Errorf("%s error handling message %s on topic '%s', %s", errorMessagePrefix, msg.ID, s.topic, err)
}
}()
return nil
}
}
func (s *subscription) tryRenewLocks() {
s.mu.RLock()
activeMessageLen := len(s.activeMessages)
s.mu.RUnlock()
if activeMessageLen == 0 {
s.logger.Debugf("No active messages require lock renewal for topic %s", s.topic)
return
}
// Snapshot the messages to try to renew locks for
msgs := make([]*azservicebus.Message, 0)
s.mu.RLock()
for _, m := range s.activeMessages {
msgs = append(msgs, m)
}
s.mu.RUnlock()
// Lock renewal is best effort and not guaranteed to succeed, warnings are expected.
s.logger.Debugf("Trying to renew %d active message lock(s) for topic %s", len(msgs), s.topic)
err := s.entity.RenewLocks(context.Background(), msgs...)
if err != nil {
s.logger.Warnf("Couldn't renew all active message lock(s) for topic %s, ", s.topic, err)
}
}
func (s *subscription) receiveMessage(ctx context.Context, handler azservicebus.HandlerFunc) error {
s.logger.Debugf("Waiting to receive message on topic %s", s.topic)
if err := s.entity.ReceiveOne(ctx, handler); err != nil {
return fmt.Errorf("%s error receiving message on topic %s, %s", errorMessagePrefix, s.topic, err)
}
return nil
}
func (s *subscription) abandonMessage(ctx context.Context, m *azservicebus.Message) error {
s.logger.Debugf("Abandoning message %s on topic %s", m.ID, s.topic)
return m.Abandon(ctx)
}
func (s *subscription) completeMessage(ctx context.Context, m *azservicebus.Message) error {
s.logger.Debugf("Completing message %s on topic %s", m.ID, s.topic)
return m.Complete(ctx)
}
func (s *subscription) addActiveMessage(m *azservicebus.Message) {
s.logger.Debugf("Adding message %s to active messages on topic %s", m.ID, s.topic)
s.mu.Lock()
s.activeMessages[m.ID] = m
s.mu.Unlock()
}
func (s *subscription) removeActiveMessage(messageID string) {
s.logger.Debugf("Removing message %s from active messages on topic %s", messageID, s.topic)
s.mu.Lock()
delete(s.activeMessages, messageID)
s.mu.Unlock()
}