-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Add PipeWriter overloads to Json #101461
Add PipeWriter overloads to Json #101461
Changes from 12 commits
cb407b9
7512f26
4efef1f
5772e3f
94c4be4
04f53d3
9448b38
eb2c628
dde0db0
2c18119
530c0c0
95cb59c
ad097e9
2e0d69d
d617b86
6a6d91b
5f622c8
cdcc77e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Buffers; | ||
using System.IO; | ||
using System.IO.Pipelines; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace System.Text.Json.Serialization | ||
{ | ||
// Common interface to help de-dupe code for different types that can do async serialization (Stream and PipeWriter) | ||
internal interface IAsyncSerializationBufferWriterContext : IDisposable | ||
{ | ||
int FlushThreshold { get; } | ||
|
||
ValueTask FlushAsync(CancellationToken cancellationToken); | ||
|
||
public IBufferWriter<byte> BufferWriter { get; } | ||
} | ||
|
||
internal readonly struct AsyncSerializationStreamContext : IAsyncSerializationBufferWriterContext | ||
{ | ||
private readonly Stream _stream; | ||
private readonly JsonSerializerOptions _options; | ||
private readonly PooledByteBufferWriter _bufferWriter; | ||
|
||
public AsyncSerializationStreamContext(Stream stream, JsonSerializerOptions options) | ||
{ | ||
_stream = stream; | ||
_options = options; | ||
_bufferWriter = new PooledByteBufferWriter(_options.DefaultBufferSize); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public async ValueTask FlushAsync(CancellationToken cancellationToken) | ||
{ | ||
await _bufferWriter.WriteToStreamAsync(_stream, cancellationToken).ConfigureAwait(false); | ||
_bufferWriter.Clear(); | ||
} | ||
|
||
public int FlushThreshold => (int)(_options.DefaultBufferSize * JsonSerializer.FlushThreshold); | ||
|
||
public IBufferWriter<byte> BufferWriter => _bufferWriter; | ||
|
||
public void Dispose() | ||
{ | ||
_bufferWriter.Dispose(); | ||
} | ||
} | ||
|
||
internal readonly struct AsyncSerializationPipeContext : IAsyncSerializationBufferWriterContext | ||
{ | ||
private readonly PipeWriter _pipe; | ||
|
||
public AsyncSerializationPipeContext(PipeWriter pipe) | ||
{ | ||
_pipe = pipe; | ||
} | ||
|
||
public int FlushThreshold => (int)((4 * PipeOptions.Default.MinimumSegmentSize) * JsonSerializer.FlushThreshold); | ||
|
||
public IBufferWriter<byte> BufferWriter => _pipe; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public async ValueTask FlushAsync(CancellationToken cancellationToken) | ||
{ | ||
FlushResult result = await _pipe.FlushAsync(cancellationToken).ConfigureAwait(false); | ||
if (result.IsCanceled || result.IsCompleted) | ||
{ | ||
if (result.IsCanceled) | ||
{ | ||
ThrowHelper.ThrowOperationCanceledException_PipeWriteCanceled(); | ||
} | ||
|
||
ThrowHelper.ThrowOperationCanceledException_PipeWriteCompleted(); | ||
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. Do we really want to throw in this case? I think this is going to end up throwing a lot of unwanted exceptions. For normal HTTP/1.1 response bodies, this just means that the client has closed the connection. 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. I think you are right @halter73. Let's remove this exception in a follow up PR. |
||
} | ||
} | ||
eiriktsarpalis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public void Dispose() { } | ||
} | ||
} |
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.
What do these properties do, out of curiosity?
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.
This property makes the assembly get included in the transport package for aspnetcore. Aspnetcore includes everything from that transport package in their shared framework.
Given that you move this assembly inbox into Microsoft.NETCore.App, this change is correct.