Allow Body to have a borrowed reader
- Dominant language
- Rust
- Stars
- 209
- Forks
- 91
- PR merge metrics
- No merged PRs in 30d
Description
Thinking about the complexity in async-h1 around cloning the readers, it occurred to me that most of it would go away if Request could have a borrowed body reader.
- Currently async-h1 needs to clone the body reader (wrapping it with `Arc>`) so it can give one clone to the handler, and keep another to discard it if the handler doesn't read it. Giving a borrow to the handler avoids having to clone: once the handler is done the borrow is cleared, so we can discard data from the original reader)
- async-h1 also needs to clone the TCP connection, so it can give one clone to the body reader while keeping another to handle next requests. With this, the body reader can borrow the TCP connection, instead of needing an owned clone.
- The body being owned means user code can keep using it even after the response is sent (for example by sending it to another task), which is a logic error (and i'm not entirely sure whether it can corrupt the stream?) If it's borrowed the lifetime prevents that. IMO the reader having a lifetime is "natural": it shouldn't be valid after sending the response.
cc @jbr
It'd be something like this:
```rust
pub struct Body<'a> {
reader: &'a mut dyn AsyncRead + Unpin + Send + Sync,
...
}
pub struct Request<'a> {
body: Body<'a>,
...
}
```
Contributor guide
Assessment
This issue has not been assessed yet.