public static IAsyncPolicy <HttpResponseMessage> AdvancedCircuitBreakerAsync(
     this PolicyBuilder <HttpResponseMessage> policyBuilder,
     CircuitBreakerPolicySettings settings)
 {
     return(policyBuilder
            .AdvancedCircuitBreakerAsync(
                settings.FailureThreshold,
                settings.SamplingDuration,
                settings.MinimumThroughput,
                settings.DurationOfBreak,
                settings.OnBreak,
                settings.OnReset,
                settings.OnHalfOpen));
 }
Beispiel #2
0
        public IAsyncPolicy <T> Polly(PolicyBuilder <T> policy, ILogger <CircuitBreakerOptions <T> > log)
        {
            return(_policy = _policy ?? PolicyOrNoOP(FailPolicy == "fail" ? LogAndFail() : LogOnly()));


            IAsyncPolicy <T> LogOnly()
            {
                return(CircuitPolicy()
                       .WrapAsync(RerunFallback()));
            }

            IAsyncPolicy <T> LogAndFail()
            {
                return
                    (CircuitPolicy());
            }

            IAsyncPolicy RerunFallback()
            {
                return(Policy
                       .Handle <BrokenCircuitException>()
                       .RetryAsync());
            }

            IAsyncPolicy <T> CircuitPolicy()
            {
                return(policy
                       .AdvancedCircuitBreakerAsync(
                           failureThreshold: FailureThreshold,
                           samplingDuration: TimeSpan.FromMilliseconds(SamplingDuration),
                           minimumThroughput: MinimumThroughput,
                           durationOfBreak: TimeSpan.FromMilliseconds(DurationOfBreak),
                           onBreak: logOnBreak,
                           onReset: logOnReset,
                           onHalfOpen: logOnHalfOpen
                           ));
            }

            void logOnHalfOpen()
            {
                log.Log(LogLevel.Warning, 0, state: new BrokenCircuitStatus()
                {
                }, null, (status, exception) => "Circuit breaker on half open");
            }

            void logOnReset(Context context)
            {
                log.Log(LogLevel.Warning, 0, state: new BrokenCircuitStatus()
                {
                    OperationKey  = context.OperationKey,
                    PolicyKey     = context.PolicyKey,
                    CorrelationId = context.CorrelationId,
                }, null, (status, exception) => "Circuit breaker on reset");
            }

            void logOnBreak(DelegateResult <T> result, CircuitState circuitState, TimeSpan timeSpan,
                            Context context)
            {
                log.Log(LogLevel.Error, 0, state: new BrokenCircuitStatus()
                {
                    OperationKey  = context.OperationKey,
                    PolicyKey     = context.PolicyKey,
                    CorrelationId = context.CorrelationId,
                    TimeSpan      = timeSpan,
                    CircuitState  = circuitState
                }, result.Exception, (status, exception) => "Circuit breaker is open");
            }
        }
 public AsyncPolicy CreatePolicyAsync(PolicyBuilder policyBuilder) =>
 policyBuilder.AdvancedCircuitBreakerAsync(_failureThreshold, _samplingDuration, _minimumThroughput, _durationOfBreak);