Ejemplo n.º 1
0
 /// <summary>
 /// For bad request exception, CHystrix circuit breaker will igore it (think it is a success).
 /// This kind of validation error will not cause chystrix circuit breaker open.
 /// </summary>
 /// <param name="name">the identity for the custom bad request exception checker</param>
 /// <param name="isBadRequestException"></param>
 public static void RegisterCustomBadRequestExceptionChecker(string name, Func <Exception, bool> isBadRequestException)
 {
     if (CHystrixIntegration.HasCHystrix && CHystrixIntegration.HasCustomBadRequestExceptionSupport)
     {
         CHystrixIntegration.RegisterCustomBadRequestExceptionChecker(name, isBadRequestException);
     }
 }
        private void SemaphoreIsolationStartExecution(object instance)
        {
            if (instance == null)
            {
                return;
            }

            CHystrixIntegration.UtilsSemaphoreIsolationStartExecution(instance);
        }
        private void SemaphoreIsolationEndExecution(ClientExecutionContext context, object instance)
        {
            try
            {
                if (instance == null)
                {
                    return;
                }

                CHystrixIntegration.UtilsSemaphoreIsolationEndExecution(instance);
            }
            catch (Exception ex)
            {
                Dictionary <string, string> addtionalInfo = GetClientInfo(context);
                addtionalInfo["ErrorCode"] = "FXD301018";
                log.Warn("CHystrix IOCP Support EndExecution failed", ex, addtionalInfo);
            }
        }
Ejemplo n.º 4
0
        private void GetCHystrixCommandKeyForIOCPAsync(ClientExecutionContext context, out string chystrixCommandKey, out string chystrixInstanceKey)
        {
            string instanceKey = context.Host;

            chystrixCommandKey = CHystrixIOCPCommandKeys.GetOrAdd(
                CHystrixCommandKeyPrefixForIOCPAsync + context.Operation + "/" + instanceKey,
                key =>
            {
                string commandKey = HystrixCommandKeyPrefixForIOCPAsync + "." + context.Operation.ToLower();
                int count         = GetCHystrixCommandMaxConcurrentCount(context.Operation);
                if (count > 0)
                {
                    CHystrixIntegration.UtilsSemaphoreIsolationConfig(instanceKey, commandKey, HystrixCommandGroupKey, HystrixCommandDomain, count);
                }
                else
                {
                    CHystrixIntegration.UtilsSemaphoreIsolationConfig(instanceKey, commandKey, HystrixCommandGroupKey, HystrixCommandDomain);
                }

                return(commandKey);
            });
            chystrixInstanceKey = instanceKey;
        }
        private object CreateCHystrixSemaphoreIsolationInstance(ClientExecutionContext context)
        {
            object chystrixSemaphoreIsolationInstance = null;

            try
            {
                if (EnableCHystrixSupportForIOCPAsync)
                {
                    string chystrixCommandKey;
                    string chystrixInstanceKey;
                    GetCHystrixCommandKeyForIOCPAsync(context, out chystrixCommandKey, out chystrixInstanceKey);
                    chystrixSemaphoreIsolationInstance = CHystrixIntegration.UtilsSemaphoreIsolationCreateInstance(chystrixInstanceKey, chystrixCommandKey);
                }
            }
            catch (Exception ex)
            {
                Dictionary <string, string> addtionalInfo = GetClientInfo(context);
                addtionalInfo["ErrorCode"] = "FXD301014";
                log.Warn("CHystrix IOCP Support CreateInstance failed", ex, addtionalInfo);
            }

            return(chystrixSemaphoreIsolationInstance);
        }
        public TResponse Invoke <TResponse>(string operationName, object requestObject, Func <TResponse> getFallback)
        {
            ClientExecutionContext context = CreateExecutionContext(operationName, requestObject, ExecutionModes.SynchronizationMode);

            Validate(context);

            try
            {
                if (!EnableCHystrixSupport)
                {
                    return(InvokeInternal <TResponse>(context));
                }

                string chystrixCommandKey;
                string chystrixInstanceKey;
                GetCHystrixCommandKey(context, out chystrixCommandKey, out chystrixInstanceKey);

                if (getFallback == null)
                {
                    return(CHystrixIntegration.RunCommand <TResponse>(chystrixInstanceKey, chystrixCommandKey, () => InvokeInternal <TResponse>(context), null));
                }

                return(CHystrixIntegration.RunCommand <TResponse>(chystrixInstanceKey, chystrixCommandKey, () => InvokeInternal <TResponse>(context), () => getFallback()));
            }
            catch (Exception ex)
            {
                context.IsSuccess = false;
                context.Error     = ex;

                throw;
            }
            finally
            {
                ApplyRequestEndFilterSafe(context);
            }
        }