You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add ability to perform zero byte reads in StreamPipeReader #37539
Bufferless reads are a technique used in a couple of places in ASP.NET Core to avoid renting memory from the memory pool if there's no data available on the socket; this helps when trying to reduce the memory usage for idle connections for applications that are trying to handle a large number of concurrent connections (like websocket based applications).
We'd like to use this for websocket applications to remove the need to allocate any memory (see #37122 for an example)
Proposed API
namespace System.Collections.Generic
{
public class StreamPipeReaderOptions
{
+ public bool WaitForData { get; set; }
}
WaitForData tells the PipeReader implementation that it should wait for data from the underlying stream before allocating memory. It relies on the stream supporting zero byte reads (which networking and SSL do)
Please don't couple the 2 APIs together. If the wrapping Stream doesn't support zero byte reads then it should throw (or return 0 I guess?). When it does though, like SSLStream and NetworkStream, this passthrough should just work (they are verified and have tests)
Please don't couple the 2 APIs together. If the wrapping Stream doesn't support zero byte reads then it should throw (or return 0 I guess?). When it does though, like SSLStream and NetworkStream, this passthrough should just work (they are verified and have tests)
What we heard was that it was not critical for Kestrel in 5.0, so a foolproof end-to-end usage is better. If critical, we can accept the API separately and demand good documentation that it expects non-standard Stream behavior.
It's not critical for Kestrel in 5.0 but that's not the point of this API. I don't mind adding the API to stream either but the purity argument being made doesn't change the API on StreamPipeReader, nor does it change what happens if the underlying Stream doesn't support it. This API is .NET Standard 2.0 so it runs in all the places (where there won't be a new WaitForDataAsync API).
For all you know, the StreamPipeReader could implement this by doing a big switch statement on the stream type and determine which ones support zero byte reads and throw if that isn't the case.
I'd like the 2 APIs decoupled in general because they don't need overlap.
Last time we said that zero byte reads are technically a violation of the Stream.Read() contract, but we believe enough streams support this already that it's better to change the documentation to call this out as something a stream must support. Therefore, we don't need a new API on Stream.
Activity
terrajobst commentedon Aug 11, 2020
Video
Stream.Read()contract, but granted many streams probably support it by accident.StreaminsteadSslStream,SocketListenerStream,NetworkStream, and other compression streams2 remaining items
davidfowl commentedon Aug 11, 2020
Please don't couple the 2 APIs together. If the wrapping Stream doesn't support zero byte reads then it should throw (or return 0 I guess?). When it does though, like SSLStream and NetworkStream, this passthrough should just work (they are verified and have tests)
scalablecory commentedon Aug 11, 2020
What we heard was that it was not critical for Kestrel in 5.0, so a foolproof end-to-end usage is better. If critical, we can accept the API separately and demand good documentation that it expects non-standard Stream behavior.
davidfowl commentedon Aug 11, 2020
It's not critical for Kestrel in 5.0 but that's not the point of this API. I don't mind adding the API to stream either but the purity argument being made doesn't change the API on StreamPipeReader, nor does it change what happens if the underlying Stream doesn't support it. This API is .NET Standard 2.0 so it runs in all the places (where there won't be a new WaitForDataAsync API).
For all you know, the StreamPipeReader could implement this by doing a big switch statement on the stream type and determine which ones support zero byte reads and throw if that isn't the case.
I'd like the 2 APIs decoupled in general because they don't need overlap.
davidfowl commentedon Mar 2, 2021
I've updated the proposal to add the Stream addition as well.
davidfowl commentedon Mar 4, 2021
@stephentoub I want to take this back to API review as is, without the Stream API.
terrajobst commentedon Mar 9, 2021
Video
Stream.Read()contract, but we believe enough streams support this already that it's better to change the documentation to call this out as something a stream must support. Therefore, we don't need a new API onStream.davidfowl commentedon Mar 10, 2021
Fixed the comment @terrajobst. I updated the API to match the fact that
StreamPipeReaderOptionsis immutable.