예제 #1
0
        public async Task ExecuteAsync <TResult>(ChoosenNode choosenNode, JsonOperationContext context, RavenCommand <TResult> command, CancellationToken token = default(CancellationToken))
        {
            string url;
            var    request = CreateRequest(choosenNode.Node, command, out url);

            long cachedEtag;
            BlittableJsonReaderObject cachedValue;

            HttpCache.ReleaseCacheItem cachedItem;
            using (cachedItem = GetFromCache(context, command, request, url, out cachedEtag, out cachedValue))
            {
                if (cachedEtag != 0)
                {
                    var aggresiveCacheOptions = AggressiveCaching.Value;
                    if (aggresiveCacheOptions != null && cachedItem.Age < aggresiveCacheOptions.Duration)
                    {
                        command.SetResponse(cachedValue);
                        return;
                    }

                    request.Headers.IfNoneMatch.Add(new EntityTagHeaderValue("\"" + cachedEtag + "\""));
                }

                var sp = Stopwatch.StartNew();
                HttpResponseMessage response;
                try
                {
                    response = await _httpClient.SendAsync(request, token).ConfigureAwait(false);

                    sp.Stop();
                }
                catch (HttpRequestException e) // server down, network down
                {
                    sp.Stop();
                    await HandleServerDown(choosenNode, context, command, e);

                    return;
                }
                finally
                {
                    var requestTimeInMilliseconds = sp.ElapsedMilliseconds;
                    choosenNode.Node.UpdateRequestTime(requestTimeInMilliseconds);
                    if (choosenNode.SkippedNodes != null)
                    {
                        foreach (var skippedNode in choosenNode.SkippedNodes)
                        {
                            skippedNode.DecreaseRate(requestTimeInMilliseconds);
                        }
                    }
                }

                if (response.StatusCode == HttpStatusCode.NotModified)
                {
                    cachedItem.NotModified();
                    command.SetResponse(cachedValue);
                    return;
                }
                if (response.IsSuccessStatusCode == false)
                {
                    if (await HandleUnsuccessfulResponse(choosenNode, context, command, response, url))
                    {
                        return;
                    }
                }

                if (response.Content.Headers.ContentLength.HasValue && response.Content.Headers.ContentLength == 0)
                {
                    return;
                }

                await command.ProcessResponse(context, _cache, response, url);
            }
        }
예제 #2
0
        private HttpCache.ReleaseCacheItem GetFromCache <TResult>(JsonOperationContext context, RavenCommand <TResult> command, HttpRequestMessage request, string url, out long cachedEtag, out BlittableJsonReaderObject cachedValue)
        {
            if (command.IsReadRequest)
            {
                if (request.Method != HttpMethod.Get)
                {
                    url = request.Method + "-" + url;
                }
                return(_cache.Get(context, url, out cachedEtag, out cachedValue));
            }

            cachedEtag  = 0;
            cachedValue = null;
            return(new HttpCache.ReleaseCacheItem());
        }
예제 #3
0
        public async Task ExecuteAsync <TResult>(RavenCommand <TResult> command, JsonOperationContext context, CancellationToken token = default(CancellationToken))
        {
            var choosenNode = ChooseNodeForRequest(command);

            await ExecuteAsync(choosenNode, context, command, token);
        }
예제 #4
0
 public void Execute <TResult>(RavenCommand <TResult> command, JsonOperationContext context)
 {
     AsyncHelpers.RunSync(() => ExecuteAsync(command, context));
 }
예제 #5
0
 public void Execute <TResult>(RavenCommand <TResult> command, CancellationToken cancellationToken = default(CancellationToken))
 {
     RequestExecutor.Execute(command, Context, cancellationToken);
 }
예제 #6
0
 public Task ExecuteAsync <TResult>(RavenCommand <TResult> command, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(RequestExecutor.ExecuteAsync(command, Context, cancellationToken));
 }
예제 #7
0
 public void Execute <TResult>(RavenCommand <TResult> command)
 {
     RequestExecutor.Execute(command, Context);
 }