/
ServiceBusConnection.cs
370 lines (305 loc) · 13.9 KB
/
ServiceBusConnection.cs
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
namespace Microsoft.AspNet.SignalR.ServiceBus
{
internal class ServiceBusConnection : IDisposable
{
private const int DefaultReceiveBatchSize = 1000;
private static readonly TimeSpan BackoffAmount = TimeSpan.FromSeconds(20);
private static readonly TimeSpan ErrorBackOffAmount = TimeSpan.FromSeconds(5);
private static readonly TimeSpan DefaultReadTimeout = TimeSpan.FromSeconds(60);
private static readonly TimeSpan ErrorReadTimeout = TimeSpan.FromSeconds(0.5);
private static readonly TimeSpan RetryDelay = TimeSpan.FromSeconds(10);
private readonly TimeSpan _idleSubscriptionTimeout;
private readonly NamespaceManager _namespaceManager;
private readonly MessagingFactory _factory;
private readonly ServiceBusScaleoutConfiguration _configuration;
private readonly string _connectionString;
private readonly TraceSource _trace;
public ServiceBusConnection(ServiceBusScaleoutConfiguration configuration, TraceSource traceSource)
{
_trace = traceSource;
_connectionString = configuration.BuildConnectionString();
try
{
_namespaceManager = NamespaceManager.CreateFromConnectionString(_connectionString);
_factory = MessagingFactory.CreateFromConnectionString(_connectionString);
if (configuration.RetryPolicy != null)
{
_factory.RetryPolicy = configuration.RetryPolicy;
}
else
{
_factory.RetryPolicy = RetryExponential.Default;
}
}
catch (ConfigurationErrorsException)
{
_trace.TraceError("The configured Service Bus connection string contains an invalid property. Check the exception details for more information.");
}
_idleSubscriptionTimeout = configuration.IdleSubscriptionTimeout;
_configuration = configuration;
}
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "The disposable is returned to the caller")]
public void Subscribe(ServiceBusConnectionContext connectionContext)
{
if (connectionContext == null)
{
throw new ArgumentNullException("connectionContext");
}
_trace.TraceInformation("Subscribing to {0} topic(s) in the service bus...", connectionContext.TopicNames.Count);
connectionContext.NamespaceManager = _namespaceManager;
for (var topicIndex = 0; topicIndex < connectionContext.TopicNames.Count; ++topicIndex)
{
Retry(() => CreateTopic(connectionContext, topicIndex));
}
_trace.TraceInformation("Subscription to {0} topics in the service bus Topic service completed successfully.", connectionContext.TopicNames.Count);
}
private void CreateTopic(ServiceBusConnectionContext connectionContext, int topicIndex)
{
lock (connectionContext.TopicClientsLock)
{
if (connectionContext.IsDisposed)
{
return;
}
string topicName = connectionContext.TopicNames[topicIndex];
if (!_namespaceManager.TopicExists(topicName))
{
try
{
_trace.TraceInformation("Creating a new topic {0} in the service bus...", topicName);
_namespaceManager.CreateTopic(topicName);
_trace.TraceInformation("Creation of a new topic {0} in the service bus completed successfully.", topicName);
}
catch (MessagingEntityAlreadyExistsException)
{
// The entity already exists
_trace.TraceInformation("Creation of a new topic {0} threw an MessagingEntityAlreadyExistsException.", topicName);
}
}
// Create a client for this topic
TopicClient topicClient = TopicClient.CreateFromConnectionString(_connectionString, topicName);
if (_configuration.RetryPolicy != null)
{
topicClient.RetryPolicy = _configuration.RetryPolicy;
}
else
{
topicClient.RetryPolicy = RetryExponential.Default;
}
connectionContext.SetTopicClients(topicClient, topicIndex);
_trace.TraceInformation("Creation of a new topic client {0} completed successfully.", topicName);
}
CreateSubscription(connectionContext, topicIndex);
}
private void CreateSubscription(ServiceBusConnectionContext connectionContext, int topicIndex)
{
lock (connectionContext.SubscriptionsLock)
{
if (connectionContext.IsDisposed)
{
return;
}
string topicName = connectionContext.TopicNames[topicIndex];
// Create a random subscription
string subscriptionName = Guid.NewGuid().ToString();
try
{
var subscriptionDescription = new SubscriptionDescription(topicName, subscriptionName);
// This cleans up the subscription while if it's been idle for more than the timeout.
subscriptionDescription.AutoDeleteOnIdle = _idleSubscriptionTimeout;
_namespaceManager.CreateSubscription(subscriptionDescription);
_trace.TraceInformation("Creation of a new subscription {0} for topic {1} in the service bus completed successfully.", subscriptionName, topicName);
}
catch (MessagingEntityAlreadyExistsException)
{
// The entity already exists
_trace.TraceInformation("Creation of a new subscription {0} for topic {1} threw an MessagingEntityAlreadyExistsException.", subscriptionName, topicName);
}
// Create a receiver to get messages
string subscriptionEntityPath = SubscriptionClient.FormatSubscriptionPath(topicName, subscriptionName);
MessageReceiver receiver = _factory.CreateMessageReceiver(subscriptionEntityPath, ReceiveMode.ReceiveAndDelete);
_trace.TraceInformation("Creation of a message receive for subscription entity path {0} in the service bus completed successfully.", subscriptionEntityPath);
connectionContext.SetSubscriptionContext(new SubscriptionContext(topicName, subscriptionName, receiver), topicIndex);
var receiverContext = new ReceiverContext(topicIndex, receiver, connectionContext);
ProcessMessages(receiverContext);
// Open the stream
connectionContext.OpenStream(topicIndex);
}
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We retry to create the topics on exceptions")]
private void Retry(Action action)
{
string errorMessage = "Failed to create service bus subscription or topic : {0}";
while (true)
{
try
{
action();
break;
}
catch (UnauthorizedAccessException ex)
{
_trace.TraceError(errorMessage, ex.Message);
break;
}
catch (MessagingException ex)
{
_trace.TraceError(errorMessage, ex.Message);
if (ex.IsTransient)
{
Thread.Sleep(RetryDelay);
}
else
{
break;
}
}
catch (Exception ex)
{
_trace.TraceError(errorMessage, ex.Message);
Thread.Sleep(RetryDelay);
}
}
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// Close the factory
if (_factory != null)
{
_factory.Close();
}
}
}
public void Dispose()
{
Dispose(true);
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are handled through the error handler callback")]
private void ProcessMessages(ReceiverContext receiverContext)
{
receive:
try
{
IAsyncResult result = receiverContext.Receiver.BeginReceiveBatch(ReceiverContext.ReceiveBatchSize, receiverContext.ReceiveTimeout, ar =>
{
if (ar.CompletedSynchronously)
{
return;
}
var ctx = (ReceiverContext)ar.AsyncState;
if (ContinueReceiving(ar, ctx))
{
ProcessMessages(ctx);
}
},
receiverContext);
if (result.CompletedSynchronously)
{
if (ContinueReceiving(result, receiverContext))
{
goto receive;
}
}
}
catch (OperationCanceledException)
{
// This means the channel is closed
_trace.TraceError("OperationCanceledException was thrown in trying to receive the message from the service bus.");
return;
}
catch (Exception ex)
{
_trace.TraceError(ex.Message);
receiverContext.OnError(ex);
Thread.Sleep(RetryDelay);
goto receive;
}
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exceptions are handled through the error handler callback")]
private bool ContinueReceiving(IAsyncResult asyncResult, ReceiverContext receiverContext)
{
bool shouldContinue = true;
TimeSpan backoffAmount = BackoffAmount;
try
{
IEnumerable<BrokeredMessage> messages = receiverContext.Receiver.EndReceiveBatch(asyncResult);
receiverContext.OnMessage(messages);
// Reset the receive timeout if it changed
receiverContext.ReceiveTimeout = DefaultReadTimeout;
}
catch (ServerBusyException ex)
{
receiverContext.OnError(ex);
// Too busy so back off
shouldContinue = false;
}
catch (OperationCanceledException)
{
// This means the channel is closed
_trace.TraceError("Receiving messages from the service bus threw an OperationCanceledException, most likely due to a closed channel.");
return false;
}
catch (MessagingEntityNotFoundException ex)
{
receiverContext.Receiver.CloseAsync().Catch();
receiverContext.OnError(ex);
TaskAsyncHelper.Delay(RetryDelay)
.Then(() => Retry(() => CreateSubscription(receiverContext.ConnectionContext, receiverContext.TopicIndex)));
return false;
}
catch (Exception ex)
{
receiverContext.OnError(ex);
shouldContinue = false;
// TODO: Exponential backoff
backoffAmount = ErrorBackOffAmount;
// After an error, we want to adjust the timeout so that we
// can recover as quickly as possible even if there's no message
receiverContext.ReceiveTimeout = ErrorReadTimeout;
}
if (!shouldContinue)
{
TaskAsyncHelper.Delay(backoffAmount)
.Then(ctx => ProcessMessages(ctx), receiverContext);
return false;
}
return true;
}
private class ReceiverContext
{
public const int ReceiveBatchSize = DefaultReceiveBatchSize;
public readonly MessageReceiver Receiver;
public readonly ServiceBusConnectionContext ConnectionContext;
public int TopicIndex { get; private set; }
public TimeSpan ReceiveTimeout { get; set; }
public ReceiverContext(int topicIndex,
MessageReceiver receiver,
ServiceBusConnectionContext connectionContext)
{
TopicIndex = topicIndex;
Receiver = receiver;
ReceiveTimeout = DefaultReadTimeout;
ConnectionContext = connectionContext;
}
public void OnError(Exception ex)
{
ConnectionContext.ErrorHandler(TopicIndex, ex);
}
public void OnMessage(IEnumerable<BrokeredMessage> messages)
{
ConnectionContext.Handler(TopicIndex, messages);
}
}
}
}