Developer knowledge network · moderated exchange

Сообщество UnreliableCode

Сообщество разработчиков, обратного проектирования и кодирования

Tutorial

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

channel_concurrency
Reactive & Pipeline
MEMBER
Представитель: 156
Дата присоединения: Jan 2022
Сообщения: 10
Спасибо: 16
2 недель назад · 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
Представитель: 57
Дата присоединения: Mar 2019
Сообщения: 18
Спасибо: 40
2 недель назад · 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
Представитель: 103
Дата присоединения: Apr 2018
Сообщения: 40
Спасибо: 24
2 недель назад · Aug 8, 2026 3:28 PM
#3

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