use std::cmp; use std::fmt; #[cfg(all(feature = "server", feature = "runtime"))] use std::future::Future; use std::io::{self, IoSlice}; use std::marker::Unpin; use std::mem::MaybeUninit; #[cfg(all(feature = "server", feature = "runtime"))] use std::time::Duration;
use bytes::{Buf, BufMut, Bytes, BytesMut}; use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; #[cfg(all(feature = "server", feature = "runtime"))] use tokio::time::Instant; use tracing::{debug, trace};
/// The initial buffer size allocated before trying to read from IO. pub(crate) const INIT_BUFFER_SIZE: usize = 8192;
/// The minimum value that can be set to max buffer size. pub(crate) const MINIMUM_MAX_BUFFER_SIZE: usize = INIT_BUFFER_SIZE;
/// The default maximum read buffer size. If the buffer gets this big and /// a message is still not complete, a `TooLarge` error is triggered. // Note: if this changes, update server::conn::Http::max_buf_size docs. pub(crate) const DEFAULT_MAX_BUFFER_SIZE: usize = 8192 + 4096 * 100;
/// The maximum number of distinct `Buf`s to hold in a list before requiring /// a flush. Only affects when the buffer strategy is to queue buffers. /// /// Note that a flush can happen before reaching the maximum. This simply /// forces a flush if the queue gets this big. const MAX_BUF_LIST_BUFFERS: usize = 16;
pub(crate) fn set_write_strategy_flatten(&mutself) { // this should always be called only at construction time, // so this assert is here to catch myself
debug_assert!(self.write_buf.queue.bufs_cnt() == 0); self.write_buf.set_strategy(WriteStrategy::Flatten);
}
pub(crate) fn set_write_strategy_queue(&mutself) { // this should always be called only at construction time, // so this assert is here to catch myself
debug_assert!(self.write_buf.queue.bufs_cnt() == 0); self.write_buf.set_strategy(WriteStrategy::Queue);
}
/// Return the "allocated" available space, not the potential space /// that could be allocated in the future. fn read_buf_remaining_mut(&self) -> usize { self.read_buf.capacity() - self.read_buf.len()
}
/// Return whether we can append to the headers buffer. /// /// Reasons we can't: /// - The write buf is in queue mode, and some of the past body is still /// needing to be flushed. pub(crate) fn can_headers_buf(&self) -> bool {
!self.write_buf.queue.has_remaining()
}
pub(crate) fn poll_read_from_io(
&mutself,
cx: &mut task::Context<'_>,
) -> Poll<io::Result<usize>> { self.read_blocked = false; let next = self.read_buf_strategy.next(); ifself.read_buf_remaining_mut() < next { self.read_buf.reserve(next);
}
let dst = self.read_buf.chunk_mut(); let dst = unsafe { &mut *(dst as *mut _ as *mut [MaybeUninit<u8>]) }; letmut buf = ReadBuf::uninit(dst); match Pin::new(&mutself.io).poll_read(cx, &mut buf) {
Poll::Ready(Ok(_)) => { let n = buf.filled().len();
trace!("received {} bytes", n); unsafe { // Safety: we just read that many bytes into the // uninitialized part of the buffer, so this is okay. // @tokio pls give me back `poll_read_buf` thanks self.read_buf.advance_mut(n);
} self.read_buf_strategy.record(n);
Poll::Ready(Ok(n))
}
Poll::Pending => { self.read_blocked = true;
Poll::Pending
}
Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
}
}
const MAX_WRITEV_BUFS: usize = 64; loop { let n = { letmut iovs = [IoSlice::new(&[]); MAX_WRITEV_BUFS]; let len = self.write_buf.chunks_vectored(&mut iovs);
ready!(Pin::new(&mutself.io).poll_write_vectored(cx, &iovs[..len]))?
}; // TODO(eliza): we have to do this manually because // `poll_write_buf` doesn't exist in Tokio 0.3 yet...when // `poll_write_buf` comes back, the manual advance will need to leave! self.write_buf.advance(n);
debug!("flushed {} bytes", n); ifself.write_buf.remaining() == 0 { break;
} elseif n == 0 {
trace!( "write returned zero, but {} bytes remaining", self.write_buf.remaining()
); return Poll::Ready(Err(io::ErrorKind::WriteZero.into()));
}
}
Pin::new(&mutself.io).poll_flush(cx)
}
}
/// Specialized version of `flush` when strategy is Flatten. /// /// Since all buffered bytes are flattened into the single headers buffer, /// that skips some bookkeeping around using multiple buffers. fn poll_flush_flattened(&mutself, cx: &mut task::Context<'_>) -> Poll<io::Result<()>> { loop { let n = ready!(Pin::new(&mutself.io).poll_write(cx, self.write_buf.headers.chunk()))?;
debug!("flushed {} bytes", n); self.write_buf.headers.advance(n); ifself.write_buf.headers.remaining() == 0 { self.write_buf.headers.reset(); break;
} elseif n == 0 {
trace!( "write returned zero, but {} bytes remaining", self.write_buf.remaining()
); return Poll::Ready(Err(io::ErrorKind::WriteZero.into()));
}
}
Pin::new(&mutself.io).poll_flush(cx)
}
// The `B` is a `Buf`, we never project a pin to it impl<T: Unpin, B> Unpin for Buffered<T, B> {}
// TODO: This trait is old... at least rename to PollBytes or something... pub(crate) trait MemRead { fn read_mem(&mutself, cx: &mut task::Context<'_>, len: usize) -> Poll<io::Result<Bytes>>;
}
impl<T, B> MemRead for Buffered<T, B> where
T: AsyncRead + AsyncWrite + Unpin,
B: Buf,
{ fn read_mem(&mutself, cx: &mut task::Context<'_>, len: usize) -> Poll<io::Result<Bytes>> { if !self.read_buf.is_empty() { let n = std::cmp::min(len, self.read_buf.len());
Poll::Ready(Ok(self.read_buf.split_to(n).freeze()))
} else { let n = ready!(self.poll_read_from_io(cx))?;
Poll::Ready(Ok(self.read_buf.split_to(::std::cmp::min(len, n)).freeze()))
}
}
}
fn record(&mutself, bytes_read: usize) { match *self {
ReadStrategy::Adaptive { refmut decrease_now, refmut next,
max,
..
} => { if bytes_read >= *next {
*next = cmp::min(incr_power_of_two(*next), max);
*decrease_now = false;
} else { let decr_to = prev_power_of_two(*next); if bytes_read < decr_to { if *decrease_now {
*next = cmp::max(decr_to, INIT_BUFFER_SIZE);
*decrease_now = false;
} else { // Decreasing is a two "record" process.
*decrease_now = true;
}
} else { // A read within the current range should cancel // a potential decrease, since we just saw proof // that we still need this size.
*decrease_now = false;
}
}
} #[cfg(feature = "client")]
ReadStrategy::Exact(_) => (),
}
}
}
fn prev_power_of_two(n: usize) -> usize { // Only way this shift can underflow is if n is less than 4. // (Which would means `usize::MAX >> 64` and underflowed!)
debug_assert!(n >= 4);
(::std::usize::MAX >> (n.leading_zeros() + 2)) + 1
}
impl Cursor<Vec<u8>> { /// If we've advanced the position a bit in this cursor, and wish to /// extend the underlying vector, we may wish to unshift the "read" bytes /// off, and move everything else over. fn maybe_unshift(&mutself, additional: usize) { ifself.pos == 0 { // nothing to do return;
}
// an internal buffer to collect writes before flushes pub(super) struct WriteBuf<B> { /// Re-usable buffer that holds message headers
headers: Cursor<Vec<u8>>,
max_buf_size: usize, /// Deque of user buffers if strategy is Queue
queue: BufList<B>,
strategy: WriteStrategy,
}
#[tokio::test] #[ignore] asyncfn iobuf_write_empty_slice() { // TODO(eliza): can i have writev back pls T_T // // First, let's just check that the Mock would normally return an // // error on an unexpected write, even if the buffer is empty... // let mut mock = Mock::new().build(); // futures_util::future::poll_fn(|cx| { // Pin::new(&mut mock).poll_write_buf(cx, &mut Cursor::new(&[])) // }) // .await // .expect_err("should be a broken pipe");
// // underlying io will return the logic error upon write, // // so we are testing that the io_buf does not trigger a write // // when there is nothing to flush // let mock = Mock::new().build(); // let mut io_buf = Buffered::<_, Cursor<Vec<u8>>>::new(mock); // io_buf.flush().await.expect("should short-circuit flush");
}
let _ = pretty_env_logger::try_init(); let mock = Mock::new() // Split over multiple reads will read all of it
.read(b"HTTP/1.1 200 OK\r\n")
.read(b"Server: hyper\r\n") // missing last line ending
.wait(Duration::from_secs(1))
.build();
let rem1 = write_buf.remaining(); let cap = write_buf.headers.bytes.capacity();
// but when this would go over capacity, don't copy the old bytes
write_buf.buffer(Cursor::new(vec![b'X'; cap]));
assert_eq!(write_buf.remaining(), cap + rem1);
assert_eq!(write_buf.headers.pos, 0);
}
#[tokio::test] asyncfn write_buf_queue_disable_auto() { let _ = pretty_env_logger::try_init();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.