Example #1
0
        public static ExceptionPolicy CircuitBreak(this PolicyBuilder<ExceptionHandler> builder, TimeSpan duration, int threshold)
        {
            Guard.Against.Null(builder, "builder cannot be null");
            if (duration == default(TimeSpan))
                throw new ArgumentOutOfRangeException("duration", "must be greater than zero");
            if (threshold <= 0)
                throw new ArgumentOutOfRangeException("threshold", "must be greater than zero");

            var breaker = new CircuitBreaker(duration, threshold);

            return new ExceptionPolicy(action => ImplementPolicy(action, builder.Condition, breaker));
        }
Example #2
0
        private static void ImplementPolicy(Action action, ExceptionHandler isHandled, CircuitBreaker breaker)
        {
            if (breaker.IsBroken)
                throw breaker.LastException;

            try
            {
                action();
                breaker.Reset();
                return;
            }
            catch (Exception ex)
            {
                if (!isHandled(ex))
                    throw;

                breaker.TryBreak(ex);
                throw;
            }
        }