From f45bf3aa07a7e4850d3827ebb2fe60699c17b6b9 Mon Sep 17 00:00:00 2001 From: Jake Willey Date: Thu, 3 Sep 2020 13:46:39 -0700 Subject: [PATCH 1/2] HttpClient: Adds optimization to avoid buffering response twice --- .../src/HttpClient/CosmosHttpClientCore.cs | 22 +++++-------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/HttpClient/CosmosHttpClientCore.cs b/Microsoft.Azure.Cosmos/src/HttpClient/CosmosHttpClientCore.cs index 25878406c3..7dbd17711a 100644 --- a/Microsoft.Azure.Cosmos/src/HttpClient/CosmosHttpClientCore.cs +++ b/Microsoft.Azure.Cosmos/src/HttpClient/CosmosHttpClientCore.cs @@ -86,6 +86,7 @@ public static CosmosHttpClient CreateWithConnectionPolicy( } HttpClient httpClient = new HttpClient(httpMessageHandler); + httpClient.MaxResponseContentBufferSize = 1; return CosmosHttpClientCore.CreateHelper( httpClient: httpClient, @@ -184,7 +185,6 @@ ValueTask CreateRequestMessage() return this.SendHttpAsync( CreateRequestMessage, - HttpCompletionOption.ResponseHeadersRead, resourceType, diagnosticsContext, cancellationToken); @@ -195,21 +195,6 @@ public override Task SendHttpAsync( ResourceType resourceType, CosmosDiagnosticsContext diagnosticsContext, CancellationToken cancellationToken) - { - return this.SendHttpAsync( - createRequestMessageAsync, - HttpCompletionOption.ResponseContentRead, - resourceType, - diagnosticsContext, - cancellationToken); - } - - private Task SendHttpAsync( - Func> createRequestMessageAsync, - HttpCompletionOption httpCompletionOption, - ResourceType resourceType, - CosmosDiagnosticsContext diagnosticsContext, - CancellationToken cancellationToken) { diagnosticsContext ??= new CosmosDiagnosticsContextCore(); HttpRequestMessage requestMessage = null; @@ -230,9 +215,12 @@ private Task SendHttpAsync( resourceType.ToResourceTypeString(), requestMessage.Headers); + // Only read the header initially. The content gets copied into a memory stream later + // if we read the content http client will buffer the message and then it will get buffered + // again when it is copied to the memory stream. HttpResponseMessage responseMessage = await this.httpClient.SendAsync( requestMessage, - httpCompletionOption, + HttpCompletionOption.ResponseHeadersRead, cancellationToken); DateTime receivedTimeUtc = DateTime.UtcNow; From b3c2b0faeaae4381f221e692a1845c6cbc438fac Mon Sep 17 00:00:00 2001 From: Jake Willey Date: Thu, 3 Sep 2020 16:14:11 -0700 Subject: [PATCH 2/2] Removed httpclient buffer setting. --- Microsoft.Azure.Cosmos/src/HttpClient/CosmosHttpClientCore.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Microsoft.Azure.Cosmos/src/HttpClient/CosmosHttpClientCore.cs b/Microsoft.Azure.Cosmos/src/HttpClient/CosmosHttpClientCore.cs index 7dbd17711a..e834f94756 100644 --- a/Microsoft.Azure.Cosmos/src/HttpClient/CosmosHttpClientCore.cs +++ b/Microsoft.Azure.Cosmos/src/HttpClient/CosmosHttpClientCore.cs @@ -86,7 +86,6 @@ public static CosmosHttpClient CreateWithConnectionPolicy( } HttpClient httpClient = new HttpClient(httpMessageHandler); - httpClient.MaxResponseContentBufferSize = 1; return CosmosHttpClientCore.CreateHelper( httpClient: httpClient,