| | 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 Primitives; |
| | 11 | |
|
| | 12 | | sealed class SessionReceivePump |
| | 13 | | { |
| | 14 | | readonly string clientId; |
| | 15 | | readonly ISessionClient client; |
| | 16 | | readonly Func<IMessageSession, Message, CancellationToken, Task> userOnSessionCallback; |
| | 17 | | readonly SessionHandlerOptions sessionHandlerOptions; |
| | 18 | | readonly string endpoint; |
| | 19 | | readonly string entityPath; |
| | 20 | | readonly CancellationToken pumpCancellationToken; |
| | 21 | | readonly SemaphoreSlim maxConcurrentSessionsSemaphoreSlim; |
| | 22 | | readonly SemaphoreSlim maxPendingAcceptSessionsSemaphoreSlim; |
| | 23 | | private readonly ServiceBusDiagnosticSource diagnosticSource; |
| | 24 | |
|
| 0 | 25 | | public SessionReceivePump(string clientId, |
| 0 | 26 | | ISessionClient client, |
| 0 | 27 | | ReceiveMode receiveMode, |
| 0 | 28 | | SessionHandlerOptions sessionHandlerOptions, |
| 0 | 29 | | Func<IMessageSession, Message, CancellationToken, Task> callback, |
| 0 | 30 | | Uri endpoint, |
| 0 | 31 | | CancellationToken token) |
| | 32 | | { |
| 0 | 33 | | this.client = client ?? throw new ArgumentException(nameof(client)); |
| 0 | 34 | | this.clientId = clientId; |
| 0 | 35 | | this.ReceiveMode = receiveMode; |
| 0 | 36 | | this.sessionHandlerOptions = sessionHandlerOptions; |
| 0 | 37 | | this.userOnSessionCallback = callback; |
| 0 | 38 | | this.endpoint = endpoint.Authority; |
| 0 | 39 | | this.entityPath = client.EntityPath; |
| 0 | 40 | | this.pumpCancellationToken = token; |
| 0 | 41 | | this.maxConcurrentSessionsSemaphoreSlim = new SemaphoreSlim(this.sessionHandlerOptions.MaxConcurrentSessions |
| 0 | 42 | | this.maxPendingAcceptSessionsSemaphoreSlim = new SemaphoreSlim(this.sessionHandlerOptions.MaxConcurrentAccep |
| 0 | 43 | | this.diagnosticSource = new ServiceBusDiagnosticSource(client.EntityPath, endpoint); |
| 0 | 44 | | } |
| | 45 | |
|
| 0 | 46 | | ReceiveMode ReceiveMode { get; } |
| | 47 | |
|
| | 48 | | public void StartPump() |
| | 49 | | { |
| | 50 | | // Schedule Tasks for doing PendingAcceptSession calls |
| 0 | 51 | | for (var i = 0; i < this.sessionHandlerOptions.MaxConcurrentAcceptSessionCalls; i++) |
| | 52 | | { |
| 0 | 53 | | TaskExtensionHelper.Schedule(this.SessionPumpTaskAsync); |
| | 54 | | } |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | static void CancelAutoRenewLock(object state) |
| | 58 | | { |
| 0 | 59 | | var renewCancellationTokenSource = (CancellationTokenSource)state; |
| | 60 | |
|
| | 61 | | try |
| | 62 | | { |
| 0 | 63 | | renewCancellationTokenSource.Cancel(); |
| 0 | 64 | | } |
| 0 | 65 | | catch (ObjectDisposedException) |
| | 66 | | { |
| | 67 | | // Ignore this race. |
| 0 | 68 | | } |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | bool ShouldRenewSessionLock() |
| | 72 | | { |
| 0 | 73 | | return |
| 0 | 74 | | this.ReceiveMode == ReceiveMode.PeekLock && |
| 0 | 75 | | this.sessionHandlerOptions.AutoRenewLock; |
| | 76 | | } |
| | 77 | |
|
| | 78 | | Task RaiseExceptionReceived(Exception e, string action) |
| | 79 | | { |
| 0 | 80 | | var eventArgs = new ExceptionReceivedEventArgs(e, action, this.endpoint, this.entityPath, this.clientId); |
| 0 | 81 | | return this.sessionHandlerOptions.RaiseExceptionReceived(eventArgs); |
| | 82 | | } |
| | 83 | |
|
| | 84 | | async Task CompleteMessageIfNeededAsync(IMessageSession session, Message message) |
| | 85 | | { |
| | 86 | | try |
| | 87 | | { |
| 0 | 88 | | if (this.ReceiveMode == ReceiveMode.PeekLock && |
| 0 | 89 | | this.sessionHandlerOptions.AutoComplete) |
| | 90 | | { |
| 0 | 91 | | await session.CompleteAsync(new[] { message.SystemProperties.LockToken }).ConfigureAwait(false); |
| | 92 | | } |
| 0 | 93 | | } |
| 0 | 94 | | catch (Exception exception) |
| | 95 | | { |
| 0 | 96 | | await this.RaiseExceptionReceived(exception, ExceptionReceivedEventArgsAction.Complete).ConfigureAwait(f |
| | 97 | | } |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | async Task AbandonMessageIfNeededAsync(IMessageSession session, Message message) |
| | 101 | | { |
| | 102 | | try |
| | 103 | | { |
| 0 | 104 | | if (session.ReceiveMode == ReceiveMode.PeekLock) |
| | 105 | | { |
| 0 | 106 | | await session.AbandonAsync(message.SystemProperties.LockToken).ConfigureAwait(false); |
| | 107 | | } |
| 0 | 108 | | } |
| 0 | 109 | | catch (Exception exception) |
| | 110 | | { |
| 0 | 111 | | await this.RaiseExceptionReceived(exception, ExceptionReceivedEventArgsAction.Abandon).ConfigureAwait(fa |
| | 112 | | } |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | async Task SessionPumpTaskAsync() |
| | 116 | | { |
| 0 | 117 | | while (!this.pumpCancellationToken.IsCancellationRequested) |
| | 118 | | { |
| 0 | 119 | | var concurrentSessionSemaphoreAcquired = false; |
| | 120 | | try |
| | 121 | | { |
| 0 | 122 | | await this.maxConcurrentSessionsSemaphoreSlim.WaitAsync(this.pumpCancellationToken).ConfigureAwait(f |
| 0 | 123 | | concurrentSessionSemaphoreAcquired = true; |
| | 124 | |
|
| 0 | 125 | | await this.maxPendingAcceptSessionsSemaphoreSlim.WaitAsync(this.pumpCancellationToken).ConfigureAwai |
| 0 | 126 | | var session = await this.client.AcceptMessageSessionAsync().ConfigureAwait(false); |
| 0 | 127 | | if (session == null) |
| | 128 | | { |
| 0 | 129 | | await Task.Delay(Constants.NoMessageBackoffTimeSpan, this.pumpCancellationToken).ConfigureAwait( |
| 0 | 130 | | continue; |
| | 131 | | } |
| | 132 | |
|
| | 133 | | // `session` needs to be copied to another local variable before passing to Schedule |
| | 134 | | // because of the way variables are captured. (Refer 'Captured variables') |
| 0 | 135 | | var messageSession = session; |
| 0 | 136 | | TaskExtensionHelper.Schedule(() => this.MessagePumpTaskAsync(messageSession)); |
| 0 | 137 | | } |
| 0 | 138 | | catch (Exception exception) |
| | 139 | | { |
| 0 | 140 | | MessagingEventSource.Log.SessionReceivePumpSessionReceiveException(this.clientId, exception); |
| | 141 | |
|
| 0 | 142 | | if (concurrentSessionSemaphoreAcquired) |
| | 143 | | { |
| 0 | 144 | | this.maxConcurrentSessionsSemaphoreSlim.Release(); |
| | 145 | | } |
| | 146 | |
|
| 0 | 147 | | if (exception is ServiceBusTimeoutException) |
| | 148 | | { |
| 0 | 149 | | await Task.Delay(Constants.NoMessageBackoffTimeSpan, this.pumpCancellationToken).ConfigureAwait( |
| | 150 | | } |
| | 151 | | else |
| | 152 | | { |
| 0 | 153 | | if (!(exception is ObjectDisposedException && this.pumpCancellationToken.IsCancellationRequested |
| | 154 | | { |
| 0 | 155 | | await this.RaiseExceptionReceived(exception, ExceptionReceivedEventArgsAction.AcceptMessageS |
| | 156 | | } |
| 0 | 157 | | if (!MessagingUtilities.ShouldRetry(exception)) |
| | 158 | | { |
| 0 | 159 | | await Task.Delay(Constants.NoMessageBackoffTimeSpan, this.pumpCancellationToken).ConfigureAw |
| | 160 | | } |
| | 161 | | } |
| 0 | 162 | | } |
| | 163 | | finally |
| | 164 | | { |
| 0 | 165 | | this.maxPendingAcceptSessionsSemaphoreSlim.Release(); |
| | 166 | | } |
| | 167 | | } |
| 0 | 168 | | } |
| | 169 | |
|
| | 170 | | async Task MessagePumpTaskAsync(IMessageSession session) |
| | 171 | | { |
| 0 | 172 | | if (session == null) |
| | 173 | | { |
| 0 | 174 | | return; |
| | 175 | | } |
| | 176 | |
|
| 0 | 177 | | var renewLockCancellationTokenSource = new CancellationTokenSource(); |
| 0 | 178 | | if (this.ShouldRenewSessionLock()) |
| | 179 | | { |
| 0 | 180 | | TaskExtensionHelper.Schedule(() => this.RenewSessionLockTaskAsync(session, renewLockCancellationTokenSou |
| | 181 | | } |
| | 182 | |
|
| 0 | 183 | | var autoRenewLockCancellationTimer = new Timer( |
| 0 | 184 | | CancelAutoRenewLock, |
| 0 | 185 | | renewLockCancellationTokenSource, |
| 0 | 186 | | Timeout.Infinite, |
| 0 | 187 | | Timeout.Infinite); |
| | 188 | |
|
| | 189 | | try |
| | 190 | | { |
| 0 | 191 | | while (!this.pumpCancellationToken.IsCancellationRequested && !session.IsClosedOrClosing) |
| | 192 | | { |
| | 193 | | Message message; |
| | 194 | | try |
| | 195 | | { |
| 0 | 196 | | message = await session.ReceiveAsync(this.sessionHandlerOptions.MessageWaitTimeout).ConfigureAwa |
| 0 | 197 | | } |
| 0 | 198 | | catch (Exception exception) |
| | 199 | | { |
| 0 | 200 | | MessagingEventSource.Log.MessageReceivePumpTaskException(this.clientId, session.SessionId, excep |
| 0 | 201 | | if (exception is ServiceBusTimeoutException) |
| | 202 | | { |
| | 203 | | // Timeout Exceptions are pretty common. Not alerting the User on this. |
| | 204 | | continue; |
| | 205 | | } |
| | 206 | |
|
| 0 | 207 | | if (!(exception is ObjectDisposedException && this.pumpCancellationToken.IsCancellationRequested |
| | 208 | | { |
| 0 | 209 | | await this.RaiseExceptionReceived(exception, ExceptionReceivedEventArgsAction.Receive).Confi |
| | 210 | | } |
| 0 | 211 | | break; |
| | 212 | | } |
| | 213 | |
|
| 0 | 214 | | if (message == null) |
| | 215 | | { |
| 0 | 216 | | MessagingEventSource.Log.SessionReceivePumpSessionEmpty(this.clientId, session.SessionId); |
| 0 | 217 | | break; |
| | 218 | | } |
| | 219 | |
|
| 0 | 220 | | bool isDiagnosticSourceEnabled = ServiceBusDiagnosticSource.IsEnabled(); |
| 0 | 221 | | Activity activity = isDiagnosticSourceEnabled ? this.diagnosticSource.ProcessSessionStart(session, m |
| 0 | 222 | | Task processTask = null; |
| | 223 | |
|
| | 224 | | try |
| | 225 | | { |
| | 226 | | // Set the timer |
| 0 | 227 | | autoRenewLockCancellationTimer.Change(this.sessionHandlerOptions.MaxAutoRenewDuration, |
| 0 | 228 | | TimeSpan.FromMilliseconds(-1)); |
| 0 | 229 | | var callbackExceptionOccurred = false; |
| | 230 | | try |
| | 231 | | { |
| 0 | 232 | | processTask = this.userOnSessionCallback(session, message, this.pumpCancellationToken); |
| 0 | 233 | | await processTask.ConfigureAwait(false); |
| 0 | 234 | | } |
| 0 | 235 | | catch (Exception exception) |
| | 236 | | { |
| 0 | 237 | | if (isDiagnosticSourceEnabled) |
| | 238 | | { |
| 0 | 239 | | this.diagnosticSource.ReportException(exception); |
| | 240 | | } |
| | 241 | |
|
| 0 | 242 | | MessagingEventSource.Log.MessageReceivePumpTaskException(this.clientId, session.SessionId, e |
| 0 | 243 | | await this.RaiseExceptionReceived(exception, ExceptionReceivedEventArgsAction.UserCallback). |
| 0 | 244 | | callbackExceptionOccurred = true; |
| 0 | 245 | | if (!(exception is MessageLockLostException || exception is SessionLockLostException)) |
| | 246 | | { |
| 0 | 247 | | await this.AbandonMessageIfNeededAsync(session, message).ConfigureAwait(false); |
| | 248 | | } |
| 0 | 249 | | } |
| | 250 | | finally |
| | 251 | | { |
| 0 | 252 | | autoRenewLockCancellationTimer.Change(Timeout.Infinite, Timeout.Infinite); |
| | 253 | | } |
| | 254 | |
|
| 0 | 255 | | if (!callbackExceptionOccurred) |
| | 256 | | { |
| 0 | 257 | | await this.CompleteMessageIfNeededAsync(session, message).ConfigureAwait(false); |
| | 258 | | } |
| 0 | 259 | | else if (session.IsClosedOrClosing) |
| | 260 | | { |
| | 261 | | // If User closed the session as part of the callback, break out of the loop |
| 0 | 262 | | break; |
| | 263 | | } |
| 0 | 264 | | } |
| | 265 | | finally |
| | 266 | | { |
| 0 | 267 | | this.diagnosticSource.ProcessSessionStop(activity, session, message, processTask?.Status); |
| | 268 | | } |
| 0 | 269 | | } |
| | 270 | | } |
| | 271 | | finally |
| | 272 | | { |
| 0 | 273 | | renewLockCancellationTokenSource.Cancel(); |
| 0 | 274 | | renewLockCancellationTokenSource.Dispose(); |
| 0 | 275 | | autoRenewLockCancellationTimer.Dispose(); |
| | 276 | |
|
| 0 | 277 | | await this.CloseSessionIfNeededAsync(session).ConfigureAwait(false); |
| 0 | 278 | | this.maxConcurrentSessionsSemaphoreSlim.Release(); |
| | 279 | | } |
| 0 | 280 | | } |
| | 281 | |
|
| | 282 | | async Task CloseSessionIfNeededAsync(IMessageSession session) |
| | 283 | | { |
| 0 | 284 | | if (!session.IsClosedOrClosing) |
| | 285 | | { |
| | 286 | | try |
| | 287 | | { |
| 0 | 288 | | await session.CloseAsync().ConfigureAwait(false); |
| 0 | 289 | | MessagingEventSource.Log.SessionReceivePumpSessionClosed(this.clientId, session.SessionId); |
| 0 | 290 | | } |
| 0 | 291 | | catch (Exception exception) |
| | 292 | | { |
| 0 | 293 | | MessagingEventSource.Log.SessionReceivePumpSessionCloseException(this.clientId, session.SessionId, e |
| 0 | 294 | | await this.RaiseExceptionReceived(exception, ExceptionReceivedEventArgsAction.CloseMessageSession).C |
| | 295 | | } |
| | 296 | | } |
| 0 | 297 | | } |
| | 298 | |
|
| | 299 | | async Task RenewSessionLockTaskAsync(IMessageSession session, CancellationToken renewLockCancellationToken) |
| | 300 | | { |
| 0 | 301 | | while (!this.pumpCancellationToken.IsCancellationRequested && |
| 0 | 302 | | !renewLockCancellationToken.IsCancellationRequested) |
| | 303 | | { |
| | 304 | | try |
| | 305 | | { |
| 0 | 306 | | var amount = MessagingUtilities.CalculateRenewAfterDuration(session.LockedUntilUtc); |
| | 307 | |
|
| 0 | 308 | | MessagingEventSource.Log.SessionReceivePumpSessionRenewLockStart(this.clientId, session.SessionId, a |
| 0 | 309 | | await Task.Delay(amount, renewLockCancellationToken).ConfigureAwait(false); |
| | 310 | |
|
| 0 | 311 | | if (!this.pumpCancellationToken.IsCancellationRequested && |
| 0 | 312 | | !renewLockCancellationToken.IsCancellationRequested) |
| | 313 | | { |
| 0 | 314 | | await session.RenewSessionLockAsync().ConfigureAwait(false); |
| 0 | 315 | | MessagingEventSource.Log.SessionReceivePumpSessionRenewLockStop(this.clientId, session.SessionId |
| | 316 | | } |
| | 317 | | else |
| | 318 | | { |
| 0 | 319 | | break; |
| | 320 | | } |
| 0 | 321 | | } |
| 0 | 322 | | catch (Exception exception) |
| | 323 | | { |
| 0 | 324 | | MessagingEventSource.Log.SessionReceivePumpSessionRenewLockException(this.clientId, session.SessionI |
| | 325 | |
|
| | 326 | | // TaskCanceled is expected here as renewTasks will be cancelled after the Complete call is made. |
| | 327 | | // ObjectDisposedException should only happen here because the CancellationToken was disposed at whi |
| | 328 | | // this renew exception is not relevant anymore. Lets not bother user with this exception. |
| 0 | 329 | | if (!(exception is TaskCanceledException) && !(exception is ObjectDisposedException)) |
| | 330 | | { |
| 0 | 331 | | await this.RaiseExceptionReceived(exception, ExceptionReceivedEventArgsAction.RenewLock).Configu |
| | 332 | | } |
| 0 | 333 | | if (!MessagingUtilities.ShouldRetry(exception)) |
| | 334 | | { |
| | 335 | | break; |
| | 336 | | } |
| 0 | 337 | | } |
| | 338 | | } |
| 0 | 339 | | } |
| | 340 | | } |
| | 341 | | } |