Table of Contents

Method Stream

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

Stream(TimeSpan, CancellationToken)

Reads the rows one at a time off the open reader, lazily, as you pull them: on your own thread, with nothing read ahead.

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

Parameters

stallTimeout TimeSpan

The longest you may go without pulling the next row. Required; there is no default. InfiniteTimeSpan (or a span over 24 days) accepts an unbounded hold. It does not limit the whole call: use a token from CancellationTokenSource.CancelAfter for that.

cancellationToken CancellationToken

Cancels the read; the enumeration then throws OperationCanceledException.

Returns

IAsyncEnumerable<T>

The rows, in the order the reader returns them. The enumeration can be taken once.

Remarks

While you pull rows, this holds the reader and the connection open, and each row is read when you pull it, on your own thread: there is no producer task and no channel. That is not the best way to read rows: to read all of them use ToArrayAsync or ToListAsync; to read while the query runs use StreamBuffered or ToPooledStream. To read the rows as a list or an array, use ToListAsync(CancellationToken) or ToArrayAsync(CancellationToken): they release the connection before the task completes. To read while the query runs, use ToPooledBufferStream(CancellationToken), which releases it when the read ends. If 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. 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.

Exceptions

ArgumentOutOfRangeException

stallTimeout is zero or negative and not InfiniteTimeSpan.

InvalidOperationException

The query is a default value, or a second enumeration is taken.