Skip to main content

Module streaming

Module streaming 

Source
Expand description

Streaming response body support.

This module provides helpers for streaming large response bodies with:

  • Memory-bounded buffering: Configurable chunk sizes to control memory usage
  • Cancel-aware streaming: Integration with asupersync Cx for graceful cancellation
  • File streaming: Efficient file streaming without loading entire files into memory
  • Backpressure: Works with asupersync’s checkpoint system

§Example

use fastapi_http::streaming::{FileStream, StreamConfig};
use fastapi_core::{Response, ResponseBody};

async fn stream_file(cx: &Cx) -> Response {
    let config = StreamConfig::default();
    let stream = FileStream::open("large_file.bin", cx.clone(), config).await?;
    Response::ok()
        .header("content-type", b"application/octet-stream".to_vec())
        .body(ResponseBody::stream(stream))
}

Structs§

CancelAwareStream
A stream that wraps another stream and respects cancellation via Cx.
ChunkedBytes
A stream that yields chunks from an in-memory buffer.
FileStream
A stream that reads a file in chunks.
StreamConfig
Configuration for streaming responses.

Enums§

StreamError
Error types for streaming operations.

Constants§

DEFAULT_CHUNK_SIZE
Default chunk size for streaming (64KB).
DEFAULT_MAX_BUFFER_SIZE
Default maximum buffer size (4MB).

Traits§

StreamingResponseExt
Extension trait for creating streaming response bodies.