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

Reuse cancellation token sources in the Kestrel #30390

Closed
davidfowl opened this issue Feb 23, 2021 · 3 comments · Fixed by #31466
Closed

Reuse cancellation token sources in the Kestrel #30390

davidfowl opened this issue Feb 23, 2021 · 3 comments · Fixed by #31466
Assignees
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions feature-kestrel Perf
Milestone

Comments

@davidfowl
Copy link
Member

davidfowl commented Feb 23, 2021

Today we use the request aborted token when writing large responses in various places. We can reuse these tokens across requests if they haven't been cancelled. We can take advantage of dotnet/runtime#48492 when it becomes available.

Here's what a simple application that uses the request aborted token looks like:

var host = new HostBuilder().ConfigureWebHostDefaults(host =>
{
    host.Configure(app =>
    {
        Task Hello(HttpContext context)
        {
            return context.Response.WriteAsync("Hello World", context.RequestAborted);
        }

        app.UseRouting();

        app.UseEndpoints(routes =>
        {
            routes.MapGet("/", Hello);
        });
    });
}).Build();


await host.RunAsync();

Here's the allocation profile after ~10K requests

Type Allocations Bytes Average Size (Bytes)
- System.Threading.CancellationTokenSource 10,250 656,000 64
- System.SByte[] 1,500 312,000 208
- Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection<Context> 125 97,000 776
- System.IO.Pipelines.Pipe 250 96,000 384
- Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketAwaitableEventArgs 250 88,000 352
- Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestHeaders 125 67,000 536
- Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseHeaders 125 49,000 392
- System.IO.Pipelines.BufferSegmentStack.SegmentAsValueType[] 250 38,000 152
- System.String 504 35,570 70.58
- Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketConnection 125 31,000 248
- System.Object 1,125 27,000 24
@ghost
Copy link

ghost commented Feb 24, 2021

Thanks for contacting us.
We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

@BrennanConroy BrennanConroy added the blocked The work on this issue is blocked due to some dependency label Feb 24, 2021
@davidfowl davidfowl self-assigned this Mar 23, 2021
@davidfowl davidfowl added feature-kestrel and removed blocked The work on this issue is blocked due to some dependency labels Apr 2, 2021
@kshyju
Copy link

kshyju commented Apr 3, 2021

@davidfowl

I am going through the PRs in Continuous Improvement project as an optimization learning exercise.

Here's the allocation profile after ~10K requests

Could you please share what tooling was used to generate the above data ? Thank You!

@davidfowl
Copy link
Member Author

Could you please share what tooling was used to generate the above data ? Thank You!

Visual Studio 2019's new allocation profiler https://docs.microsoft.com/en-us/visualstudio/profiling/dotnet-alloc-tool?view=vs-2019. You can copy as markdown 😄

@BrennanConroy BrennanConroy moved this from To do to Done in Continuous Improvement Apr 3, 2021
@ghost ghost locked as resolved and limited conversation to collaborators May 3, 2021
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions feature-kestrel Perf
Development

Successfully merging a pull request may close this issue.

4 participants