Developer knowledge network · moderated exchange

Zajednica UnreliableCode

Zajednica za istraživanje, obrnuti inženjering i programiranje programera

Knowledge indexŽivjeti
4Categories
919Threads
2.8KPostovi
Tutorial

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

channel_concurrency
Reactive & Pipeline
MEMBER
Rep: 156
Datum pridruživanja: Jan 2022
Postovi: 10
Hvala: 16
2 prije tjedana · 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
Rep: 57
Datum pridruživanja: Mar 2019
Postovi: 18
Hvala: 40
2 prije tjedana · 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
Rep: 103
Datum pridruživanja: Apr 2018
Postovi: 40
Hvala: 24
2 prije tjedana · Aug 8, 2026 3:28 PM
#3

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