Skip to content

Commit

Permalink
HttpClient: Adds optimization to avoid double buffering response (#1835)
Browse files Browse the repository at this point in the history
* HttpClient: Adds optimization to avoid buffering response twice

* Removed httpclient buffer setting.
  • Loading branch information
j82w authored Sep 11, 2020
1 parent ed0c8f0 commit 8c31ca0
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions Microsoft.Azure.Cosmos/src/HttpClient/CosmosHttpClientCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ ValueTask<HttpRequestMessage> CreateRequestMessage()

return this.SendHttpAsync(
CreateRequestMessage,
HttpCompletionOption.ResponseHeadersRead,
resourceType,
diagnosticsContext,
cancellationToken);
Expand All @@ -195,21 +194,6 @@ public override Task<HttpResponseMessage> SendHttpAsync(
ResourceType resourceType,
CosmosDiagnosticsContext diagnosticsContext,
CancellationToken cancellationToken)
{
return this.SendHttpAsync(
createRequestMessageAsync,
HttpCompletionOption.ResponseContentRead,
resourceType,
diagnosticsContext,
cancellationToken);
}

private Task<HttpResponseMessage> SendHttpAsync(
Func<ValueTask<HttpRequestMessage>> createRequestMessageAsync,
HttpCompletionOption httpCompletionOption,
ResourceType resourceType,
CosmosDiagnosticsContext diagnosticsContext,
CancellationToken cancellationToken)
{
diagnosticsContext ??= new CosmosDiagnosticsContextCore();
HttpRequestMessage requestMessage = null;
Expand All @@ -230,9 +214,12 @@ private Task<HttpResponseMessage> 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;
Expand Down

0 comments on commit 8c31ca0

Please sign in to comment.