Пример #1
0
        internal static DurableCircuitBreaker ConfigureCircuitBreaker(IDurableEntityContext context, ILogger log)
        {
            string circuitBreakerId = context.EntityKey;

            log?.LogCircuitBreakerMessage(circuitBreakerId, $"Setting configuration for circuit-breaker {circuitBreakerId}.");

            // Intentionally no defaults - users should consciously decide what tolerances suit the operations invoked through the circuit-breaker.
            TimeSpan breakDuration          = XmlConvert.ToTimeSpan(GetCircuitConfiguration(circuitBreakerId, "BreakDuration"));
            int      maxConsecutiveFailures = Convert.ToInt32(GetCircuitConfiguration(circuitBreakerId, "MaxConsecutiveFailures"));

            var breaker = new DurableCircuitBreaker(null)
            {
                CircuitState            = CircuitState.Closed,
                BrokenUntil             = DateTime.MinValue,
                ConsecutiveFailureCount = 0,
                MaxConsecutiveFailures  = maxConsecutiveFailures,
                BreakDuration           = breakDuration
            };

            if (breaker.BreakDuration <= TimeSpan.Zero)
            {
                throw new InvalidOperationException($"Circuit-breaker {circuitBreakerId} must be configured with a positive break-duration.");
            }

            if (breaker.MaxConsecutiveFailures <= 0)
            {
                throw new InvalidOperationException($"Circuit-breaker {circuitBreakerId}  must be configured with a max number of consecutive failures greater than or equal to 1.");
            }

            return(breaker);
        }
Пример #2
0
        public async Task <DurableCircuitBreaker> GetBreakerState(string circuitBreakerId, ILogger log, IDurableClient durableClient)
        {
            log?.LogCircuitBreakerMessage(circuitBreakerId, $"Getting breaker state for circuit-breaker = '{circuitBreakerId}'.");

            var readState = await durableClient.ReadEntityStateAsync <DurableCircuitBreaker>(DurableCircuitBreaker.GetEntityId(circuitBreakerId));

            // We present a not-yet-initialized circuit-breaker as null (it will be initialized when successes or failures are first posted against it).
            if (!readState.EntityExists || readState.EntityState == null)
            {
                return(null);
            }

            return(readState.EntityState);
        }
Пример #3
0
        public async Task <CircuitState> GetCircuitState(string circuitBreakerId, ILogger log, IDurableClient durableClient)
        {
            log?.LogCircuitBreakerMessage(circuitBreakerId, $"Getting circuit state for circuit-breaker = '{circuitBreakerId}'.");

            var readState = await durableClient.ReadEntityStateAsync <DurableCircuitBreaker>(DurableCircuitBreaker.GetEntityId(circuitBreakerId));

            // To keep the return type simple, we present a not-yet-initialized circuit-breaker as closed (it will be closed when first used).
            return(readState.EntityExists && readState.EntityState != null ? readState.EntityState.CircuitState : CircuitState.Closed);
        }