Exemplo n.º 1
0
 private static void UpdateMetric(IRequestTimeMetric metric, long value, int numberOfRequests)
 {
     for (var i = 0; i < numberOfRequests; i++)
     {
         metric.Update(value);
     }
 }
Exemplo n.º 2
0
        private Task <RavenJToken> SendRequestInternal(Func <HttpRequestMessage> getRequestMessage, bool readErrorString = true)
        {
            if (isRequestSentToServer && Debugger.IsAttached == false)
            {
                throw new InvalidOperationException("Request was already sent to the server, cannot retry request.");
            }
            isRequestSentToServer = true;

            return(RunWithAuthRetry(async() =>
            {
                try
                {
                    var requestMessage = getRequestMessage();
                    CopyHeadersToHttpRequestMessage(requestMessage);
                    var sp = Stopwatch.StartNew();
                    try
                    {
                        Response = await httpClient.SendAsync(requestMessage).ConfigureAwait(false);
                    }
                    catch (OperationCanceledException e)
                    {
                        throw new TimeoutException(string.Format("Request for {0} timed out after {1:#,#;;0} ms", requestMessage.RequestUri, sp.ElapsedMilliseconds), e);
                    }
                    SetResponseHeaders(Response);
                    AssertServerVersionSupported();
                    ResponseStatusCode = Response.StatusCode;
                }

                catch (HttpRequestException e)
                {
                    if (Response == null) //something bad happened and httpClient.SendAsync failed -> i.e. server down, network down
                    {
                        e.Data.Add(Constants.RequestFailedExceptionMarker, true);
                    }

                    throw ErrorResponseException.FromHttpRequestException(e);
                }
                finally
                {
                    sp.Stop();
                    if (requestTimeMetric != null)
                    {
                        requestTimeMetric.Update((int)sp.Elapsed.TotalMilliseconds);
                    }
                }

                // throw the conflict exception
                return await CheckForErrorsAndReturnCachedResultIfAnyAsync(readErrorString).ConfigureAwait(false);
            }));
        }
Exemplo n.º 3
0
        private Task <RavenJToken> SendRequestInternal(Func <HttpRequestMessage> getRequestMessage, bool readErrorString = true)
        {
            if (isRequestSentToServer && Debugger.IsAttached == false)
            {
                throw new InvalidOperationException("Request was already sent to the server, cannot retry request.");
            }
            isRequestSentToServer = true;

            return(RunWithAuthRetry(async() =>
            {
                try
                {
                    var requestMessage = getRequestMessage();
                    CopyHeadersToHttpRequestMessage(requestMessage);
                    Response = await httpClient.SendAsync(requestMessage).ConfigureAwait(false);
                    SetResponseHeaders(Response);
                    AssertServerVersionSupported();
                    ResponseStatusCode = Response.StatusCode;
                }
                catch (HttpRequestException e)
                {
                    var exception = ErrorResponseException.FromException(e);
                    exception.Data.Add(Constants.RequestFailedExceptionMarker, true);
                    Response = exception.Response;
                    ResponseStatusCode = Response.StatusCode;
                    throw exception;
                }
                catch (TaskCanceledException e)
                {
                    var exception = ErrorResponseException.FromException(e);
                    exception.Data.Add(Constants.RequestFailedExceptionMarker, true);
                    Response = exception.Response;
                    ResponseStatusCode = Response.StatusCode;
                    throw exception;
                }
                finally
                {
                    sp.Stop();
                    if (requestTimeMetric != null)
                    {
                        requestTimeMetric.Update((int)sp.Elapsed.TotalMilliseconds);
                    }
                }

                // throw the conflict exception
                return await CheckForErrorsAndReturnCachedResultIfAnyAsync(readErrorString).ConfigureAwait(false);
            }));
        }
Exemplo n.º 4
0
        public void Update(long requestTimeInMilliseconds)
        {
            var rate    = RequestTimeMetric.Rate();
            var maxRate = MaxDecreasingRatio * rate;
            var minRate = MinDecreasingRatio * rate;

            var decreasingRate = rate - requestTimeInMilliseconds;

            if (decreasingRate > maxRate)
            {
                decreasingRate = maxRate;
            }

            if (decreasingRate < minRate)
            {
                decreasingRate = minRate;
            }

            RequestTimeMetric.Update((long)decreasingRate);
        }