private static async Task <TimeMeasureProfiler <TResult> > WithFunctionAsyncCore <TTuple, TResult>(TaskFuncFactory <TTuple, TResult> factory, Action <TimeMeasureOptions> setup) where TTuple : Template
        {
            var options    = setup.ConfigureOptions();
            var descriptor = options.MethodDescriptor?.Invoke() ?? new MethodDescriptor(factory.DelegateInfo);
            var profiler   = new TimeMeasureProfiler <TResult>()
            {
                Member = descriptor.ToString(),
                Data   = descriptor.MergeParameters(options.RuntimeParameters ?? factory.GenericArguments.ToArray())
            };

            await PerformTimeMeasuringAsync(profiler, options, async p => p.Result = await factory.ExecuteMethodAsync().ContinueWithSuppressedContext()).ContinueWithSuppressedContext();

            return(profiler);
        }
Exemple #2
0
        private static async Task <TResult> WithFuncAsyncCore <TTuple, TResult>(TaskFuncFactory <TTuple, TResult> factory, Action <TransientOperationOptions> setup) where TTuple : Template
        {
            var options = setup.ConfigureOptions();

            if (!options.EnableRecovery)
            {
                return(await factory.ExecuteMethodAsync().ContinueWithSuppressedContext());
            }
            DateTime         timestamp        = DateTime.UtcNow;
            TimeSpan         latency          = TimeSpan.Zero;
            TimeSpan         totalWaitTime    = TimeSpan.Zero;
            TimeSpan         lastWaitTime     = TimeSpan.Zero;
            bool             isTransientFault = false;
            bool             throwExceptions;
            List <Exception> aggregatedExceptions = new List <Exception>();
            TResult          result = default(TResult);

            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(await factory.ExecuteMethodAsync().ContinueWithSuppressedContext());
                }
                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++;
                        await Task.Delay(waitTime).ContinueWithSuppressedContext();

                        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(result);
        }