Table of Contents

Method StreamBuffered

Namespace
Open.Database.Extensions
Assembly
Open.Database.Extensions.Core.dll

StreamBuffered(TimeSpan, CancellationToken)

Reads the rows lazily through a bounded channel that buffers up to 1,024 rows ahead of your loop, stopping the read when you stop pulling once the buffer is full.

IAsyncEnumerable<T> StreamBuffered(TimeSpan stallTimeout, CancellationToken cancellationToken = default)

Parameters

stallTimeout TimeSpan

The longest you may go without pulling the next row once the buffer is full. Required; there is no default. InfiniteTimeSpan (or a span over 24 days) accepts an unbounded hold.

cancellationToken CancellationToken

Cancels the read; the enumeration then throws OperationCanceledException.

Returns

IAsyncEnumerable<T>

The rows. The enumeration can be taken once.

Remarks

The buffer is a ceiling, not a target, and it counts rows, not bytes: wide rows (blobs) cost memory, for each stream running at once. A consumer that keeps up leaves it nearly empty; a larger buffer lets the read ride out a slow consumer instead of waiting on every row. A result that fits is read whole, the later result sets included, and the connection released before you have pulled every row. Breaking out early never throws for rows or result sets you did not reach. If the buffer is full and you go stallTimeout without pulling the next row, the read stops, the command is cancelled, the connection this library opened is closed, and your next pull throws TimeoutException. Rows still waiting in the buffer when the stall trips are discarded, not delivered. Dispose the enumeration (await foreach does). The rows are read with Read() on your thread unless the query asked for AsyncReads(), so a slow database read blocks your thread for its wait; a cancel cannot interrupt a read the provider is blocked in, so the command's CommandTimeout bounds it, and a cancel while you hold a row is seen at your next pull. A timer checks the stall timeout, so an expiry is found up to about a quarter of the timeout (at least 10 ms) after the timeout has passed, never before it.

Three ways to read the rows as they arrive; pick by what the connection and your loop need:

MethodBuffer; threads; connection held until; stall protection
StreamNone: no row is read ahead; your loop's thread (with AsyncReads(), the thread that finishes each read), with no producer task and no channel; until you pull past the last row; yes, on every pull.
StreamBufferedUp to bufferSize rows (default 1,024; it counts rows, not bytes); a producer task and your loop; until the read ends, or, once the buffer is full, until the stall timeout passes without a pull (never with an infinite timeout); yes, once the buffer is full.
ToPooledStreamUnbounded: pooled chunks, the whole result if you are slow; a producer task and your loop; until the read ends, however slowly you read; none.

Which to use: a fast loop, Stream; slow or async work per row, StreamBuffered; to free the connection as soon as possible, ToPooledStream.

All three are lazy: calling one does nothing until the first MoveNextAsync. A slow or hung database read is bounded by the command's CommandTimeout and your cancellation token on every one of them; the stall timeout is only about a slow consumer. A cancel or a dispose stops the read promptly only as far as the provider honors the cancellation.

StreamBuffered(TimeSpan, int, CancellationToken)

Reads the rows lazily through a bounded channel that buffers up to bufferSize rows (it counts rows, not bytes); a size of 0 reads as Stream does, with no channel.

IAsyncEnumerable<T> StreamBuffered(TimeSpan stallTimeout, int bufferSize, CancellationToken cancellationToken = default)

Parameters

stallTimeout TimeSpan

The longest you may go without pulling the next row once the buffer is full. Required; there is no default.

bufferSize int

The most rows the read runs ahead of your loop; 0 reads as Stream does (no channel, nothing ahead), and a negative size throws.

cancellationToken CancellationToken

Cancels the read; the enumeration then throws OperationCanceledException.

Returns

IAsyncEnumerable<T>

The rows. The enumeration can be taken once.

Exceptions

ArgumentOutOfRangeException

bufferSize is negative.