| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Diagnostics; |
| | 8 | | using System.Threading; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | | using Core; |
| | 11 | | using Primitives; |
| | 12 | |
|
| | 13 | | sealed class MessageReceivePump |
| | 14 | | { |
| | 15 | | readonly Func<Message, CancellationToken, Task> onMessageCallback; |
| | 16 | | readonly string endpoint; |
| | 17 | | readonly MessageHandlerOptions registerHandlerOptions; |
| | 18 | | readonly IMessageReceiver messageReceiver; |
| | 19 | | readonly CancellationToken pumpCancellationToken; |
| | 20 | | readonly SemaphoreSlim maxConcurrentCallsSemaphoreSlim; |
| | 21 | | readonly ServiceBusDiagnosticSource diagnosticSource; |
| | 22 | |
|
| 0 | 23 | | public MessageReceivePump(IMessageReceiver messageReceiver, |
| 0 | 24 | | MessageHandlerOptions registerHandlerOptions, |
| 0 | 25 | | Func<Message, CancellationToken, Task> callback, |
| 0 | 26 | | Uri endpoint, |
| 0 | 27 | | CancellationToken pumpCancellationToken) |
| | 28 | | { |
| 0 | 29 | | this.messageReceiver = messageReceiver ?? throw new ArgumentNullException(nameof(messageReceiver)); |
| 0 | 30 | | this.registerHandlerOptions = registerHandlerOptions; |
| 0 | 31 | | this.onMessageCallback = callback; |
| 0 | 32 | | this.endpoint = endpoint.Authority; |
| 0 | 33 | | this.pumpCancellationToken = pumpCancellationToken; |
| 0 | 34 | | this.maxConcurrentCallsSemaphoreSlim = new SemaphoreSlim(this.registerHandlerOptions.MaxConcurrentCalls); |
| 0 | 35 | | this.diagnosticSource = new ServiceBusDiagnosticSource(messageReceiver.Path, endpoint); |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | public void StartPump() |
| | 39 | | { |
| 0 | 40 | | TaskExtensionHelper.Schedule(() => this.MessagePumpTaskAsync()); |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | bool ShouldRenewLock() |
| | 44 | | { |
| 0 | 45 | | return |
| 0 | 46 | | this.messageReceiver.ReceiveMode == ReceiveMode.PeekLock && |
| 0 | 47 | | this.registerHandlerOptions.AutoRenewLock; |
| | 48 | | } |
| | 49 | |
|
| | 50 | | Task RaiseExceptionReceived(Exception e, string action) |
| | 51 | | { |
| 0 | 52 | | var eventArgs = new ExceptionReceivedEventArgs(e, action, this.endpoint, this.messageReceiver.Path, this.mes |
| 0 | 53 | | return this.registerHandlerOptions.RaiseExceptionReceived(eventArgs); |
| | 54 | | } |
| | 55 | |
|
| | 56 | | async Task MessagePumpTaskAsync() |
| | 57 | | { |
| 0 | 58 | | while (!this.pumpCancellationToken.IsCancellationRequested) |
| | 59 | | { |
| | 60 | | try |
| | 61 | | { |
| 0 | 62 | | await this.maxConcurrentCallsSemaphoreSlim.WaitAsync(this.pumpCancellationToken).ConfigureAwait(fals |
| | 63 | |
|
| 0 | 64 | | TaskExtensionHelper.Schedule(async () => |
| 0 | 65 | | { |
| 0 | 66 | | Message message = null; |
| 0 | 67 | | try |
| 0 | 68 | | { |
| 0 | 69 | | message = await this.messageReceiver.ReceiveAsync(this.registerHandlerOptions.ReceiveTimeOut |
| 0 | 70 | | if (message != null) |
| 0 | 71 | | { |
| 0 | 72 | | MessagingEventSource.Log.MessageReceiverPumpTaskStart(this.messageReceiver.ClientId, mes |
| 0 | 73 | |
|
| 0 | 74 | | TaskExtensionHelper.Schedule(() => |
| 0 | 75 | | { |
| 0 | 76 | | if (ServiceBusDiagnosticSource.IsEnabled()) |
| 0 | 77 | | { |
| 0 | 78 | | return this.MessageDispatchTaskInstrumented(message); |
| 0 | 79 | | } |
| 0 | 80 | | else |
| 0 | 81 | | { |
| 0 | 82 | | return this.MessageDispatchTask(message); |
| 0 | 83 | | } |
| 0 | 84 | | }); |
| 0 | 85 | | } |
| 0 | 86 | | } |
| 0 | 87 | | catch (OperationCanceledException) when (pumpCancellationToken.IsCancellationRequested) |
| 0 | 88 | | { |
| 0 | 89 | | // Ignore as we are stopping the pump |
| 0 | 90 | | } |
| 0 | 91 | | catch (ObjectDisposedException) when (pumpCancellationToken.IsCancellationRequested) |
| 0 | 92 | | { |
| 0 | 93 | | // Ignore as we are stopping the pump |
| 0 | 94 | | } |
| 0 | 95 | | catch (Exception exception) |
| 0 | 96 | | { |
| 0 | 97 | | MessagingEventSource.Log.MessageReceivePumpTaskException(this.messageReceiver.ClientId, stri |
| 0 | 98 | | await this.RaiseExceptionReceived(exception, ExceptionReceivedEventArgsAction.Receive).Confi |
| 0 | 99 | | } |
| 0 | 100 | | finally |
| 0 | 101 | | { |
| 0 | 102 | | // Either an exception or for some reason message was null, release semaphore and retry. |
| 0 | 103 | | if (message == null) |
| 0 | 104 | | { |
| 0 | 105 | | this.maxConcurrentCallsSemaphoreSlim.Release(); |
| 0 | 106 | | MessagingEventSource.Log.MessageReceiverPumpTaskStop(this.messageReceiver.ClientId, this |
| 0 | 107 | | } |
| 0 | 108 | | } |
| 0 | 109 | | }); |
| 0 | 110 | | } |
| 0 | 111 | | catch (OperationCanceledException) when (pumpCancellationToken.IsCancellationRequested) |
| | 112 | | { |
| | 113 | | // Ignore as we are stopping the pump |
| 0 | 114 | | } |
| 0 | 115 | | catch (ObjectDisposedException) when (pumpCancellationToken.IsCancellationRequested) |
| | 116 | | { |
| | 117 | | // Ignore as we are stopping the pump |
| 0 | 118 | | } |
| 0 | 119 | | catch (Exception exception) |
| | 120 | | { |
| 0 | 121 | | MessagingEventSource.Log.MessageReceivePumpTaskException(this.messageReceiver.ClientId, string.Empty |
| 0 | 122 | | await this.RaiseExceptionReceived(exception, ExceptionReceivedEventArgsAction.Receive).ConfigureAwai |
| | 123 | | } |
| | 124 | | } |
| 0 | 125 | | } |
| | 126 | |
|
| | 127 | | async Task MessageDispatchTaskInstrumented(Message message) |
| | 128 | | { |
| 0 | 129 | | Activity activity = this.diagnosticSource.ProcessStart(message); |
| 0 | 130 | | Task processTask = null; |
| | 131 | | try |
| | 132 | | { |
| 0 | 133 | | processTask = MessageDispatchTask(message); |
| 0 | 134 | | await processTask.ConfigureAwait(false); |
| 0 | 135 | | } |
| 0 | 136 | | catch (Exception e) |
| | 137 | | { |
| 0 | 138 | | this.diagnosticSource.ReportException(e); |
| 0 | 139 | | throw; |
| | 140 | | } |
| | 141 | | finally |
| | 142 | | { |
| 0 | 143 | | this.diagnosticSource.ProcessStop(activity, message, processTask?.Status); |
| | 144 | | } |
| 0 | 145 | | } |
| | 146 | |
|
| | 147 | | async Task MessageDispatchTask(Message message) |
| | 148 | | { |
| 0 | 149 | | CancellationTokenSource renewLockCancellationTokenSource = null; |
| 0 | 150 | | Timer autoRenewLockCancellationTimer = null; |
| | 151 | |
|
| 0 | 152 | | MessagingEventSource.Log.MessageReceiverPumpDispatchTaskStart(this.messageReceiver.ClientId, message); |
| | 153 | |
|
| 0 | 154 | | if (this.ShouldRenewLock()) |
| | 155 | | { |
| 0 | 156 | | renewLockCancellationTokenSource = new CancellationTokenSource(); |
| 0 | 157 | | TaskExtensionHelper.Schedule(() => this.RenewMessageLockTask(message, renewLockCancellationTokenSource.T |
| | 158 | |
|
| | 159 | | // After a threshold time of renewal('AutoRenewTimeout'), create timer to cancel anymore renewals. |
| 0 | 160 | | autoRenewLockCancellationTimer = new Timer(this.CancelAutoRenewLock, renewLockCancellationTokenSource, t |
| | 161 | | } |
| | 162 | |
|
| | 163 | | try |
| | 164 | | { |
| 0 | 165 | | MessagingEventSource.Log.MessageReceiverPumpUserCallbackStart(this.messageReceiver.ClientId, message); |
| 0 | 166 | | await this.onMessageCallback(message, this.pumpCancellationToken).ConfigureAwait(false); |
| | 167 | |
|
| 0 | 168 | | MessagingEventSource.Log.MessageReceiverPumpUserCallbackStop(this.messageReceiver.ClientId, message); |
| 0 | 169 | | } |
| 0 | 170 | | catch (Exception exception) |
| | 171 | | { |
| 0 | 172 | | MessagingEventSource.Log.MessageReceiverPumpUserCallbackException(this.messageReceiver.ClientId, message |
| 0 | 173 | | await this.RaiseExceptionReceived(exception, ExceptionReceivedEventArgsAction.UserCallback).ConfigureAwa |
| | 174 | |
|
| | 175 | | // Nothing much to do if UserCallback throws, Abandon message and Release semaphore. |
| 0 | 176 | | if (!(exception is MessageLockLostException)) |
| | 177 | | { |
| 0 | 178 | | await this.AbandonMessageIfNeededAsync(message).ConfigureAwait(false); |
| | 179 | | } |
| | 180 | |
|
| 0 | 181 | | if (ServiceBusDiagnosticSource.IsEnabled()) |
| | 182 | | { |
| 0 | 183 | | this.diagnosticSource.ReportException(exception); |
| | 184 | | } |
| | 185 | | // AbandonMessageIfNeededAsync should take care of not throwing exception |
| 0 | 186 | | this.maxConcurrentCallsSemaphoreSlim.Release(); |
| | 187 | |
|
| 0 | 188 | | return; |
| | 189 | | } |
| | 190 | | finally |
| | 191 | | { |
| 0 | 192 | | renewLockCancellationTokenSource?.Cancel(); |
| 0 | 193 | | renewLockCancellationTokenSource?.Dispose(); |
| 0 | 194 | | autoRenewLockCancellationTimer?.Dispose(); |
| | 195 | | } |
| | 196 | |
|
| | 197 | | // If we've made it this far, user callback completed fine. Complete message and Release semaphore. |
| 0 | 198 | | await this.CompleteMessageIfNeededAsync(message).ConfigureAwait(false); |
| 0 | 199 | | this.maxConcurrentCallsSemaphoreSlim.Release(); |
| | 200 | |
|
| 0 | 201 | | MessagingEventSource.Log.MessageReceiverPumpDispatchTaskStop(this.messageReceiver.ClientId, message, this.ma |
| 0 | 202 | | } |
| | 203 | |
|
| | 204 | | void CancelAutoRenewLock(object state) |
| | 205 | | { |
| 0 | 206 | | var renewLockCancellationTokenSource = (CancellationTokenSource)state; |
| | 207 | | try |
| | 208 | | { |
| 0 | 209 | | renewLockCancellationTokenSource.Cancel(); |
| 0 | 210 | | } |
| 0 | 211 | | catch (ObjectDisposedException) |
| | 212 | | { |
| | 213 | | // Ignore this race. |
| 0 | 214 | | } |
| 0 | 215 | | } |
| | 216 | |
|
| | 217 | | async Task AbandonMessageIfNeededAsync(Message message) |
| | 218 | | { |
| | 219 | | try |
| | 220 | | { |
| 0 | 221 | | if (this.messageReceiver.ReceiveMode == ReceiveMode.PeekLock) |
| | 222 | | { |
| 0 | 223 | | await this.messageReceiver.AbandonAsync(message.SystemProperties.LockToken).ConfigureAwait(false); |
| | 224 | | } |
| 0 | 225 | | } |
| 0 | 226 | | catch (Exception exception) |
| | 227 | | { |
| 0 | 228 | | await this.RaiseExceptionReceived(exception, ExceptionReceivedEventArgsAction.Abandon).ConfigureAwait(fa |
| | 229 | | } |
| 0 | 230 | | } |
| | 231 | |
|
| | 232 | | async Task CompleteMessageIfNeededAsync(Message message) |
| | 233 | | { |
| | 234 | | try |
| | 235 | | { |
| 0 | 236 | | if (this.messageReceiver.ReceiveMode == ReceiveMode.PeekLock && |
| 0 | 237 | | this.registerHandlerOptions.AutoComplete) |
| | 238 | | { |
| 0 | 239 | | await this.messageReceiver.CompleteAsync(new[] { message.SystemProperties.LockToken }).ConfigureAwai |
| | 240 | | } |
| 0 | 241 | | } |
| 0 | 242 | | catch (Exception exception) |
| | 243 | | { |
| 0 | 244 | | await this.RaiseExceptionReceived(exception, ExceptionReceivedEventArgsAction.Complete).ConfigureAwait(f |
| | 245 | | } |
| 0 | 246 | | } |
| | 247 | |
|
| | 248 | | async Task RenewMessageLockTask(Message message, CancellationToken renewLockCancellationToken) |
| | 249 | | { |
| 0 | 250 | | while (!this.pumpCancellationToken.IsCancellationRequested && |
| 0 | 251 | | !renewLockCancellationToken.IsCancellationRequested) |
| | 252 | | { |
| | 253 | | try |
| | 254 | | { |
| 0 | 255 | | var amount = MessagingUtilities.CalculateRenewAfterDuration(message.SystemProperties.LockedUntilUtc) |
| 0 | 256 | | MessagingEventSource.Log.MessageReceiverPumpRenewMessageStart(this.messageReceiver.ClientId, message |
| | 257 | |
|
| | 258 | | // We're awaiting the task created by 'ContinueWith' to avoid awaiting the Delay task which may be c |
| | 259 | | // by the renewLockCancellationToken. This way we prevent a TaskCanceledException. |
| 0 | 260 | | var delayTask = await Task.Delay(amount, renewLockCancellationToken) |
| 0 | 261 | | .ContinueWith(t => t, TaskContinuationOptions.ExecuteSynchronously) |
| 0 | 262 | | .ConfigureAwait(false); |
| 0 | 263 | | if (delayTask.IsCanceled) |
| | 264 | | { |
| 0 | 265 | | break; |
| | 266 | | } |
| | 267 | |
|
| 0 | 268 | | if (!this.pumpCancellationToken.IsCancellationRequested && |
| 0 | 269 | | !renewLockCancellationToken.IsCancellationRequested) |
| | 270 | | { |
| 0 | 271 | | await this.messageReceiver.RenewLockAsync(message).ConfigureAwait(false); |
| 0 | 272 | | MessagingEventSource.Log.MessageReceiverPumpRenewMessageStop(this.messageReceiver.ClientId, mess |
| | 273 | | } |
| | 274 | | else |
| | 275 | | { |
| 0 | 276 | | break; |
| | 277 | | } |
| 0 | 278 | | } |
| 0 | 279 | | catch (Exception exception) |
| | 280 | | { |
| 0 | 281 | | MessagingEventSource.Log.MessageReceiverPumpRenewMessageException(this.messageReceiver.ClientId, mes |
| | 282 | |
|
| | 283 | | // ObjectDisposedException should only happen here because the CancellationToken was disposed at whi |
| | 284 | | // this renew exception is not relevant anymore. Lets not bother user with this exception. |
| 0 | 285 | | if (!(exception is ObjectDisposedException)) |
| | 286 | | { |
| 0 | 287 | | await this.RaiseExceptionReceived(exception, ExceptionReceivedEventArgsAction.RenewLock).Configu |
| | 288 | | } |
| | 289 | |
|
| 0 | 290 | | if (!MessagingUtilities.ShouldRetry(exception)) |
| | 291 | | { |
| | 292 | | break; |
| | 293 | | } |
| 0 | 294 | | } |
| | 295 | | } |
| 0 | 296 | | } |
| | 297 | | } |
| | 298 | | } |