Skip to content

Commit a1c5d62

Browse files
committed
Merge in 'release/6.0' changes
2 parents d503019 + 832ccdc commit a1c5d62

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

src/Servers/HttpSys/src/HttpSysOptions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public class HttpSysOptions
3535
/// </summary>
3636
public HttpSysOptions()
3737
{
38+
const string EnableKernelResponseBufferingSwitch = "Microsoft.AspNetCore.Server.HttpSys.EnableKernelResponseBuffering";
39+
EnableKernelResponseBuffering = AppContext.TryGetSwitch(EnableKernelResponseBufferingSwitch, out var enabled) && enabled;
3840
}
3941

4042
/// <summary>
@@ -109,6 +111,15 @@ public string? RequestQueueName
109111
/// </summary>
110112
public bool ThrowWriteExceptions { get; set; }
111113

114+
/// <summary>
115+
/// Enable buffering of response data in the Kernel.
116+
/// It should be used by an application doing synchronous I/O or by an application doing asynchronous I/O with
117+
/// no more than one outstanding write at a time, and can significantly improve throughput over high-latency connections.
118+
/// Applications that use asynchronous I/O and that may have more than one send outstanding at a time should not use this flag.
119+
/// Enabling this can results in higher CPU and memory usage by Http.Sys.
120+
/// </summary>
121+
internal bool EnableKernelResponseBuffering { get; set; } // internal via app-context in pre-8.0
122+
112123
/// <summary>
113124
/// Gets or sets the maximum number of concurrent connections to accept, -1 for infinite, or null to
114125
/// use the machine wide setting from the registry. The default value is null.

src/Servers/HttpSys/src/RequestProcessing/Response.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,6 @@ actual HTTP header will be generated by the application and sent as entity body.
280280
// This will give us more control of the bytes that hit the wire, including encodings, HTTP 1.0, etc..
281281
// It may also be faster to do this work in managed code and then pass down only one buffer.
282282
// What would we loose by bypassing HttpSendHttpResponse?
283-
//
284-
// TODO: Consider using the HTTP_SEND_RESPONSE_FLAG_BUFFER_DATA flag for most/all responses rather than just Opaque.
285283
internal unsafe uint SendHeaders(HttpApiTypes.HTTP_DATA_CHUNK[]? dataChunks,
286284
ResponseStreamAsyncResult? asyncResult,
287285
HttpApiTypes.HTTP_FLAGS flags,

src/Servers/HttpSys/src/RequestProcessing/ResponseBody.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ internal RequestContext RequestContext
4242

4343
internal bool ThrowWriteExceptions => RequestContext.Server.Options.ThrowWriteExceptions;
4444

45+
internal bool EnableKernelResponseBuffering => RequestContext.Server.Options.EnableKernelResponseBuffering;
46+
4547
internal bool IsDisposed => _disposed;
4648

4749
public override bool CanSeek
@@ -466,6 +468,13 @@ private HttpApiTypes.HTTP_FLAGS ComputeLeftToWrite(long writeCount, bool endOfRe
466468
{
467469
flags |= HttpApiTypes.HTTP_FLAGS.HTTP_SEND_RESPONSE_FLAG_MORE_DATA;
468470
}
471+
if (EnableKernelResponseBuffering)
472+
{
473+
// "When this flag is set, it should also be used consistently in calls to the HttpSendResponseEntityBody function."
474+
// so: make sure we add it in *all* scenarios where it applies - our "close" could be at the end of a bunch
475+
// of buffered chunks
476+
flags |= HttpApiTypes.HTTP_FLAGS.HTTP_SEND_RESPONSE_FLAG_BUFFER_DATA;
477+
}
469478

470479
// Update _leftToWrite now so we can queue up additional async writes.
471480
if (_leftToWrite > 0)

src/Servers/HttpSys/test/FunctionalTests/ResponseBodyTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,41 @@ public async Task ResponseBody_WriteNoHeaders_SetsChunked()
133133
}
134134
}
135135

136+
[ConditionalTheory]
137+
[InlineData(true)]
138+
[InlineData(false)]
139+
public async Task ResponseBody_WriteNoHeaders_SetsChunked_LargeBody(bool enableKernelBuffering)
140+
{
141+
const int WriteSize = 1024 * 1024;
142+
const int NumWrites = 32;
143+
144+
string address;
145+
using (Utilities.CreateHttpServer(
146+
baseAddress: out address,
147+
configureOptions: options => { options.EnableKernelResponseBuffering = enableKernelBuffering; },
148+
app: async httpContext =>
149+
{
150+
httpContext.Features.Get<IHttpBodyControlFeature>().AllowSynchronousIO = true;
151+
for (int i = 0; i < NumWrites - 1; i++)
152+
{
153+
httpContext.Response.Body.Write(new byte[WriteSize], 0, WriteSize);
154+
}
155+
await httpContext.Response.Body.WriteAsync(new byte[WriteSize], 0, WriteSize);
156+
}))
157+
{
158+
var response = await SendRequestAsync(address);
159+
Assert.Equal(200, (int)response.StatusCode);
160+
Assert.Equal(new Version(1, 1), response.Version);
161+
IEnumerable<string> ignored;
162+
Assert.False(response.Content.Headers.TryGetValues("content-length", out ignored), "Content-Length");
163+
Assert.True(response.Headers.TransferEncodingChunked.HasValue, "Chunked");
164+
165+
var bytes = await response.Content.ReadAsByteArrayAsync();
166+
Assert.Equal(WriteSize * NumWrites, bytes.Length);
167+
Assert.True(bytes.All(b => b == 0));
168+
}
169+
}
170+
136171
[ConditionalFact]
137172
public async Task ResponseBody_WriteNoHeadersAndFlush_DefaultsToChunked()
138173
{

0 commit comments

Comments
 (0)