Developer knowledge network · moderated exchange

Społeczność UnreliableCode

Badania programistów, inżynieria wsteczna i społeczność programistów

Knowledge indexNa żywo
4Categories
919Threads
2.8KPosty
Tutorial

Processing High-Throughput Network Streams with System.IO.Pipelines (PipeReader / PipeWriter) [StackOverflow Architecture Guide]

channel_concurrency
Reactive & Pipeline
MEMBER
Rozpustnik: 156
Data dołączenia: Jan 2022
Posty: 10
Dzięki: 16
2 tygodnie temu · Aug 8, 2026 1:10 AM
#1

Why System.IO.Pipelines (the engine behind ASP.NET Core Kestrel) beats traditional Stream and byte[] buffers:

  • Memory Management: Automatically manages buffer pooling and zero-copy slicing.
  • Backpressure: PipeWriter pauses upstream producers when downstream consumers are busy.
  • Line/Packet Parsing:
CSHARP
while (true)
{
    ReadResult result = await reader.ReadAsync();
    ReadOnlySequence<byte> buffer = result.Buffer;
    while (TryReadLine(ref buffer, out ReadOnlySequence<byte> line))
    {
        ProcessLine(line);
    }
    reader.AdvanceTo(buffer.Start, buffer.End); // Tells pipeline which bytes were consumed!
    if (result.IsCompleted) break;
}
csharp_async_master
Async & Task Expert
MEMBER
Rozpustnik: 57
Data dołączenia: Mar 2019
Posty: 18
Dzięki: 40
2 tygodnie temu · Aug 8, 2026 5:15 AM
#2

System.IO.Pipelines is how Kestrel handles millions of HTTP requests per second with flat memory usage.

dotnet_runtime_architect
.NET Core Specialist
MEMBER
Rozpustnik: 103
Data dołączenia: Apr 2018
Posty: 40
Dzięki: 24
2 tygodnie temu · Aug 8, 2026 3:28 PM
#3

The AdvanceTo(consumed, examined) design solves the classic 'incomplete packet' problem cleanly.