-
Notifications
You must be signed in to change notification settings - Fork 60
/
MessagePump.cs
487 lines (416 loc) · 19.9 KB
/
MessagePump.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
namespace NServiceBus.Transport.RabbitMQ
{
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Extensibility;
using global::RabbitMQ.Client;
using global::RabbitMQ.Client.Events;
using global::RabbitMQ.Client.Exceptions;
using Logging;
sealed class MessagePump : IMessageReceiver, IDisposable
{
static readonly ILog Logger = LogManager.GetLogger(typeof(MessagePump));
static readonly TransportTransaction TransportTransaction = new TransportTransaction();
readonly ReceiveSettings settings;
readonly ConnectionFactory connectionFactory;
readonly MessageConverter messageConverter;
readonly string consumerTag;
readonly ChannelProvider channelProvider;
readonly TimeSpan timeToWaitBeforeTriggeringCircuitBreaker;
readonly QueuePurger queuePurger;
readonly PrefetchCountCalculation prefetchCountCalculation;
readonly Action<string, Exception, CancellationToken> criticalErrorAction;
readonly TimeSpan retryDelay;
readonly string name;
bool disposed;
OnMessage onMessage;
OnError onError;
int maxConcurrency;
long numberOfMessagesBeingProcessed;
CancellationTokenSource messagePumpCancellationTokenSource;
CancellationTokenSource messageProcessingCancellationTokenSource;
MessagePumpConnectionFailedCircuitBreaker circuitBreaker;
IConnection connection;
// Stop
TaskCompletionSource<bool> connectionShutdownCompleted;
public MessagePump(
ReceiveSettings settings,
ConnectionFactory connectionFactory,
IRoutingTopology routingTopology,
MessageConverter messageConverter,
string consumerTag,
ChannelProvider channelProvider,
TimeSpan timeToWaitBeforeTriggeringCircuitBreaker,
PrefetchCountCalculation prefetchCountCalculation,
Action<string, Exception,
CancellationToken> criticalErrorAction,
TimeSpan retryDelay)
{
this.settings = settings;
this.connectionFactory = connectionFactory;
this.messageConverter = messageConverter;
this.consumerTag = consumerTag;
this.channelProvider = channelProvider;
this.timeToWaitBeforeTriggeringCircuitBreaker = timeToWaitBeforeTriggeringCircuitBreaker;
this.prefetchCountCalculation = prefetchCountCalculation;
this.criticalErrorAction = criticalErrorAction;
this.retryDelay = retryDelay;
ReceiveAddress = RabbitMQTransportInfrastructure.TranslateAddress(settings.ReceiveAddress);
if (settings.UsePublishSubscribe)
{
Subscriptions = new SubscriptionManager(connectionFactory, routingTopology, ReceiveAddress);
}
queuePurger = new QueuePurger(connectionFactory);
name = $"{ReceiveAddress} MessagePump";
}
public ISubscriptionManager Subscriptions { get; }
public string Id => settings.Id;
public string ReceiveAddress { get; }
public Task Initialize(PushRuntimeSettings limitations, OnMessage onMessage, OnError onError, CancellationToken cancellationToken = default)
{
this.onMessage = onMessage;
this.onError = onError;
maxConcurrency = limitations.MaxConcurrency;
if (settings.PurgeOnStartup)
{
queuePurger.Purge(ReceiveAddress);
}
return Task.CompletedTask;
}
public Task StartReceive(CancellationToken cancellationToken = default)
{
messagePumpCancellationTokenSource = new CancellationTokenSource();
messageProcessingCancellationTokenSource = new CancellationTokenSource();
circuitBreaker = new MessagePumpConnectionFailedCircuitBreaker(
name,
timeToWaitBeforeTriggeringCircuitBreaker,
(message, exception) => criticalErrorAction(message, exception, messageProcessingCancellationTokenSource.Token));
ConnectToBroker();
return Task.CompletedTask;
}
public Task ChangeConcurrency(PushRuntimeSettings limitations, CancellationToken cancellationToken = default)
{
maxConcurrency = limitations.MaxConcurrency;
Logger.InfoFormat("Calling a change concurrency and reconnecting with new value {0}.", limitations.MaxConcurrency);
_ = Task.Run(() => Reconnect(), cancellationToken);
return Task.CompletedTask;
}
void ConnectToBroker()
{
connection = connectionFactory.CreateConnection(name, false, maxConcurrency);
connection.ConnectionShutdown += Connection_ConnectionShutdown;
var prefetchCount = prefetchCountCalculation(maxConcurrency);
if (prefetchCount < maxConcurrency)
{
Logger.Warn($"The specified prefetch count '{prefetchCount}' is smaller than the specified maximum concurrency '{maxConcurrency}'. The maximum concurrency value will be used as the prefetch count instead.");
prefetchCount = maxConcurrency;
}
var channel = connection.CreateModel();
channel.ModelShutdown += Channel_ModelShutdown;
channel.BasicQos(0, (ushort)Math.Min(prefetchCount, ushort.MaxValue), false);
var consumer = new AsyncEventingBasicConsumer(channel);
consumer.ConsumerCancelled += Consumer_ConsumerCancelled;
consumer.Registered += Consumer_Registered;
consumer.Received += Consumer_Received;
channel.BasicConsume(ReceiveAddress, false, consumerTag, consumer);
}
public async Task StopReceive(CancellationToken cancellationToken = default)
{
messagePumpCancellationTokenSource?.Cancel();
using (cancellationToken.Register(() => messageProcessingCancellationTokenSource?.Cancel()))
{
while (Interlocked.Read(ref numberOfMessagesBeingProcessed) > 0)
{
// We are deliberately not forwarding the cancellation token here because
// this loop is our way of waiting for all pending messaging operations
// to participate in cooperative cancellation or not.
// We do not want to rudely abort them because the cancellation token has been canceled.
// This allows us to preserve the same behaviour in v8 as in v7 in that,
// if CancellationToken.None is passed to this method,
// the method will only return when all in flight messages have been processed.
// If, on the other hand, a non-default CancellationToken is passed,
// all message processing operations have the opportunity to
// participate in cooperative cancellation.
// If we ever require a method of stopping the endpoint such that
// all message processing is canceled immediately,
// we can provide that as a separate feature.
await Task.Delay(50, CancellationToken.None).ConfigureAwait(false);
}
connectionShutdownCompleted = new TaskCompletionSource<bool>();
if (connection.IsOpen)
{
connection.Close();
}
else
{
connectionShutdownCompleted.SetResult(true);
}
await connectionShutdownCompleted.Task.ConfigureAwait(false);
}
}
#pragma warning disable PS0018 // A task-returning method should have a CancellationToken parameter unless it has a parameter implementing ICancellableContext
Task Consumer_Registered(object sender, ConsumerEventArgs e)
#pragma warning restore PS0018 // A task-returning method should have a CancellationToken parameter unless it has a parameter implementing ICancellableContext
{
circuitBreaker.Success();
return Task.CompletedTask;
}
void Connection_ConnectionShutdown(object sender, ShutdownEventArgs e)
{
if (e.Initiator == ShutdownInitiator.Application && e.ReplyCode == 200)
{
connectionShutdownCompleted?.TrySetResult(true);
}
else if (circuitBreaker.Disarmed)
{
//log entry handled by event handler registered in ConnectionFactory
circuitBreaker.Failure(new Exception(e.ToString()));
_ = Task.Run(() => Reconnect());
}
else
{
Logger.WarnFormat("'{0}' connection shutdown while reconnect already in progress: {1}", name, e);
}
}
void Channel_ModelShutdown(object sender, ShutdownEventArgs e)
{
if (e.Initiator == ShutdownInitiator.Application)
{
return;
}
if (e.Initiator == ShutdownInitiator.Peer && e.ReplyCode == 404)
{
return;
}
if (circuitBreaker.Disarmed)
{
Logger.WarnFormat("'{0}' channel shutdown: {1}", name, e);
circuitBreaker.Failure(new Exception(e.ToString()));
_ = Task.Run(() => Reconnect());
}
else
{
Logger.WarnFormat("'{0}' channel shutdown while reconnect already in progress: {1}", name, e);
}
}
#pragma warning disable PS0018 // A task-returning method should have a CancellationToken parameter unless it has a parameter implementing ICancellableContext
Task Consumer_ConsumerCancelled(object sender, ConsumerEventArgs e)
#pragma warning restore PS0018 // A task-returning method should have a CancellationToken parameter unless it has a parameter implementing ICancellableContext
{
var consumer = (AsyncEventingBasicConsumer)sender;
if (consumer.Model.IsOpen && connection.IsOpen)
{
if (circuitBreaker.Disarmed)
{
Logger.WarnFormat("'{0}' consumer canceled by broker", name);
circuitBreaker.Failure(new Exception($"'{name}' consumer canceled by broker"));
_ = Task.Run(() => Reconnect());
}
else
{
Logger.WarnFormat("'{0}' consumer canceled by broker while reconnect already in progress", name);
}
}
return Task.CompletedTask;
}
#pragma warning disable PS0018 // A task-returning method should have a CancellationToken parameter unless it has a parameter implementing ICancellableContext
async Task Reconnect()
#pragma warning restore PS0018 // A task-returning method should have a CancellationToken parameter unless it has a parameter implementing ICancellableContext
{
try
{
var oldConnection = connection;
while (true)
{
Logger.InfoFormat("'{0}': Attempting to reconnect in {1} seconds.", name, retryDelay.TotalSeconds);
await Task.Delay(retryDelay, messageProcessingCancellationTokenSource.Token).ConfigureAwait(false);
try
{
ConnectToBroker();
break;
}
catch (Exception ex)
{
Logger.InfoFormat("'{0}': Reconnecting to the broker failed: {1}", name, ex);
}
}
Logger.InfoFormat("'{0}': Connection to the broker reestablished successfully.", name);
if (oldConnection.IsOpen)
{
oldConnection.Close();
oldConnection.Dispose();
}
}
catch (Exception ex) when (ex.IsCausedBy(messageProcessingCancellationTokenSource.Token))
{
Logger.DebugFormat("'{0}': Reconnection canceled since the transport is being stopped: {1}", name, ex);
}
catch (Exception ex)
{
Logger.WarnFormat("'{0}': Unexpected error while reconnecting: '{1}'", name, ex);
}
}
#pragma warning disable PS0018 // A task-returning method should have a CancellationToken parameter unless it has a parameter implementing ICancellableContext
async Task Consumer_Received(object sender, BasicDeliverEventArgs eventArgs)
#pragma warning restore PS0018 // A task-returning method should have a CancellationToken parameter unless it has a parameter implementing ICancellableContext
{
if (messagePumpCancellationTokenSource.IsCancellationRequested)
{
return;
}
var consumer = (AsyncEventingBasicConsumer)sender;
await ProcessAndSwallowExceptions(consumer, eventArgs, messageProcessingCancellationTokenSource.Token).ConfigureAwait(false);
}
async Task ProcessAndSwallowExceptions(AsyncEventingBasicConsumer consumer, BasicDeliverEventArgs message, CancellationToken messageProcessingCancellationToken)
{
Interlocked.Increment(ref numberOfMessagesBeingProcessed);
try
{
try
{
await Process(consumer, message, messageProcessingCancellationToken).ConfigureAwait(false);
}
#pragma warning disable PS0019 // Do not catch Exception without considering OperationCanceledException - handling is the same for OCE
catch (Exception ex)
#pragma warning restore PS0019 // Do not catch Exception without considering OperationCanceledException
{
Logger.Debug("Returning message to queue...", ex);
consumer.Model.BasicRejectAndRequeueIfOpen(message.DeliveryTag);
throw;
}
}
catch (Exception ex) when (ex.IsCausedBy(messageProcessingCancellationToken))
{
Logger.Debug("Message processing canceled.", ex);
}
catch (Exception ex)
{
Logger.Error("Message processing failed.", ex);
}
finally
{
Interlocked.Decrement(ref numberOfMessagesBeingProcessed);
}
}
async Task Process(AsyncEventingBasicConsumer consumer, BasicDeliverEventArgs message, CancellationToken messageProcessingCancellationToken)
{
Dictionary<string, string> headers;
try
{
headers = messageConverter.RetrieveHeaders(message);
}
catch (Exception ex)
{
Logger.Error(
$"Failed to retrieve headers from poison message. Moving message to queue '{settings.ErrorQueue}'...",
ex);
await MovePoisonMessage(consumer, message, settings.ErrorQueue, messageProcessingCancellationToken).ConfigureAwait(false);
return;
}
string messageId;
try
{
messageId = messageConverter.RetrieveMessageId(message, headers);
}
catch (Exception ex)
{
Logger.Error(
$"Failed to retrieve ID from poison message. Moving message to queue '{settings.ErrorQueue}'...",
ex);
await MovePoisonMessage(consumer, message, settings.ErrorQueue, messageProcessingCancellationToken).ConfigureAwait(false);
return;
}
var processed = false;
var errorHandled = false;
var numberOfDeliveryAttempts = 0;
while (!processed && !errorHandled)
{
var processingContext = new ContextBag();
processingContext.Set(message);
try
{
var messageContext = new MessageContext(messageId, headers, message.Body, TransportTransaction, ReceiveAddress, processingContext);
await onMessage(messageContext, messageProcessingCancellationToken).ConfigureAwait(false);
processed = true;
}
catch (Exception ex) when (!ex.IsCausedBy(messageProcessingCancellationToken))
{
++numberOfDeliveryAttempts;
headers = messageConverter.RetrieveHeaders(message);
var errorContext = new ErrorContext(ex, headers, messageId, message.Body, TransportTransaction, numberOfDeliveryAttempts, ReceiveAddress, processingContext);
try
{
errorHandled =
await onError(errorContext, messageProcessingCancellationToken).ConfigureAwait(false) ==
ErrorHandleResult.Handled;
if (!errorHandled)
{
headers = messageConverter.RetrieveHeaders(message);
}
}
catch (Exception onErrorEx) when (!onErrorEx.IsCausedBy(messageProcessingCancellationToken))
{
criticalErrorAction(
$"Failed to execute recoverability policy for message with native ID: `{messageId}`", onErrorEx,
messageProcessingCancellationToken);
consumer.Model.BasicRejectAndRequeueIfOpen(message.DeliveryTag);
return;
}
}
}
try
{
consumer.Model.BasicAckSingle(message.DeliveryTag);
}
catch (AlreadyClosedException ex)
{
Logger.Warn(
$"Failed to acknowledge message '{messageId}' because the channel was closed. The message was returned to the queue.",
ex);
}
}
async Task MovePoisonMessage(AsyncEventingBasicConsumer consumer, BasicDeliverEventArgs message, string queue, CancellationToken messageProcessingCancellationToken)
{
try
{
var channel = channelProvider.GetPublishChannel();
try
{
await channel.RawSendInCaseOfFailure(queue, message.Body, message.BasicProperties, messageProcessingCancellationToken).ConfigureAwait(false);
}
finally
{
channelProvider.ReturnPublishChannel(channel);
}
}
catch (Exception ex) when (!ex.IsCausedBy(messageProcessingCancellationToken))
{
Logger.Error($"Failed to move poison message to queue '{queue}'. Returning message to original queue...", ex);
consumer.Model.BasicRejectAndRequeueIfOpen(message.DeliveryTag);
return;
}
try
{
consumer.Model.BasicAckSingle(message.DeliveryTag);
}
catch (AlreadyClosedException ex)
{
Logger.Warn($"Failed to acknowledge poison message because the channel was closed. The message was sent to queue '{queue}' but also returned to the original queue.", ex);
}
}
public void Dispose()
{
if (disposed)
{
return;
}
circuitBreaker?.Dispose();
messagePumpCancellationTokenSource?.Dispose();
messageProcessingCancellationTokenSource?.Dispose();
connection?.Dispose();
disposed = true;
}
}
}