Skip to content

Commit

Permalink
.Net:[OpenAPI] Prevent buffering of streamed responses (microsoft#8557)
Browse files Browse the repository at this point in the history
### Motivation and Context
Today, the `RestApiOperationRunner` reads the entire response body into
a memory buffer before the `HttpClient.SendAsync` operation returns
control to the calling code. This approach is not efficient for
scenarios where the calling code can start working with the stream
content as soon as it becomes available, without waiting until it's
fully read and buffered.

### Description
This PR specifies the `HttpCompletionOption.ResponseHeadersRead` option
as an argument of the `HttpClient.SendAsync` method to indicate that the
method should return execution control to the calling code as soon as
headers are available but before the content is read. If the calling
code specifies a custom HTTP content reader to read the response content
as a stream, then the calling code will start receiving the streamed
content as soon as its first bytes are available. In all other cases,
when the response content is consumed as a string using the
`ReadAsStringAsync` method or as a byte array using the
`ReadAsByteArrayAsync` method, the response content will be buffered in
memory and become available when fully read from the wire.

Addresses:
microsoft#8070 (comment)

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄
SergeyMenshykh authored Sep 9, 2024
1 parent 2689d64 commit 24294bf
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
@@ -214,7 +214,7 @@ private async Task<RestApiOperationResponse> SendAsync(

try
{
responseMessage = await this._httpClient.SendWithSuccessCheckAsync(requestMessage, cancellationToken).ConfigureAwait(false);
responseMessage = await this._httpClient.SendWithSuccessCheckAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

response = await this.ReadContentAndCreateOperationResponseAsync(requestMessage, responseMessage, payload, cancellationToken).ConfigureAwait(false);

0 comments on commit 24294bf

Please sign in to comment.