Closed
Description
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 |