Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpClient: Adds optimization to avoid double buffering response #1835

Merged
merged 6 commits into from
Sep 11, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions Microsoft.Azure.Cosmos/src/HttpClient/CosmosHttpClientCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public static CosmosHttpClient CreateWithConnectionPolicy(
}

HttpClient httpClient = new HttpClient(httpMessageHandler);
httpClient.MaxResponseContentBufferSize = 1;
j82w marked this conversation as resolved.
Show resolved Hide resolved
j82w marked this conversation as resolved.
Show resolved Hide resolved

return CosmosHttpClientCore.CreateHelper(
httpClient: httpClient,
Expand Down Expand Up @@ -184,7 +185,6 @@ ValueTask<HttpRequestMessage> CreateRequestMessage()

return this.SendHttpAsync(
CreateRequestMessage,
HttpCompletionOption.ResponseHeadersRead,
resourceType,
diagnosticsContext,
cancellationToken);
Expand All @@ -195,21 +195,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 +215,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,
j82w marked this conversation as resolved.
Show resolved Hide resolved
cancellationToken);

DateTime receivedTimeUtc = DateTime.UtcNow;
Expand Down