示例#1
0
        public void TestRetryPolicyWithPreDefinedFalse()
        {
            var retryPolicyContext = new RetryPolicyContext(null, "200", 0, "ecs", "2019-06-01",
                                                            "DescribeInstance", RetryCondition.BlankStatus);

            var retryPolicy = new RetryPolicy(3, false);

            var shouldRetry = retryPolicy.ShouldRetry(retryPolicyContext);
            var delay       = retryPolicy.GetDelayTimeBeforeNextRetry(retryPolicyContext);

            Assert.Equal(RetryCondition.NoRetry | RetryCondition.ShouldRetry, shouldRetry);
            Assert.True(500 <= delay);
        }
示例#2
0
        public void TestRetryPolicyWithNoRetry()
        {
            var retryPolicyContext = new RetryPolicyContext(null, "200", 3, "ecs", "2019-06-01",
                                                            "DescribeInstance", RetryCondition.BlankStatus);

            var retryPolicy = new RetryPolicy();

            var shouldRetry = retryPolicy.ShouldRetry(retryPolicyContext);
            var delay       = retryPolicy.GetDelayTimeBeforeNextRetry(retryPolicyContext);

            Assert.Equal(RetryCondition.NoRetry, shouldRetry);
            Assert.Equal(0, delay);
        }
        public override HttpResponse DoAction <T>(AcsRequest <T> request, bool autoRetry, int maxRetryNumber,
                                                  string regionId,
                                                  AlibabaCloudCredentials credentials, Signer signer, FormatType?format, List <Endpoint> endpoints)
        {
            var                httpStatusCode    = "";
            var                retryAttemptTimes = 0;
            ClientException    exception;
            RetryPolicyContext retryPolicyContext;

            do
            {
                try
                {
                    var watch = Stopwatch.StartNew();

                    FormatType?requestFormatType = request.AcceptFormat;
                    format = requestFormatType;

                    ProductDomain domain;
                    if (!string.IsNullOrEmpty(this.endpoint))
                    {
                        domain = new ProductDomain("", this.endpoint);
                    }
                    else
                    {
                        domain = request.ProductDomain ??
                                 Aliyun.Acs.Core.Regions.Endpoint.FindProductDomain(regionId, request.Product, endpoints);
                    }

                    if (null == domain)
                    {
                        throw new ClientException("SDK.InvalidRegionId", "Can not find endpoint to access.");
                    }

                    var userAgent = UserAgent.Resolve(request.GetSysUserAgentConfig(), userAgentConfig);
                    DictionaryUtil.Add(request.Headers, "User-Agent", userAgent);
                    DictionaryUtil.Add(request.Headers, "x-acs-version", request.Version);
                    if (!string.IsNullOrWhiteSpace(request.ActionName))
                    {
                        DictionaryUtil.Add(request.Headers, "x-acs-action", request.ActionName);
                    }

                    var httpRequest = SignRequest(request, signer, credentials, format, domain);
                    Helper.RunInstanceMethod(typeof(DefaultAcsClient), "ResolveTimeout", this, new object[] { httpRequest, request.Product, request.Version, request.ActionName });
                    SetHttpsInsecure(IgnoreCertificate);
                    ResolveProxy(httpRequest, request);
                    var response = GetResponse(httpRequest);

                    httpStatusCode = response.Status.ToString();
                    Helper.RunInstanceMethod(typeof(DefaultAcsClient), "PrintHttpDebugMsg", this, new object[] { request, response });
                    watch.Stop();
                    return(response);
                }
                catch (ClientException ex)
                {
                    retryPolicyContext = new RetryPolicyContext(ex, httpStatusCode, retryAttemptTimes, request.Product,
                                                                request.Version,
                                                                request.ActionName, RetryCondition.BlankStatus);

                    exception = ex;
                }

                Thread.Sleep(retryPolicy.GetDelayTimeBeforeNextRetry(retryPolicyContext));
            } while ((retryPolicy.ShouldRetry(retryPolicyContext) & RetryCondition.NoRetry) != RetryCondition.NoRetry);

            if (exception != null)
            {
                throw new ClientException(exception.ErrorCode, exception.ErrorMessage);
            }

            return(null);
        }
        public virtual HttpResponse DoAction <T>(AcsRequest <T> request, bool autoRetry, int maxRetryNumber,
                                                 string regionId,
                                                 AlibabaCloudCredentials credentials, Signer signer, FormatType?format, List <Endpoint> endpoints)
            where T : AcsResponse
        {
            var                httpStatusCode    = "";
            var                retryAttemptTimes = 0;
            ClientException    exception;
            RetryPolicyContext retryPolicyContext;

            do
            {
                try
                {
                    var watch = Stopwatch.StartNew();

                    FormatType?requestFormatType = request.AcceptFormat;
                    format = requestFormatType;

                    var domain = request.ProductDomain ??
                                 Endpoint.FindProductDomain(regionId, request.Product, endpoints);

                    if (null == domain)
                    {
                        throw new ClientException("SDK.InvalidRegionId", "Can not find endpoint to access.");
                    }

                    request.Headers["User-Agent"] =
                        UserAgent.Resolve(request.GetSysUserAgentConfig(), userAgentConfig);
                    var httpRequest = request.SignRequest(signer, credentials, format, domain);
                    ResolveTimeout(httpRequest);
                    SetHttpsInsecure(IgnoreCertificate);
                    ResolveProxy(httpRequest, request);
                    var response = GetResponse(httpRequest);

                    httpStatusCode = response.Status.ToString();
                    PrintHttpDebugMsg(request, response);
                    watch.Stop();
                    CommonLog.ExecuteTime = watch.ElapsedMilliseconds;
                    return(response);
                }
                catch (ClientException ex)
                {
                    retryPolicyContext = new RetryPolicyContext(ex, httpStatusCode, retryAttemptTimes, request.Product,
                                                                request.Version,
                                                                request.ActionName, RetryCondition.BlankStatus);

                    CommonLog.LogException(ex, ex.ErrorCode, ex.ErrorMessage);
                    exception = ex;
                }

                Thread.Sleep(retryPolicy.GetDelayTimeBeforeNextRetry(retryPolicyContext));
            } while ((retryPolicy.ShouldRetry(retryPolicyContext) & RetryCondition.NoRetry) != RetryCondition.NoRetry);

            if (exception != null)
            {
                CommonLog.LogException(exception, exception.ErrorCode, exception.ErrorMessage);
                throw new ClientException(exception.ErrorCode, exception.ErrorMessage);
            }

            return(null);
        }