stream-body
An HttpBody implementation with efficient streaming support for the Rust HTTP library hyper.
Motivation
The existing Body type in hyper uses Bytes
as streaming chunk. Hence, a lot of buffer allocation and de-allocation happen during the real-time large data streaming because of the Bytes type.
Therefore, StreamBody
comes to tackle this kind of situation. The StreamBody
implements HttpBody and uses &[u8]
slice as the streaming chunk, so it is possible to use the same buffer without allocating a new one; hence it overcomes any allocation/de-allocation overhead.
Also, the channel() method in hyper Body returns
a pair of a Sender and a Body.
Here, the Sender accepts Bytes as a data chunk which again
creates allocation/de-allocation overhead.
To solve this, StreamBody
has a method named StreamBody::channel()
which returns a pair of an AsyncWrite and the StreamBody
itself. As the AsyncWrite accepts &[u8]
instead of Bytes, there will
be no allocation/de-allocation overhead.
Usage
First add this to your Cargo.toml:
[]
= "0.1"
An example on handling a large file:
use ;
use ;
use ;
use StreamBody;
use File;
use ;
async
async
Contributing
Your PRs and stars are always welcome.