-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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]: Event when a HttpResponseMessage is complete #86502
Comments
Tagging subscribers to this area: @dotnet/ncl Issue DetailsBackground and motivationHttpClient and its middleware friends are based around passing the request message down through a nested The response being returned from This becomes obvious in longer-running HTTP requests, such as gRPC streaming, where the response headers are returned almost immediately. Still, the response body periodically returns messages written by the server. There should be an event that libraries and apps can subscribe to that indicates when the request is complete. Potential users:
API ProposalAn event on namespace System.Net.Http;
public class HttpResponseMessage
{
// Option 1: Cancellation token
public CancellationToken Completed { get; }
// Option 2: Register callback
public OnCompleted(Action<HttpResponseMessage> callback);
} If the response is already complete when these APIs are used, then the registered callback is immediately invoked. The interesting discussion point here is: when is the overall request considered complete? I think a request should be considered complete when both of these events are true:
API Usagepublic void MyDelegatingHandler : DelegatingHandler
{
public long CompletedRequests { get; private set; }
public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request)
{
var response = await SendAsync(request);
response.Completed.Register(() => CompletedRequests++);
return response;
}
} Alternative DesignsNo response RisksNo response
|
Just on a side note, there's another alternative approach used in System.Net.Quic -- a Task property: QuicStream.ReadsClosed and QuicStream.WritesClosed, and you can register continuations instead of subscribing to an event |
|
My main concern with the proposal is with the code that would be responsible to watch this citeria. There are two options implementation-wise: to add a layer of indirection that wraps the response's There are several concerns with this:
|
Another, even more worrying concern:
We don't have an abstraction for "no content". There is |
Triage: Not needed for Metrics anymore. Might be useful in general - moving to Future. |
Background and motivation
HttpClient and its middleware friends are based around passing the request message down through a nested
HttpMessageHandler.SendAsync
pipeline and returning the response message back.The response being returned from
SendAsync
has historically been used to indicate that a request is complete. However, it only indicates the response header headers have been returned (apart from HttpClient and its option to read the response to completion before returning). This means that code that cares about the request being finished, such as counters, activities, and diagnostic source events, are reporting a HTTP request is finished even though the response body is still being read.This becomes obvious in longer-running HTTP requests, such as gRPC streaming, where the response headers are returned almost immediately. Still, the response body periodically returns messages written by the server.
There should be an event that libraries and apps can subscribe to that indicates when the request is complete.
Potential users:
SubchannelCallTracker
grpc/grpc-dotnet#2133API Proposal
An event on
HttpResponseMessage
that reports when the overall request is considered complete.API options:
CancellationToken
might not be the right thing. I can't think of a scenario where someone would use it with an async method call that takes a token. I suggest it because it is similar to ASP.NET Core's HttpContext.RequestAborted token.HttpResponseMessage.OnCompleted(Action callback)
may be preferable. It's like ASP.NET Core's HttpResponse.OnCompleted.When these APIs are used, the registered callback is invoked if the response is complete.
The interesting discussion point here is: when is the overall request considered complete?
I think a request should be considered complete when both of these events are true (or the
HttpResponseMessage
is explicitly disposed):HttpClient.SendAsync
orHttpMessageInvoker.SendAsync
.Edit: Potential problem with the idea above. The
HttpResponseMessage
returned for a request doesn't have to exit pipeline. A handler could decide to create a new one or retry the HTTP request which returns a new response. I added explicitly callingHttpResponseMessage.Dispose
as another way to indicate the response is complete, but that doesn't seem like a guaranteed fix because someone might not dispose the originalHttpResponseMessage
.API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: