stream-transfer-limit
stream-transfer-limit applies cumulative byte limits to fallible
futures::Streams.
It is meant for cases where an HTTP middleware or fixed body limit is not quite the right abstraction: for example, when the allowed transfer size is chosen after routing, authentication, configuration, or account lookup.
use ;
use ;
Behavior
- The stream may produce exactly the configured limit.
- The first chunk that makes the cumulative byte count greater than the limit
returns
TransferLimitError::LimitExceeded. - After a limit error, the wrapper terminates and does not keep polling the inner stream.
- The default byte counter is
usize. If the cumulative count cannot fit in the selected counter type, the wrapper returnsTransferLimitError::CounterOverflowand terminates. - Inner stream errors are preserved as
TransferLimitError::Inner. - Progress callbacks receive cumulative bytes after every successful chunk read from the inner stream, including the chunk that crosses the limit.
APIs
For new code, construct a TransferLimit and wrap the stream:
use stream;
use TransferLimit;
usize avoids unnecessary conversions for ordinary in-memory or platform-sized
limits. For very large streams, choose a wider counter explicitly:
use stream;
use TransferLimit;
The crate implements counters for usize, u64, and u128.
Development
The repository includes a Nix flake for a reproducible development shell:
If you use direnv, allow the checked-in .envrc once:
The development shell provides the pinned Rust toolchain, rustfmt, clippy,
rust-analyzer, cargo helper tools, Docker client, and act.
Run the local CI pipeline:
Run the GitHub Actions workflow locally with act:
Why not tower-http?
tower_http::limit::RequestBodyLimitLayer and http_body_util::Limited are
good choices when the limit is known at the HTTP-body layer. This crate is for
code that already has a futures::Stream and needs to choose the limit closer to
business logic, such as per-tenant transfer limits.