/// <summary> /// Repetitively executes the specified <paramref name="faultSensitiveMethod"/> until the operation is successful, the amount of retry attempts has been reached, or a failed operation is not considered related to transient fault condition. /// </summary> /// <typeparam name="T1">The type of the first parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="T2">The type of the second parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="T3">The type of the third parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="T4">The type of the fourth parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="T5">The type of the fifth parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="TResult">The type of the out result value of the function delegate encapsulates <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="TSuccess">The type of the return value that indicates success of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <param name="faultSensitiveMethod">The fault sensitive function delegate that is invoked until an operation is successful, the amount of retry attempts has been reached, or a failed operation is not considered related to transient fault condition.</param> /// <param name="arg1">The first parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="arg2">The second parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="arg3">The third parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="arg4">The fourth parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="arg5">The fifth parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="result">The result of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="setup">The <see cref="TransientOperationOptions"/> which need to be configured.</param> /// <returns>The return value that indicates success of the function delegate <paramref name="faultSensitiveMethod"/>.</returns> public static TSuccess TryWithFunc <T1, T2, T3, T4, T5, TResult, TSuccess>(TesterFunc <T1, T2, T3, T4, T5, TResult, TSuccess> faultSensitiveMethod, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, out TResult result, Action <TransientOperationOptions> setup = null) { var factory = TesterFuncFactory.Create(faultSensitiveMethod, arg1, arg2, arg3, arg4, arg5); return(TryWithFuncCore(factory, out result, setup)); }
private static TSuccess TryWithFuncCore <TTuple, TSuccess, TResult>(TesterFuncFactory <TTuple, TResult, TSuccess> factory, out TResult result, Action <TransientOperationOptions> setup) where TTuple : Template { result = default(TResult); var options = setup.ConfigureOptions(); if (!options.EnableRecovery) { return(factory.ExecuteMethod(out result)); } DateTime timestamp = DateTime.UtcNow; TimeSpan latency = TimeSpan.Zero; TimeSpan totalWaitTime = TimeSpan.Zero; TimeSpan lastWaitTime = TimeSpan.Zero; bool throwExceptions; bool isTransientFault = false; List <Exception> aggregatedExceptions = new List <Exception>(); for (int attempts = 0; ;) { bool exceptionThrown = false; TimeSpan waitTime = options.RetryStrategy(attempts); try { if (latency > options.MaximumAllowedLatency) { throw new LatencyException(string.Format(CultureInfo.InvariantCulture, "The latency of the operation exceeded the allowed maximum value of {0} seconds. Actual latency was: {1} seconds.", options.MaximumAllowedLatency.TotalSeconds, latency.TotalSeconds)); } return(factory.ExecuteMethod(out result)); } catch (Exception ex) { try { lock (aggregatedExceptions) { aggregatedExceptions.Insert(0, ex); } isTransientFault = options.DetectionStrategy(ex); if (attempts >= options.RetryAttempts) { throw; } if (!isTransientFault) { throw; } lastWaitTime = waitTime; totalWaitTime = totalWaitTime.Add(waitTime); attempts++; Sleep(waitTime); latency = DateTime.UtcNow.Subtract(timestamp).Subtract(totalWaitTime); } catch (Exception) { throwExceptions = true; exceptionThrown = true; if (isTransientFault) { var evidence = new TransientFaultEvidence(attempts, lastWaitTime, totalWaitTime, latency, new MethodDescriptor(factory.DelegateInfo).ToString()); aggregatedExceptions.InsertTransientFaultException(evidence); FaultCallback?.Invoke(evidence); } break; } } finally { if (exceptionThrown) { IDisposable disposable = result as IDisposable; disposable?.Dispose(); } } } if (throwExceptions) { throw new AggregateException(aggregatedExceptions); } return(default(TSuccess)); }
/// <summary> /// Repetitively executes the specified <paramref name="faultSensitiveMethod"/> until the operation is successful, the amount of retry attempts has been reached, or a failed operation is not considered related to transient fault condition. /// </summary> /// <typeparam name="T">The type of the parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="TResult">The type of the out result value of the function delegate encapsulates <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="TSuccess">The type of the return value that indicates success of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <param name="faultSensitiveMethod">The fault sensitive function delegate that is invoked until an operation is successful, the amount of retry attempts has been reached, or a failed operation is not considered related to transient fault condition.</param> /// <param name="arg">The parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="result">The result of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="setup">The <see cref="TransientOperationOptions"/> which need to be configured.</param> /// <returns>The return value that indicates success of the function delegate <paramref name="faultSensitiveMethod"/>.</returns> public static TSuccess TryWithFunc <T, TResult, TSuccess>(TesterFunc <T, TResult, TSuccess> faultSensitiveMethod, T arg, out TResult result, Action <TransientOperationOptions> setup = null) { var factory = TesterFuncFactory.Create(faultSensitiveMethod, arg); return(TryWithFuncCore(factory, out result, setup)); }