AsyncCircuitBreakerEngine
using Polly.Utilities;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Polly.CircuitBreaker
{
internal class AsyncCircuitBreakerEngine
{
internal static async Task<TResult> ImplementationAsync<TResult>(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext, ExceptionPredicates shouldHandleExceptionPredicates, ResultPredicates<TResult> shouldHandleResultPredicates, ICircuitController<TResult> breakerController)
{
cancellationToken.ThrowIfCancellationRequested();
breakerController.OnActionPreExecute();
try {
TResult result = await action(context, cancellationToken).ConfigureAwait(continueOnCapturedContext);
if (shouldHandleResultPredicates.AnyMatch(result))
breakerController.OnActionFailure(new DelegateResult<TResult>(result), context);
else
breakerController.OnActionSuccess(context);
return result;
} catch (Exception ex) {
Exception ex2 = shouldHandleExceptionPredicates.FirstMatchOrDefault(ex);
if (ex2 == null)
throw;
breakerController.OnActionFailure(new DelegateResult<TResult>(ex2), context);
ex2.RethrowWithOriginalStackTraceIfDiffersFrom(ex);
throw;
}
}
}
}