예제 #1
0
        public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
        {
            Debug.Assert(!string.IsNullOrEmpty(this.RequestName));

            AsyncOperation asyncOp = new AsyncOperation(cb, context, extraData, this, ServicePath, RequestName.ToLower());

            if (!asyncOp.IsAsync)
            {
                // Continue tracing if traceId & spanId are provided in request header
                var traceIdString = context.Request.Headers[ServiceUtils.TRACE_ID_HTTP_HEADER] ?? context.Request.Headers[ServiceUtils.ESB_TRACE_ID_HTTP_HEADER];
                if (!string.IsNullOrWhiteSpace(traceIdString))
                {
                    context.Response.AddHeader(ServiceUtils.TRACE_ID_HTTP_HEADER, traceIdString);
                }
                var parentSpanIdString = context.Request.Headers[ServiceUtils.SPAN_ID_HTTP_HEADER] ?? context.Request.Headers[ServiceUtils.ESB_SPAN_ID_HTTP_HEADER];
                if (traceIdString != null && parentSpanIdString != null)
                {
                    try
                    {
                        var traceId      = long.Parse(traceIdString);
                        var parentSpanId = long.Parse(parentSpanIdString);
                    }
                    catch (Exception)
                    {
                        // no tracing
                    }
                }

                asyncOp.ContinueCatTransaction();
            }
            else
            {
                HostContext.Instance.Request.Items[ServiceCatConstants.SOA2AsyncServiceStartTimeKey] = DateTime.Now;
            }

            try
            {
                //检查单个IP的连接数是否超标
                CheckConnectionMaxRequestCount(context);
                //电容保护针对每个方法
                var hystrixCommand = EndpointHost.Config.MetadataMap[ServicePath].GetHystrixCommandByOpName(this.RequestName);
                var hystrixMetrics = hystrixCommand.Metrics;

                var hystrixCircuitBreaker = hystrixCommand.CircuitBreaker;
                // We fallback if circuit is open
                if (!hystrixCircuitBreaker.AllowRequest())
                {
                    hystrixMetrics.MarkShortCircuited();
                    //设置_slaErrorSent
                    asyncOp.SendSyncSLAErrorResponse(HystrixEventType.ShortCircuited);
                    if (asyncOp.IsAsync)
                    {
                        // var message = "Circuit Breaker is open. Server entered into self-protecting mode";
                    }
                    return(asyncOp);
                }

                if (asyncOp.IsAsync)
                {
                    // The circuit is healthy, We can start async work
                    hystrixMetrics.IncrementConcurrentExecutionCount();
                    EndpointHost.Config.MetadataMap[ServicePath].IncrementConcurrentExecutionCount();
                    asyncOp.StartAsyncWork();
                }
                else // sync mode
                {
                    asyncOp.StartSyncWork();
                }

                return(asyncOp);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
            }
        }