Skip to content

Add ability to perform zero byte reads in StreamPipeReader #37539

Closed

Description

@davidfowl

Background and Motivation

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)

Usage Examples

var reader = PipeReader.Create(stream, new StreamPipeReaderOptions { WaitForData = true })

Numbers in SSLStream

Activity

  1. added
    api-suggestion
    on Jun 6, 2020
  2. added
    untriaged
    on Jun 6, 2020
  3. added and removed
    api-suggestion
    on Jun 8, 2020
  4. added
    api-ready-for-review
    and removed on Jul 6, 2020
  5. added this to the Future milestone on Jul 8, 2020
  6. removed
    untriaged
    on Jul 8, 2020
  7. terrajobst commented on Aug 11, 2020

    @terrajobst
    Contributor

    Video

    • Zero byte reads are technically a violation of the Stream.Read() contract, but granted many streams probably support it by accident.
    • We should have an API on Stream instead
    • We should validate the SslStream, SocketListenerStream, NetworkStream, and other compression streams
    • This should be investigated and the API proposal should be updated
    namespace System.IO.Pipelines
    {
        public class StreamPipeReaderOptions
        {
            public bool WaitForData { get; set; }
        }
    }
    namespace System.IO
    {
        public partial class Stream
        {
            public virtual Task WaitForDataAsync() => return Task.CompletedTask;
        }
    }
  8. added
    api-needs-work
    and removed
    api-ready-for-review
    on Aug 11, 2020
  9. removed this from the Future milestone on Aug 11, 2020
  10. 2 remaining items

  11. davidfowl commented on Aug 11, 2020

    @davidfowl
    MemberAuthor

    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)

  12. scalablecory commented on Aug 11, 2020

    @scalablecory
    Contributor

    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.

  13. davidfowl commented on Aug 11, 2020

    @davidfowl
    MemberAuthor

    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.

  14. davidfowl commented on Mar 2, 2021

    @davidfowl
    MemberAuthor

    I've updated the proposal to add the Stream addition as well.

  15. ghost added
    in-pr
    on Mar 4, 2021
  16. added
    api-ready-for-review
    and removed
    api-needs-work
    on Mar 4, 2021
  17. davidfowl commented on Mar 4, 2021

    @davidfowl
    MemberAuthor

    @stephentoub I want to take this back to API review as is, without the Stream API.

  18. added
    api-approved
    and removed
    api-ready-for-review
    on Mar 9, 2021
  19. terrajobst commented on Mar 9, 2021

    @terrajobst
    Contributor

    Video

    • 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.
    namespace System.IO.Pipelines
    {
        public class StreamPipeReaderOptions
        {
            public StreamPipeReaderOptions(MemoryPool<byte>? pool, int bufferSize, int minimumReadSize, bool leaveOpen);
            public StreamPipeReaderOptions(MemoryPool<byte>? pool = null, int bufferSize = -1, int minimumReadSize = -1, bool leaveOpen = false, bool useZeroByteReads = false);
            public bool UseZeroByteReads { get; }
        }
    }
  20. davidfowl commented on Mar 10, 2021

    @davidfowl
    MemberAuthor

    Fixed the comment @terrajobst. I updated the API to match the fact that StreamPipeReaderOptions is immutable.

  21. ghost removed
    in-pr
    on Mar 11, 2021
  22. ghost locked as resolved and limited conversation to collaborators on Apr 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedarea-System.IO.Pipelines

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions