Buffer short responses (2) (#531)

* Response: build reader at construction time

* Remove unwrapping/rewrapping DeadlineStream

* Let into_reader() provide Sync + 'static

* Prebuffer Vec use known capacity

Co-authored-by: Martin Algesten <martin@algesten.se>
This commit is contained in:
Jacob Hoffman-Andrews
2022-07-10 02:12:50 -07:00
committed by GitHub
parent 9908c446d6
commit f14fc45179
2 changed files with 44 additions and 21 deletions

View File

@@ -50,7 +50,7 @@ impl<T: ReadWrite + ?Sized> ReadWrite for Box<T> {
// after the provided deadline, and sets timeouts on the underlying
// TcpStream to ensure read() doesn't block beyond the deadline.
// When the From trait is used to turn a DeadlineStream back into a
// Stream (by PoolReturningRead), the timeouts are removed.
// Stream (by PoolReturnRead), the timeouts are removed.
pub(crate) struct DeadlineStream {
stream: Stream,
deadline: Option<Instant>,
@@ -60,6 +60,10 @@ impl DeadlineStream {
pub(crate) fn new(stream: Stream, deadline: Option<Instant>) -> Self {
DeadlineStream { stream, deadline }
}
pub(crate) fn inner_ref(&self) -> &Stream {
&self.stream
}
}
impl From<DeadlineStream> for Stream {
@@ -184,6 +188,10 @@ impl Stream {
stream
}
pub(crate) fn buffer(&self) -> &[u8] {
self.inner.buffer()
}
fn from_tcp_stream(t: TcpStream) -> Stream {
Stream::logged_create(Stream {
inner: BufReader::new(Box::new(t)),