Ejemplo n.º 1
0
        public T Request <T>(string path, object request) where T : IHasResponseStatus
        {
            int            retryTime      = _httpClientRetryTimes.Value;
            ResponseStatus responseStatus = null;

            for (int i = 0; i < retryTime; i++)
            {
                AddressContext context = null;
                try
                {
                    context = _addressManager.AddressContext;
                    string requestUrl = context.CustomHttpUrl(path);
                    T      response   = requestUrl.PostJsonToUrl(request).FromJson <T>();
                    if (response == null || response.ResponseStatus == null)
                    {
                        throw new Exception("Got null response or null response status.");
                    }

                    responseStatus = response.ResponseStatus;
                    bool isServiceDown = responseStatus.IsServiceDown();
                    bool isRerunnable  = responseStatus.IsRerunnable();
                    if (!(isServiceDown || isRerunnable))
                    {
                        return(response);
                    }

                    if (isServiceDown)
                    {
                        context.MarkUnavailable();
                    }

                    _log.Info("get response failed, but can be retried. at turn: " + (i + 1) + ". responseStatus: " + responseStatus);
                }
                catch (Exception e)
                {
                    if (context != null)
                    {
                        context.MarkUnavailable();
                    }

                    if (i < retryTime - 1)
                    {
                        _log.Info("get response failed in this turn: " + (i + 1), e);
                    }
                    else
                    {
                        _log.Error("get response failed at turn: " + retryTime, e);
                        throw;
                    }
                }

                Thread.Sleep(_retryInterval.Value);
            }

            throw new Exception("Got failed response: " + responseStatus);
        }