<PackageReference Include="Polly" Version="8.0.0-alpha.7" />

AsyncAdvancedCircuitBreakerSyntax

public static class AsyncAdvancedCircuitBreakerSyntax
using Polly.CircuitBreaker; using Polly.Utilities; using System; using System.Runtime.CompilerServices; namespace Polly { public static class AsyncAdvancedCircuitBreakerSyntax { public static AsyncCircuitBreakerPolicy AdvancedCircuitBreakerAsync(this PolicyBuilder policyBuilder, double failureThreshold, TimeSpan samplingDuration, int minimumThroughput, TimeSpan durationOfBreak) { Action<Exception, TimeSpan> onBreak = delegate { }; Action onReset = delegate { }; return policyBuilder.AdvancedCircuitBreakerAsync(failureThreshold, samplingDuration, minimumThroughput, durationOfBreak, onBreak, onReset); } public static AsyncCircuitBreakerPolicy AdvancedCircuitBreakerAsync(this PolicyBuilder policyBuilder, double failureThreshold, TimeSpan samplingDuration, int minimumThroughput, TimeSpan durationOfBreak, Action<Exception, TimeSpan> onBreak, Action onReset) { return policyBuilder.AdvancedCircuitBreakerAsync(failureThreshold, samplingDuration, minimumThroughput, durationOfBreak, delegate(Exception exception, TimeSpan timespan, Context _) { onBreak(exception, timespan); }, delegate { onReset(); }); } public static AsyncCircuitBreakerPolicy AdvancedCircuitBreakerAsync(this PolicyBuilder policyBuilder, double failureThreshold, TimeSpan samplingDuration, int minimumThroughput, TimeSpan durationOfBreak, Action<Exception, TimeSpan, Context> onBreak, Action<Context> onReset) { Action onHalfOpen = delegate { }; return policyBuilder.AdvancedCircuitBreakerAsync(failureThreshold, samplingDuration, minimumThroughput, durationOfBreak, onBreak, onReset, onHalfOpen); } public static AsyncCircuitBreakerPolicy AdvancedCircuitBreakerAsync(this PolicyBuilder policyBuilder, double failureThreshold, TimeSpan samplingDuration, int minimumThroughput, TimeSpan durationOfBreak, Action<Exception, TimeSpan> onBreak, Action onReset, Action onHalfOpen) { return policyBuilder.AdvancedCircuitBreakerAsync(failureThreshold, samplingDuration, minimumThroughput, durationOfBreak, delegate(Exception exception, TimeSpan timespan, Context _) { onBreak(exception, timespan); }, delegate { onReset(); }, onHalfOpen); } public static AsyncCircuitBreakerPolicy AdvancedCircuitBreakerAsync(this PolicyBuilder policyBuilder, double failureThreshold, TimeSpan samplingDuration, int minimumThroughput, TimeSpan durationOfBreak, Action<Exception, TimeSpan, Context> onBreak, Action<Context> onReset, Action onHalfOpen) { return policyBuilder.AdvancedCircuitBreakerAsync(failureThreshold, samplingDuration, minimumThroughput, durationOfBreak, delegate(Exception exception, CircuitState _, TimeSpan timespan, Context context) { onBreak(exception, timespan, context); }, onReset, onHalfOpen); } public static AsyncCircuitBreakerPolicy AdvancedCircuitBreakerAsync(this PolicyBuilder policyBuilder, double failureThreshold, TimeSpan samplingDuration, int minimumThroughput, TimeSpan durationOfBreak, Action<Exception, CircuitState, TimeSpan, Context> onBreak, Action<Context> onReset, Action onHalfOpen) { TimeSpan t = TimeSpan.FromTicks(AdvancedCircuitController<EmptyStruct>.ResolutionOfCircuitTimer); if (failureThreshold <= 0) throw new ArgumentOutOfRangeException("failureThreshold", "Value must be greater than zero."); if (failureThreshold > 1) throw new ArgumentOutOfRangeException("failureThreshold", "Value must be less than or equal to one."); if (samplingDuration < t) { DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(113, 1); defaultInterpolatedStringHandler.AppendLiteral("Value must be equal to or greater than "); defaultInterpolatedStringHandler.AppendFormatted(t.TotalMilliseconds); defaultInterpolatedStringHandler.AppendLiteral(" milliseconds. This is the minimum resolution of the CircuitBreaker timer."); throw new ArgumentOutOfRangeException("samplingDuration", defaultInterpolatedStringHandler.ToStringAndClear()); } if (minimumThroughput <= 1) throw new ArgumentOutOfRangeException("minimumThroughput", "Value must be greater than one."); if (durationOfBreak < TimeSpan.Zero) throw new ArgumentOutOfRangeException("durationOfBreak", "Value must be greater than zero."); if (onBreak == null) throw new ArgumentNullException("onBreak"); if (onReset == null) throw new ArgumentNullException("onReset"); if (onHalfOpen == null) throw new ArgumentNullException("onHalfOpen"); AdvancedCircuitController<EmptyStruct> breakerController = new AdvancedCircuitController<EmptyStruct>(failureThreshold, samplingDuration, minimumThroughput, durationOfBreak, delegate(DelegateResult<EmptyStruct> outcome, CircuitState state, TimeSpan timespan, Context context) { onBreak(outcome.Exception, state, timespan, context); }, onReset, onHalfOpen); return new AsyncCircuitBreakerPolicy(policyBuilder, breakerController); } } }