| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.ComponentModel; |
| | 6 | | using System.Runtime.ExceptionServices; |
| | 7 | | using System.Threading; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using Azure.Messaging.ServiceBus.Amqp; |
| | 10 | | using Azure.Messaging.ServiceBus.Core; |
| | 11 | | using Azure.Messaging.ServiceBus.Diagnostics; |
| | 12 | |
|
| | 13 | | namespace Azure.Messaging.ServiceBus |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// An abstract representation of a policy to govern retrying of messaging operations. |
| | 17 | | /// </summary> |
| | 18 | | /// |
| | 19 | | /// <remarks> |
| | 20 | | /// It is recommended that developers without advanced needs not implement custom retry |
| | 21 | | /// policies but instead configure the default policy by specifying the desired set of |
| | 22 | | /// retry options when creating one of the Service Bus clients. |
| | 23 | | /// </remarks> |
| | 24 | | /// |
| | 25 | | /// <seealso cref="ServiceBusRetryOptions"/> |
| | 26 | | /// |
| | 27 | | public abstract class ServiceBusRetryPolicy |
| | 28 | | { |
| | 29 | |
|
| 0 | 30 | | private static readonly TimeSpan ServerBusyBaseSleepTime = TimeSpan.FromSeconds(10); |
| | 31 | |
|
| 116 | 32 | | private readonly object serverBusyLock = new object(); |
| | 33 | |
|
| | 34 | | // This is a volatile copy of IsServerBusy. IsServerBusy is synchronized with a lock, whereas encounteredServerB |
| | 35 | | private volatile bool encounteredServerBusy; |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Determines whether or not the server returned a busy error. |
| | 39 | | /// </summary> |
| 0 | 40 | | private bool IsServerBusy { get; set; } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Gets the exception message when a server busy error is returned. |
| | 44 | | /// </summary> |
| 0 | 45 | | private string ServerBusyExceptionMessage { get; set; } |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// The instance of <see cref="ServiceBusEventSource" /> which can be mocked for testing. |
| | 49 | | /// </summary> |
| | 50 | | /// |
| 138 | 51 | | internal ServiceBusEventSource Logger { get; set; } = ServiceBusEventSource.Log; |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Calculates the amount of time to allow the current attempt for an operation to |
| | 55 | | /// complete before considering it to be timed out. |
| | 56 | | /// </summary> |
| | 57 | | /// |
| | 58 | | /// <param name="attemptCount">The number of total attempts that have been made, including the initial attempt b |
| | 59 | | /// |
| | 60 | | /// <returns>The amount of time to allow for an operation to complete.</returns> |
| | 61 | | /// |
| | 62 | | public abstract TimeSpan CalculateTryTimeout(int attemptCount); |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Calculates the amount of time to wait before another attempt should be made. |
| | 66 | | /// </summary> |
| | 67 | | /// |
| | 68 | | /// <param name="lastException">The last exception that was observed for the operation to be retried.</param> |
| | 69 | | /// <param name="attemptCount">The number of total attempts that have been made, including the initial attempt b |
| | 70 | | /// |
| | 71 | | /// <returns>The amount of time to delay before retrying the associated operation; if <c>null</c>, then the oper |
| | 72 | | /// |
| | 73 | | public abstract TimeSpan? CalculateRetryDelay( |
| | 74 | | Exception lastException, |
| | 75 | | int attemptCount); |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Determines whether the specified <see cref="System.Object" /> is equal to this instance. |
| | 79 | | /// </summary> |
| | 80 | | /// |
| | 81 | | /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param> |
| | 82 | | /// |
| | 83 | | /// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c> |
| | 84 | | /// |
| | 85 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 86 | | public override bool Equals(object obj) => base.Equals(obj); |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Returns a hash code for this instance. |
| | 90 | | /// </summary> |
| | 91 | | /// |
| | 92 | | /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a ha |
| | 93 | | /// |
| | 94 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 95 | | public override int GetHashCode() => base.GetHashCode(); |
| | 96 | |
|
| | 97 | | /// <summary> |
| | 98 | | /// Converts the instance to string representation. |
| | 99 | | /// </summary> |
| | 100 | | /// |
| | 101 | | /// <returns>A <see cref="System.String" /> that represents this instance.</returns> |
| | 102 | | /// |
| | 103 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 104 | | public override string ToString() => base.ToString(); |
| | 105 | |
|
| | 106 | | /// <summary> |
| | 107 | | /// |
| | 108 | | /// </summary> |
| | 109 | | /// <param name="operation"></param> |
| | 110 | | /// <param name="scope"></param> |
| | 111 | | /// <param name="cancellationToken"></param> |
| | 112 | | /// <returns></returns> |
| | 113 | | internal async Task RunOperation( |
| | 114 | | Func<TimeSpan, Task> operation, |
| | 115 | | TransportConnectionScope scope, |
| | 116 | | CancellationToken cancellationToken) |
| | 117 | | { |
| 20 | 118 | | var failedAttemptCount = 0; |
| | 119 | |
|
| | 120 | |
|
| 20 | 121 | | TimeSpan tryTimeout = CalculateTryTimeout(0); |
| 20 | 122 | | if (IsServerBusy && tryTimeout < ServerBusyBaseSleepTime) |
| | 123 | | { |
| | 124 | | // We are in a server busy state before we start processing. |
| | 125 | | // Since ServerBusyBaseSleepTime > remaining time for the operation, we don't wait for the entire Sleep |
| 0 | 126 | | await Task.Delay(tryTimeout, cancellationToken).ConfigureAwait(false); |
| 0 | 127 | | throw new ServiceBusException( |
| 0 | 128 | | ServerBusyExceptionMessage, |
| 0 | 129 | | ServiceBusFailureReason.ServiceBusy); |
| | 130 | | } |
| 38 | 131 | | while (!cancellationToken.IsCancellationRequested) |
| | 132 | | { |
| 36 | 133 | | if (IsServerBusy) |
| | 134 | | { |
| 0 | 135 | | await Task.Delay(ServerBusyBaseSleepTime, cancellationToken).ConfigureAwait(false); |
| | 136 | | } |
| | 137 | |
|
| | 138 | | try |
| | 139 | | { |
| 36 | 140 | | await operation(tryTimeout).ConfigureAwait(false); |
| 0 | 141 | | return; |
| | 142 | | } |
| | 143 | |
|
| | 144 | | catch (Exception ex) |
| | 145 | | { |
| 36 | 146 | | Exception activeEx = AmqpExceptionHelper.TranslateException(ex); |
| | 147 | |
|
| | 148 | | // Determine if there should be a retry for the next attempt; if so enforce the delay but do not qui |
| | 149 | | // Otherwise, throw the translated exception. |
| | 150 | |
|
| 36 | 151 | | ++failedAttemptCount; |
| 36 | 152 | | TimeSpan? retryDelay = CalculateRetryDelay(activeEx, failedAttemptCount); |
| 36 | 153 | | if (retryDelay.HasValue && !scope.IsDisposed && !cancellationToken.IsCancellationRequested) |
| | 154 | | { |
| 18 | 155 | | Logger.RunOperationExceptionEncountered(activeEx.ToString()); |
| 18 | 156 | | await Task.Delay(retryDelay.Value, cancellationToken).ConfigureAwait(false); |
| 18 | 157 | | tryTimeout = CalculateTryTimeout(failedAttemptCount); |
| | 158 | | } |
| | 159 | | else |
| | 160 | | { |
| 18 | 161 | | ExceptionDispatchInfo.Capture(activeEx) |
| 18 | 162 | | .Throw(); |
| | 163 | | } |
| | 164 | | } |
| | 165 | | } |
| | 166 | | // If no value has been returned nor exception thrown by this point, |
| | 167 | | // then cancellation has been requested. |
| 2 | 168 | | throw new TaskCanceledException(); |
| 0 | 169 | | } |
| | 170 | |
|
| | 171 | | internal void SetServerBusy(string exceptionMessage) |
| | 172 | | { |
| | 173 | | // multiple call to this method will not prolong the timer. |
| 0 | 174 | | if (encounteredServerBusy) |
| | 175 | | { |
| 0 | 176 | | return; |
| | 177 | | } |
| | 178 | |
|
| 0 | 179 | | lock (serverBusyLock) |
| | 180 | | { |
| 0 | 181 | | if (!encounteredServerBusy) |
| | 182 | | { |
| 0 | 183 | | encounteredServerBusy = true; |
| 0 | 184 | | ServerBusyExceptionMessage = string.IsNullOrWhiteSpace(exceptionMessage) ? |
| 0 | 185 | | Resources.DefaultServerBusyException : exceptionMessage; |
| 0 | 186 | | IsServerBusy = true; |
| 0 | 187 | | _ = ScheduleResetServerBusy(); |
| | 188 | | } |
| 0 | 189 | | } |
| 0 | 190 | | } |
| | 191 | |
|
| | 192 | | internal void ResetServerBusy() |
| | 193 | | { |
| 0 | 194 | | if (!encounteredServerBusy) |
| | 195 | | { |
| 0 | 196 | | return; |
| | 197 | | } |
| | 198 | |
|
| 0 | 199 | | lock (serverBusyLock) |
| | 200 | | { |
| 0 | 201 | | if (encounteredServerBusy) |
| | 202 | | { |
| 0 | 203 | | encounteredServerBusy = false; |
| 0 | 204 | | ServerBusyExceptionMessage = Resources.DefaultServerBusyException; |
| 0 | 205 | | IsServerBusy = false; |
| | 206 | | } |
| 0 | 207 | | } |
| 0 | 208 | | } |
| | 209 | |
|
| | 210 | | private async Task ScheduleResetServerBusy() |
| | 211 | | { |
| 0 | 212 | | await Task.Delay(ServerBusyBaseSleepTime).ConfigureAwait(false); |
| 0 | 213 | | ResetServerBusy(); |
| 0 | 214 | | } |
| | 215 | | } |
| | 216 | | } |