-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Revert the output pipe in the DuplexStreamPipeAdapter #11601
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
826c788
Revert back to copying data to pipes
davidfowl e2c18db
Replace the output pipe only
davidfowl c9060f7
Don't complete the connection pipe in Http2FrameWriter
davidfowl c991817
Set a logger
davidfowl 87be120
Small nits
davidfowl 0ab532b
PR feedback and simplification
davidfowl 63bad10
Remove CTA
davidfowl 1891f35
Critical
jkotalik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
using System.IO; | ||
using System.IO.Pipelines; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal | ||
{ | ||
|
@@ -14,36 +15,114 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal | |
/// <typeparam name="TStream"></typeparam> | ||
internal class DuplexPipeStreamAdapter<TStream> : DuplexPipeStream, IDuplexPipe where TStream : Stream | ||
{ | ||
private readonly Pipe _output; | ||
private Task _outputTask; | ||
|
||
public DuplexPipeStreamAdapter(IDuplexPipe duplexPipe, Func<Stream, TStream> createStream) : | ||
this(duplexPipe, new StreamPipeReaderOptions(leaveOpen: true), new StreamPipeWriterOptions(leaveOpen: true), createStream) | ||
{ | ||
} | ||
|
||
public DuplexPipeStreamAdapter(IDuplexPipe duplexPipe, StreamPipeReaderOptions readerOptions, StreamPipeWriterOptions writerOptions, Func<Stream, TStream> createStream) : base(duplexPipe.Input, duplexPipe.Output) | ||
public DuplexPipeStreamAdapter(IDuplexPipe duplexPipe, StreamPipeReaderOptions readerOptions, StreamPipeWriterOptions writerOptions, Func<Stream, TStream> createStream) : | ||
base(duplexPipe.Input, duplexPipe.Output) | ||
{ | ||
Stream = createStream(this); | ||
|
||
var outputOptions = new PipeOptions(pool: writerOptions.Pool, | ||
readerScheduler: PipeScheduler.Inline, | ||
writerScheduler: PipeScheduler.Inline, | ||
pauseWriterThreshold: 1, | ||
resumeWriterThreshold: 1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably use the defaults here like we did before to reduce thrashing: internal PipeOptions AdaptedOutputPipeOptions => new PipeOptions
(
pool: MemoryPool,
readerScheduler: PipeScheduler.Inline,
writerScheduler: PipeScheduler.Inline,
pauseWriterThreshold: _context.ServiceContext.ServerOptions.Limits.MaxResponseBufferSize ?? 0,
resumeWriterThreshold: _context.ServiceContext.ServerOptions.Limits.MaxResponseBufferSize ?? 0,
useSynchronizationContext: false,
minimumSegmentSize: MemoryPool.GetMinimumSegmentSize()
); |
||
minimumSegmentSize: writerOptions.MinimumBufferSize, | ||
useSynchronizationContext: false); | ||
|
||
Input = PipeReader.Create(Stream, readerOptions); | ||
Output = PipeWriter.Create(Stream, writerOptions); | ||
|
||
// We're using a pipe here because the HTTP/2 stack in Kestrel currently makes assumptions | ||
// about when it is ok to write to the PipeWriter. This should be reverted back to PipeWriter.Create once | ||
// those patterns are fixed. | ||
_output = new Pipe(outputOptions); | ||
} | ||
|
||
public ILogger Log { get; set; } | ||
|
||
public TStream Stream { get; } | ||
|
||
public PipeReader Input { get; } | ||
|
||
public PipeWriter Output { get; } | ||
public PipeWriter Output | ||
{ | ||
get | ||
{ | ||
if (_outputTask == null) | ||
{ | ||
_outputTask = WriteOutputAsync(); | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
return _output.Writer; | ||
} | ||
} | ||
|
||
public override async ValueTask DisposeAsync() | ||
{ | ||
Input.Complete(); | ||
Output.Complete(); | ||
base.Dispose(disposing); | ||
_output.Writer.Complete(); | ||
|
||
if (_outputTask != null) | ||
{ | ||
// Wait for the output task to complete, this ensures that we've copied | ||
// the application data to the underlying stream | ||
await _outputTask; | ||
} | ||
} | ||
|
||
public override ValueTask DisposeAsync() | ||
private async Task WriteOutputAsync() | ||
{ | ||
Input.Complete(); | ||
Output.Complete(); | ||
return base.DisposeAsync(); | ||
try | ||
{ | ||
while (true) | ||
{ | ||
var result = await _output.Reader.ReadAsync(); | ||
var buffer = result.Buffer; | ||
|
||
try | ||
{ | ||
if (buffer.IsEmpty) | ||
davidfowl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (result.IsCompleted) | ||
{ | ||
break; | ||
} | ||
|
||
await Stream.FlushAsync(); | ||
} | ||
else if (buffer.IsSingleSegment) | ||
{ | ||
await Stream.WriteAsync(buffer.First); | ||
} | ||
else | ||
{ | ||
foreach (var memory in buffer) | ||
{ | ||
await Stream.WriteAsync(memory); | ||
} | ||
} | ||
} | ||
finally | ||
{ | ||
_output.Reader.AdvanceTo(buffer.End); | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log?.LogCritical(0, ex, $"{GetType().Name}.{nameof(WriteOutputAsync)}"); | ||
} | ||
finally | ||
{ | ||
_output.Reader.Complete(); | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍