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

[API Proposal]: Expose overloads of HttpContent.LoadIntoBufferAsync taking a CancellationToken #102659

Open
andrewhickman-aveva opened this issue May 24, 2024 · 4 comments · May be fixed by #103991
Labels
api-approved API was approved in API review, it can be implemented area-System.Net.Http help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@andrewhickman-aveva
Copy link

andrewhickman-aveva commented May 24, 2024

Background and motivation

HttpContent.LoadIntoBufferAsync and HttpContent.LoadIntoBufferAsync(long) currently forward to overloads which take a cancellation token, passing CancellationToken.None.

public Task LoadIntoBufferAsync(long maxBufferSize) =>
LoadIntoBufferAsync(maxBufferSize, CancellationToken.None);

HttpClient also uses this API but passes a cancellation token:

response.Content.LoadIntoBuffer(_maxResponseContentBufferSize, cts.Token);

It would be useful for external consumers to be able to pass a cancellation token, similar to HttpContent.CopyToAsync(Stream, CancellationToken

API Proposal

 namespace System.Net.Http;
 
 public class HttpContent
 {
     public Task LoadIntoBufferAsync();
     public Task LoadIntoBufferAsync(long maxBufferSize);
+    public Task LoadIntoBufferAsync(CancellationToken cancellationToken);
+    public Task LoadIntoBufferAsync(long maxBufferSize, CancellationToken cancellationToken);
 }

API Usage

using var client = new HttpClient();
using var response = await client.GetAsync("https://example.com", HttpCompletionOption.ResponseHeadersRead);

using (var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(30))
{
    var maxBufferSize = 134217728L;
    await response.LoadIntoBufferAsync(maxBufferSize, timeoutCts.Token);
}

Alternative Designs

If the HttpContent has been created by HttpClient, it could internally track cancellation based on the token passed to SendAsync. However I don't think this is consistent with existing HttpContent methods like CopyToAsync.

Risks

None

@andrewhickman-aveva andrewhickman-aveva added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label May 24, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label May 24, 2024
@ManickaP
Copy link
Member

@andrewhickman-aveva what is your motivation for filing this API proposal? Do you have any concrete use-case in mind? Does the current state limits you / your project? How impactfull it is?

I don't have anything against this API, I'm just trying to understand the need for this and assign priority accordingly.

@ManickaP ManickaP added the needs-author-action An issue or pull request that requires more info or actions from the author. label May 27, 2024
@andrewhickman-aveva
Copy link
Author

andrewhickman-aveva commented May 27, 2024

@andrewhickman-aveva what is your motivation for filing this API proposal? Do you have any concrete use-case in mind? Does the current state limits you / your project? How impactfull it is?

I don't have anything against this API, I'm just trying to understand the need for this and assign priority accordingly.

This is motivated by a memory dump we received where a particular task was stuck calling this method, when it should have timed out.

The reason we're using LoadIntoBufferAsync in the first place is to enable better retry behaviour. We're using PolicyHttpMessageHandler for retries, timeouts and circuit breaking behaviour. However the call to LoadIntoBufferAsync inside HttpClient runs outside the handlers and so wouldn't be retried. To work around this we defined a custom handler which runs within the scope of the retry policy and calls LoadIntoBufferAsync manually.

@dotnet-policy-service dotnet-policy-service bot removed the needs-author-action An issue or pull request that requires more info or actions from the author. label May 27, 2024
@ManickaP ManickaP added needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration and removed untriaged New issue has not been triaged by the area owner needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration labels May 28, 2024
@ManickaP ManickaP added this to the 9.0.0 milestone May 28, 2024
@ManickaP
Copy link
Member

Triage: there were no concerns from our team about it. No reason not to expose this overloads. Putting in 9.0 time frame for the moment. I'll try to push it through API review. But as we don't have anyone else needing this, it might get push out 9.0.

@ManickaP ManickaP self-assigned this May 28, 2024
@ManickaP ManickaP added api-ready-for-review API is ready for review, it is NOT ready for implementation blocking Marks issues that we want to fast track in order to unblock other important work and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels May 30, 2024
@bartonjs
Copy link
Member

bartonjs commented Jun 13, 2024

Video

  • We asked if they should use a more complicated pattern of showing them as optional parameters and doing EditorBrowsable-Never on the existing overloads, and decided that pure overloads was more locally consistent.
 namespace System.Net.Http;
 
 public partial class HttpContent
 {
     public Task LoadIntoBufferAsync(CancellationToken cancellationToken);
     public Task LoadIntoBufferAsync(long maxBufferSize, CancellationToken cancellationToken);
 }

@bartonjs bartonjs added api-approved API was approved in API review, it can be implemented and removed blocking Marks issues that we want to fast track in order to unblock other important work api-ready-for-review API is ready for review, it is NOT ready for implementation labels Jun 13, 2024
@ManickaP ManickaP added the help wanted [up-for-grabs] Good issue for external contributors label Jun 17, 2024
@MihaZupan MihaZupan modified the milestones: 9.0.0, Future Jun 17, 2024
@ManickaP ManickaP removed their assignment Jun 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-approved API was approved in API review, it can be implemented area-System.Net.Http help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants