Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make send_file example streaming #3432

Closed
seanmonstar opened this issue Nov 20, 2023 · 3 comments
Closed

Make send_file example streaming #3432

seanmonstar opened this issue Nov 20, 2023 · 3 comments
Labels
A-docs Area: documentation. E-easy Effort: easy. A task that would be a great starting point for a new contributor.

Comments

@seanmonstar
Copy link
Member

The send_file example currently reads the entire file into memory. It would be better to stream the file, to show how to do so in case of large files.

  • Instead of using tokio::fs::read(), we can use `tokio::fs::File::open()``.
    • Then, the File can be wrapped in a tokio_util::io::ReaderStream.
    • That stream can be converted into a body with http_body_util::StreamBody.
  • But the "not found" body is not a stream, it is still a Full<Bytes>. So, to be able to return both, the final body type needs to a http_body_util::combinators::BoxBody<Bytes, std::io::Error>. You can use http_body_util::BodyExt::boxed() to create one.
@seanmonstar seanmonstar added E-easy Effort: easy. A task that would be a great starting point for a new contributor. A-docs Area: documentation. labels Nov 20, 2023
@acedogblast
Copy link
Contributor

How would we add tokio_util as a new dependency?
I get this error when building the example.

@seanmonstar
Copy link
Member Author

You can add tokio-util to the [dev-dependencies] in the root Cargo.toml.

@seanmonstar
Copy link
Member Author

Done in #3433.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation. E-easy Effort: easy. A task that would be a great starting point for a new contributor.
Projects
None yet
Development

No branches or pull requests

2 participants