Beispiel #1
0
        public CircuitBreakerOptions Build()
        {
            var circuitBreakerOptions = new CircuitBreakerOptions();

            if (_circuitBreakerOptionsSection != null && _circuitBreakerOptionsSection.Exists())
            {
                circuitBreakerOptions = _circuitBreakerOptionsSection.Get <CircuitBreakerOptions>();
                _services.Configure <CircuitBreakerOptions>(_circuitBreakerOptionsSection);
            }
            else
            {
                circuitBreakerOptions.OpenToHalfOpenWaitTimeInSeconds              = _openToHalfOpenWaitTimeInSeconds;
                circuitBreakerOptions.ConcurrentHalfOpenAttempts                   = _concurrentHalfOpenAttempts;
                circuitBreakerOptions.NumberOfFailuresBeforeOpen                   = _numberOfFailuresBeforeOpen;
                circuitBreakerOptions.NumberOfHalfOpenSuccessesToClose             = _numberOfHalfOpenSuccessesToClose;
                circuitBreakerOptions.SecondsOpenBeforeCriticalFailureNotification = _secondsOpenBeforeCriticalFailureNotification;
            }

            if (_exceptionPredicates.Count > 0)
            {
                _services.AddSingleton <ICircuitBreakerExceptionPredicatesProvider>(new ConfigCircuitBreakerExceptionPredicatesProvider(_exceptionPredicates));
            }

            _services.AddSingleton(circuitBreakerOptions);

            return(circuitBreakerOptions);
        }
Beispiel #2
0
        public CircuitBreaker(ICircuitBreakerStateStore stateStore,
                              CircuitBreakerOptions circuitBreakerOptions,
                              ILogger <CircuitBreaker> logger,
                              ICircuitBreakerExceptionEvaluator exceptionEvaluator)
        {
            if (circuitBreakerOptions is null)
            {
                throw new ArgumentNullException(nameof(circuitBreakerOptions));
            }

            _stateStore                                = stateStore ?? throw new ArgumentNullException(nameof(stateStore));
            _logger                                    = logger ?? throw new ArgumentNullException(nameof(logger));
            _exceptionEvaluator                        = exceptionEvaluator ?? throw new ArgumentNullException(nameof(exceptionEvaluator));
            _openToHalfOpenWaitTime                    = TimeSpan.FromSeconds(circuitBreakerOptions.OpenToHalfOpenWaitTimeInSeconds);
            _concurrentHalfOpenAttempts                = circuitBreakerOptions.ConcurrentHalfOpenAttempts;
            _numberOfFailuresBeforeOpen                = circuitBreakerOptions.NumberOfFailuresBeforeOpen;
            _numberOfHalfOpenSuccessesToClose          = circuitBreakerOptions.NumberOfHalfOpenSuccessesToClose;
            _timeOpenBeforeCriticalFailureNotification = TimeSpan.FromSeconds(circuitBreakerOptions.SecondsOpenBeforeCriticalFailureNotification);
            _halfOpenSemaphore                         = new SemaphoreSlim(_concurrentHalfOpenAttempts, _concurrentHalfOpenAttempts);
            _timer = new Timer(CriticalFailureNotification);
        }