tower-rs / tower-http

HTTP specific Tower utilities.
710 stars 165 forks source link

Use sendfile syscall in ServeFile #381

Open YuzhongHuangCS opened 1 year ago

YuzhongHuangCS commented 1 year ago

Feature Request

Motivation

Currently, tower_http::services::ServeFile will read content of the file to the memory (in chunks if file is large), then send it to net socket, which involves 2 copies.

Linux/BSD support sendfile system call, which directly transfer data from file descriptor to net socket, with zero copy in user space, and support arbitrary large files.

Proposal

There is a sendfile wrapper available in Rust, but it require direct access to the net socket, which is not exposed in tower or axum framework.

If you could give me hints on how to find/expose the net socket I am happy to implement it myself.

Alternatives

  1. Use current 2 copy implementation in tower_http::services::ServeFile
jplatte commented 1 year ago

We can't really do anything here until hyper supports sendfile¹:

This likely also requires changes to the http_body crate.

¹ while tower-http isn't strongly tied to hyper, it's by far the most-used server for serving HTTP services using tower. It would probably not be worth it to implement this in a way that only works for some other server impl, even if there was one supporting sendfile somehow, which I doubt.