-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
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).
- Sockets - https://github.com/dotnet/aspnetcore/blob/278715734e957c8cfb3ee720510a79e8db1495b4/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs#L195 - Aside, it would also be great if we could make this cheaper 😄
- SignalR - https://github.com/dotnet/aspnetcore/blob/278715734e957c8cfb3ee720510a79e8db1495b4/src/SignalR/common/Http.Connections/src/Internal/Transports/WebSocketsServerTransport.cs#L147-L153
0 byte reads are supported by Sockets today and we take advantage of that to support the above scenario. We'd like to do the same for HTTPs based connections, so it would need to also work for SSLStream.
The typical layering looks like this for a WebSocket implemention (roughly):
WS.ReadAync(Buffer)
-> US.ReadAync(WSHeaderBuffer)
-> US.ReadAync(Buffer)
-> SSlStream.ReadAync(TlsFrameBuffer)
-> SSlStream.ReadAync(Buffer)
-> NetworkStream.ReadAync(Buffer)
-> Socket.ReadAsync(Buffer)
PS: I haven't tested it but if it just works then we should add test coverage so it doesn't regress.
cc: @stephentoub @wfurt