Ejemplo n.º 1
0
        private Task Notify(ICircuitRequest request, ICircuitResponse response, TimeSpan executionTime)
        {
            if (_listeners == null || !_listeners.Any())
            {
                return(Task.CompletedTask);
            }

            try
            {
                var data = new CircuitStatusData
                {
                    RequestId     = request.RequestId,
                    CircuitKey    = request.CircuitKey,
                    Context       = request.Context,
                    ExecutionTime = executionTime,
                    Status        = response.Status
                };

                var tasks = _listeners.Select(x => x.Notify(data)).ToArray();

                return(Task.WhenAll(tasks));
            }
            catch (Exception e)
            {
                CircuitBreakerLog.LogError(e, e.Message);
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 2
0
        private async Task <TResponse> HandleError <TResponse>(ICircuitRequest request, Exception e) where TResponse : ICircuitResponse, new()
        {
            CircuitBreakerLog.LogError(e, e.Message);

            var response = new TResponse {
                Status = CircuitStatus.Failed
            };

            await Notify(request, response, TimeSpan.Zero);

            return(response);
        }
Ejemplo n.º 3
0
        private TResponse BuildResponse <TResponse>(ICircuitRequest request, Polly.OutcomeType outcome, Exception finalException, TimeSpan executionTime) where TResponse : ICircuitResponse, new()
        {
            var result = new TResponse();

            if (outcome == Polly.OutcomeType.Failure)
            {
                if (finalException is Polly.Timeout.TimeoutRejectedException || finalException is TimeoutException)
                {
                    Trace(CircuitStatus.Timeout, request, finalException, executionTime);

                    result.Status = CircuitStatus.Timeout;
                }
                else if (finalException is Polly.CircuitBreaker.BrokenCircuitException)
                {
                    Trace(CircuitStatus.Broken, request, finalException, executionTime);

                    result.Status = CircuitStatus.Broken;
                }
                else
                {
                    CircuitBreakerLog.LogError(finalException, finalException.Message);

                    Trace(CircuitStatus.Failed, request, finalException, executionTime);

                    result.Status = CircuitStatus.Failed;
                }
            }
            else
            {
                Trace(CircuitStatus.Succeed, request, finalException, executionTime);

                result.Status = CircuitStatus.Succeed;
            }

            return(result);
        }