From ebbedf567feeedbca5d1db31565dae111711d3dc Mon Sep 17 00:00:00 2001 From: John Howard Date: Wed, 1 Feb 2023 08:58:22 -0800 Subject: [PATCH] io: return bytes read/write in copy This matches the behavior of std and Tokio copy methods, and allows doing things like reporting telemetry based on how much data is copied, for example. The implementation follows `tokio`'s bidi copy implementation. --- realm_io/src/bidi_copy.rs | 47 ++++++++++++++++++++------------------- realm_io/src/buf.rs | 11 +++++---- realm_io/src/mem_copy.rs | 2 +- realm_io/src/zero_copy.rs | 2 +- 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/realm_io/src/bidi_copy.rs b/realm_io/src/bidi_copy.rs index 721b991..a8771a6 100644 --- a/realm_io/src/bidi_copy.rs +++ b/realm_io/src/bidi_copy.rs @@ -9,8 +9,8 @@ use super::{AsyncIOBuf, CopyBuffer}; enum TransferState { Running(CopyBuffer), - ShuttingDown, - Done, + ShuttingDown(u64), + Done(u64), } fn transfer( @@ -18,7 +18,7 @@ fn transfer( state: &mut TransferState, r: &mut as AsyncIOBuf>::StreamR, w: &mut as AsyncIOBuf>::StreamW, -) -> Poll> +) -> Poll> where B: Unpin, SL: AsyncRead + AsyncWrite + Unpin, @@ -29,16 +29,16 @@ where loop { match state { TransferState::Running(buf) => { - ready!(buf.poll_copy(cx, r, w))?; + let count = ready!(buf.poll_copy(cx, r, w))?; - *state = TransferState::ShuttingDown; + *state = TransferState::ShuttingDown(count); } - TransferState::ShuttingDown => { + TransferState::ShuttingDown(count) => { ready!(Pin::new(&mut *w).poll_shutdown(cx))?; - *state = TransferState::Done; + *state = TransferState::Done(*count); } - TransferState::Done => return Poll::Ready(Ok(())), + TransferState::Done(count) => return Poll::Ready(Ok(*count)), } } } @@ -48,7 +48,7 @@ fn transfer2( state: &mut TransferState, // reverse r: &mut as AsyncIOBuf>::StreamW, w: &mut as AsyncIOBuf>::StreamR, -) -> Poll> +) -> Poll> where B: Unpin, SL: AsyncRead + AsyncWrite + Unpin, @@ -62,16 +62,16 @@ where loop { match state { TransferState::Running(buf) => { - ready!(buf.poll_copy(cx, r, w))?; + let count = ready!(buf.poll_copy(cx, r, w))?; - *state = TransferState::ShuttingDown; + *state = TransferState::ShuttingDown(count); } - TransferState::ShuttingDown => { + TransferState::ShuttingDown(count) => { ready!(Pin::new(&mut *w).poll_shutdown(cx))?; - *state = TransferState::Done; + *state = TransferState::Done(*count); } - TransferState::Done => return Poll::Ready(Ok(())), + TransferState::Done(count) => return Poll::Ready(Ok(*count)), } } } @@ -98,7 +98,7 @@ where CopyBuffer: AsyncIOBuf, CopyBuffer: AsyncIOBuf, { - type Output = Result<()>; + type Output = Result<(u64, u64)>; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { // Unpack self into mut refs to each field to avoid borrow check issues. @@ -110,18 +110,19 @@ where // graceful shutdown #[cfg(not(feature = "brutal-shutdown"))] { - ready!(a_to_b); - ready!(b_to_a); - Poll::Ready(Ok(())) + let a_to_b = ready!(a_to_b); + let b_to_a = ready!(b_to_a); + Poll::Ready(Ok((a_to_b, b_to_a))) } // brutal shutdown #[cfg(feature = "brutal-shutdown")] { - if a_to_b.is_ready() || b_to_a.is_ready() { - Poll::Ready(Ok(())) - } else { - Poll::Pending + match (a_to_b, b_to_a) { + (Poll::Ready(a), Poll::Ready(b)) => Poll::Ready(Ok((a, b))), + (Poll::Pending, Poll::Ready(b)) => Poll::Ready(Ok((0, b))), + (Poll::Ready(a), Poll::Pending) => Poll::Ready(Ok((a, 0))), + _ => Poll::Pending, } } } @@ -133,7 +134,7 @@ pub async fn bidi_copy_buf( b: &mut as AsyncIOBuf>::StreamW, a_to_b_buf: CopyBuffer, b_to_a_buf: CopyBuffer, -) -> Result<()> +) -> Result<(u64, u64)> where B: Unpin, SR: AsyncRead + AsyncWrite + Unpin, diff --git a/realm_io/src/buf.rs b/realm_io/src/buf.rs index af3a532..9170e20 100644 --- a/realm_io/src/buf.rs +++ b/realm_io/src/buf.rs @@ -1,6 +1,6 @@ -use std::io::{Result, ErrorKind}; -use std::task::{Context, Poll, ready}; +use std::io::{ErrorKind, Result}; use std::marker::PhantomData; +use std::task::{ready, Context, Poll}; use tokio::io::{AsyncRead, AsyncWrite}; @@ -10,6 +10,7 @@ pub struct CopyBuffer { pub(crate) need_flush: bool, pub(crate) pos: usize, pub(crate) cap: usize, + pub(crate) amt: u64, pub(crate) buf: B, _marker: PhantomData, __marker: PhantomData, @@ -23,6 +24,7 @@ impl CopyBuffer { need_flush: false, pos: 0, cap: 0, + amt: 0, buf, _marker: PhantomData, __marker: PhantomData, @@ -55,7 +57,7 @@ where cx: &mut Context<'_>, r: &mut as AsyncIOBuf>::StreamR, w: &mut as AsyncIOBuf>::StreamW, - ) -> Poll> { + ) -> Poll> { loop { // If our buffer is empty, then we need to read some data to // continue. @@ -94,6 +96,7 @@ where return Poll::Ready(Err(ErrorKind::WriteZero.into())); } else { self.pos += i; + self.amt += i as u64; self.need_flush = true; } } @@ -107,7 +110,7 @@ where // data and finish the transfer. if self.pos == self.cap && self.read_done { ready!(self.poll_flush_buf(cx, w))?; - return Poll::Ready(Ok(())); + return Poll::Ready(Ok(self.amt)); } } } diff --git a/realm_io/src/mem_copy.rs b/realm_io/src/mem_copy.rs index 50094cc..b8b0811 100644 --- a/realm_io/src/mem_copy.rs +++ b/realm_io/src/mem_copy.rs @@ -35,7 +35,7 @@ where } /// Copy data bidirectionally between two streams with userspace buffer. -pub async fn bidi_copy(a: &mut A, b: &mut B) -> Result<()> +pub async fn bidi_copy(a: &mut A, b: &mut B) -> Result<(u64, u64)> where A: AsyncRead + AsyncWrite + Unpin, B: AsyncRead + AsyncWrite + Unpin, diff --git a/realm_io/src/zero_copy.rs b/realm_io/src/zero_copy.rs index 63c4312..63e25b6 100644 --- a/realm_io/src/zero_copy.rs +++ b/realm_io/src/zero_copy.rs @@ -161,7 +161,7 @@ mod tokio_net { } /// Copy data bidirectionally between two streams with pipe. -pub async fn bidi_zero_copy(a: &mut A, b: &mut B) -> Result<()> +pub async fn bidi_zero_copy(a: &mut A, b: &mut B) -> Result<(u64, u64)> where A: AsyncRawIO + Unpin, B: AsyncRawIO + Unpin,