Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.
Merged
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Remove async
  • Loading branch information
jkotalik committed Nov 16, 2018
commit 92d02dcb4bcc9af18cac6ad5207473a3363c40cf
92 changes: 46 additions & 46 deletions src/Microsoft.AspNetCore.Http/StreamPipeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,62 @@ private void Cancel()
InternalTokenSource.Cancel();
}

private async ValueTask<FlushResult> FlushAsyncInternal(CancellationToken cancellationToken = default)
private ValueTask<FlushResult> FlushAsyncInternal(CancellationToken cancellationToken = default)
{
CancellationTokenRegistration reg = new CancellationTokenRegistration();
if (cancellationToken.CanBeCanceled)
{
reg = cancellationToken.Register(state => ((StreamPipeWriter)state).Cancel(), this);
}

return WriteAndFlushAsync(reg, cancellationToken);
}

private async ValueTask<FlushResult> WriteAndFlushAsync(CancellationTokenRegistration reg, CancellationToken token)
{
// Write all completed segments and whatever remains in the current segment
// and flush the result.
using (reg)
{
try
{
return await WriteAndFlushAsync();
if (_completedSegments != null && _completedSegments.Count > 0)
{
var count = _completedSegments.Count;
for (var i = 0; i < count; i++)
{
var segment = _completedSegments[0];
#if NETCOREAPP2_2
await _writingStream.WriteAsync(segment.Buffer.Slice(0, segment.Length), InternalTokenSource.Token);
#elif NETSTANDARD2_0
MemoryMarshal.TryGetArray<byte>(segment.Buffer, out var arraySegment);
await _writingStream.WriteAsync(arraySegment.Array, 0, segment.Length, InternalTokenSource.Token);
#else
#error Target frameworks need to be updated.
#endif
_bytesWritten -= segment.Length;
segment.Return();
_completedSegments.RemoveAt(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For next PR: this is slow and we don't have a benchmark that exercises this code path at all.

}
}

if (!_currentSegment.IsEmpty)
{
#if NETCOREAPP2_2
await _writingStream.WriteAsync(_currentSegment.Slice(0, _position), InternalTokenSource.Token);
#elif NETSTANDARD2_0
MemoryMarshal.TryGetArray<byte>(_currentSegment, out var arraySegment);
await _writingStream.WriteAsync(arraySegment.Array, 0, _position, InternalTokenSource.Token);
#else
#error Target frameworks need to be updated.
#endif
_bytesWritten -= _position;
_position = 0;
}

await _writingStream.FlushAsync(_internalTokenSource.Token);

return new FlushResult(isCanceled: false, IsCompletedOrThrow());
}
catch (OperationCanceledException)
{
Expand All @@ -166,7 +209,7 @@ private async ValueTask<FlushResult> FlushAsyncInternal(CancellationToken cancel
_internalTokenSource = null;
}

if (cancellationToken.IsCancellationRequested)
if (token.IsCancellationRequested)
{
throw;
}
Expand All @@ -177,49 +220,6 @@ private async ValueTask<FlushResult> FlushAsyncInternal(CancellationToken cancel
}
}

private async ValueTask<FlushResult> WriteAndFlushAsync()
{
// Write all completed segments and whatever remains in the current segment
// and flush the result.
if (_completedSegments != null && _completedSegments.Count > 0)
{
var count = _completedSegments.Count;
for (var i = 0; i < count; i++)
{
var segment = _completedSegments[0];
#if NETCOREAPP2_2
await _writingStream.WriteAsync(segment.Buffer.Slice(0, segment.Length), InternalTokenSource.Token);
#elif NETSTANDARD2_0
MemoryMarshal.TryGetArray<byte>(segment.Buffer, out var arraySegment);
await _writingStream.WriteAsync(arraySegment.Array, 0, segment.Length, InternalTokenSource.Token);
#else
#error Target frameworks need to be updated.
#endif
_bytesWritten -= segment.Length;
segment.Return();
_completedSegments.RemoveAt(0);
}
}

if (!_currentSegment.IsEmpty)
{
#if NETCOREAPP2_2
await _writingStream.WriteAsync(_currentSegment.Slice(0, _position), InternalTokenSource.Token);
#elif NETSTANDARD2_0
MemoryMarshal.TryGetArray<byte>(_currentSegment, out var arraySegment);
await _writingStream.WriteAsync(arraySegment.Array, 0, _position, InternalTokenSource.Token);
#else
#error Target frameworks need to be updated.
#endif
_bytesWritten -= _position;
_position = 0;
}

await _writingStream.FlushAsync(_internalTokenSource.Token);

return new FlushResult(isCanceled: false, IsCompletedOrThrow());
}

private void EnsureCapacity(int sizeHint)
{
// This does the Right Thing. It only subtracts _position from the current segment length if it's non-null.
Expand Down