internal static async Task ImplementationAsync(Func <CancellationToken, Task> action, Context context, IEnumerable <ExceptionPredicate> shouldHandlePredicates, ICircuitController breakerController, CancellationToken cancellationToken, bool continueOnCapturedContext) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfCircuitBroken(breakerController); try { await action(cancellationToken).ConfigureAwait(continueOnCapturedContext); breakerController.OnActionSuccess(context); } catch (Exception ex) { if (cancellationToken.IsCancellationRequested) { if (ex is OperationCanceledException && ((OperationCanceledException)ex).CancellationToken == cancellationToken) { throw; } cancellationToken.ThrowIfCancellationRequested(); } if (!shouldHandlePredicates.Any(predicate => predicate(ex))) { throw; } breakerController.OnActionFailure(ex, context); throw; } }
internal static async Task ImplementationAsync(Func<CancellationToken, Task> action, Context context, IEnumerable<ExceptionPredicate> shouldHandlePredicates, ICircuitController breakerController, CancellationToken cancellationToken, bool continueOnCapturedContext) { cancellationToken.ThrowIfCancellationRequested(); breakerController.OnActionPreExecute(); try { await action(cancellationToken).ConfigureAwait(continueOnCapturedContext); breakerController.OnActionSuccess(context); } catch (Exception ex) { if (cancellationToken.IsCancellationRequested) { if (ex is OperationCanceledException && ((OperationCanceledException) ex).CancellationToken == cancellationToken) { throw; } cancellationToken.ThrowIfCancellationRequested(); } if (!shouldHandlePredicates.Any(predicate => predicate(ex))) { throw; } breakerController.OnActionFailure(ex, context); throw; } }
internal static async Task <TResult> ImplementationAsync <TResult>( Func <CancellationToken, Task <TResult> > action, Context context, IEnumerable <ExceptionPredicate> shouldHandleExceptionPredicates, IEnumerable <ResultPredicate <TResult> > shouldHandleResultPredicates, ICircuitController <TResult> breakerController, CancellationToken cancellationToken, bool continueOnCapturedContext) { cancellationToken.ThrowIfCancellationRequested(); breakerController.OnActionPreExecute(); try { DelegateResult <TResult> delegateOutcome = new DelegateResult <TResult>(await action(cancellationToken).ConfigureAwait(continueOnCapturedContext)); cancellationToken.ThrowIfCancellationRequested(); if (shouldHandleResultPredicates.Any(predicate => predicate(delegateOutcome.Result))) { breakerController.OnActionFailure(delegateOutcome, context); } else { breakerController.OnActionSuccess(context); } return(delegateOutcome.Result); } catch (Exception ex) { if (cancellationToken.IsCancellationRequested) { if (ex is OperationCanceledException && ((OperationCanceledException)ex).CancellationToken == cancellationToken) { throw; } cancellationToken.ThrowIfCancellationRequested(); } if (!shouldHandleExceptionPredicates.Any(predicate => predicate(ex))) { throw; } breakerController.OnActionFailure(new DelegateResult <TResult>(ex), context); throw; } }
internal static async Task <TResult> ImplementationAsync <TResult>( Func <Context, CancellationToken, Task <TResult> > action, Context context, IEnumerable <ExceptionPredicate> shouldHandleExceptionPredicates, IEnumerable <ResultPredicate <TResult> > shouldHandleResultPredicates, ICircuitController <TResult> breakerController, CancellationToken cancellationToken, bool continueOnCapturedContext) { cancellationToken.ThrowIfCancellationRequested(); breakerController.OnActionPreExecute(); try { TResult result = await action(context, cancellationToken).ConfigureAwait(continueOnCapturedContext); if (shouldHandleResultPredicates.Any(predicate => predicate(result))) { breakerController.OnActionFailure(new DelegateResult <TResult>(result), context); } else { breakerController.OnActionSuccess(context); } return(result); } catch (Exception ex) { Exception handledException = shouldHandleExceptionPredicates .Select(predicate => predicate(ex)) .FirstOrDefault(e => e != null); if (handledException == null) { throw; } breakerController.OnActionFailure(new DelegateResult <TResult>(handledException), context); if (handledException != ex) { ExceptionDispatchInfo.Capture(handledException).Throw(); } throw; } }
internal static TResult Implementation <TResult>( Func <Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken, ExceptionPredicates shouldHandleExceptionPredicates, ResultPredicates <TResult> shouldHandleResultPredicates, ICircuitController <TResult> breakerController) { cancellationToken.ThrowIfCancellationRequested(); breakerController.OnActionPreExecute(); try { TResult result = action(context, cancellationToken); if (shouldHandleResultPredicates.AnyMatch(result)) { breakerController.OnActionFailure(new DelegateResult <TResult>(result), context); } else { breakerController.OnActionSuccess(context); } return(result); } catch (Exception ex) { Exception handledException = shouldHandleExceptionPredicates.FirstMatchOrDefault(ex); if (handledException == null) { throw; } breakerController.OnActionFailure(new DelegateResult <TResult>(handledException), context); if (handledException != ex) { ExceptionDispatchInfo.Capture(handledException).Throw(); } throw; } }
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 handledException = shouldHandleExceptionPredicates.FirstMatchOrDefault(ex); if (handledException == null) { throw; } breakerController.OnActionFailure(new DelegateResult <TResult>(handledException), context); handledException.RethrowWithOriginalStackTraceIfDiffersFrom(ex); throw; } }
internal static void Implementation(Action action, Context context, IEnumerable <ExceptionPredicate> shouldHandlePredicates, ICircuitController breakerController) { ThrowIfCircuitBroken(breakerController); try { action(); breakerController.OnActionSuccess(context); } catch (Exception ex) { if (!shouldHandlePredicates.Any(predicate => predicate(ex))) { throw; } breakerController.OnActionFailure(ex, context); throw; } }
internal static TResult Implementation <TResult>( Func <CancellationToken, TResult> action, Context context, CancellationToken cancellationToken, IEnumerable <ExceptionPredicate> shouldHandleExceptionPredicates, IEnumerable <ResultPredicate <TResult> > shouldHandleResultPredicates, ICircuitController <TResult> breakerController) { cancellationToken.ThrowIfCancellationRequested(); breakerController.OnActionPreExecute(); try { DelegateResult <TResult> delegateOutcome = new DelegateResult <TResult>(action(cancellationToken)); if (shouldHandleResultPredicates.Any(predicate => predicate(delegateOutcome.Result))) { breakerController.OnActionFailure(delegateOutcome, context); } else { breakerController.OnActionSuccess(context); } return(delegateOutcome.Result); } catch (Exception ex) { if (!shouldHandleExceptionPredicates.Any(predicate => predicate(ex))) { throw; } breakerController.OnActionFailure(new DelegateResult <TResult>(ex), context); throw; } }