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.
This commit is contained in:
John Howard
2023-02-01 08:58:22 -08:00
parent f7b9f734d3
commit ebbedf567f
4 changed files with 33 additions and 29 deletions
+24 -23
View File
@@ -9,8 +9,8 @@ use super::{AsyncIOBuf, CopyBuffer};
enum TransferState<B, SR, SW> {
Running(CopyBuffer<B, SR, SW>),
ShuttingDown,
Done,
ShuttingDown(u64),
Done(u64),
}
fn transfer<B, SL, SR>(
@@ -18,7 +18,7 @@ fn transfer<B, SL, SR>(
state: &mut TransferState<B, SL, SR>,
r: &mut <CopyBuffer<B, SL, SR> as AsyncIOBuf>::StreamR,
w: &mut <CopyBuffer<B, SL, SR> as AsyncIOBuf>::StreamW,
) -> Poll<Result<()>>
) -> Poll<Result<u64>>
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<B, SL, SR>(
state: &mut TransferState<B, SR, SL>, // reverse
r: &mut <CopyBuffer<B, SL, SR> as AsyncIOBuf>::StreamW,
w: &mut <CopyBuffer<B, SL, SR> as AsyncIOBuf>::StreamR,
) -> Poll<Result<()>>
) -> Poll<Result<u64>>
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<B, SL, SR>: AsyncIOBuf,
CopyBuffer<B, SR, SL>: AsyncIOBuf,
{
type Output = Result<()>;
type Output = Result<(u64, u64)>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
// 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, SR, SW>(
b: &mut <CopyBuffer<B, SR, SW> as AsyncIOBuf>::StreamW,
a_to_b_buf: CopyBuffer<B, SR, SW>,
b_to_a_buf: CopyBuffer<B, SW, SR>,
) -> Result<()>
) -> Result<(u64, u64)>
where
B: Unpin,
SR: AsyncRead + AsyncWrite + Unpin,
+7 -4
View File
@@ -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<B, SR, SW> {
pub(crate) need_flush: bool,
pub(crate) pos: usize,
pub(crate) cap: usize,
pub(crate) amt: u64,
pub(crate) buf: B,
_marker: PhantomData<SR>,
__marker: PhantomData<SW>,
@@ -23,6 +24,7 @@ impl<B, SR, SW> CopyBuffer<B, SR, SW> {
need_flush: false,
pos: 0,
cap: 0,
amt: 0,
buf,
_marker: PhantomData,
__marker: PhantomData,
@@ -55,7 +57,7 @@ where
cx: &mut Context<'_>,
r: &mut <CopyBuffer<B, SR, SW> as AsyncIOBuf>::StreamR,
w: &mut <CopyBuffer<B, SR, SW> as AsyncIOBuf>::StreamW,
) -> Poll<Result<()>> {
) -> Poll<Result<u64>> {
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));
}
}
}
+1 -1
View File
@@ -35,7 +35,7 @@ where
}
/// Copy data bidirectionally between two streams with userspace buffer.
pub async fn bidi_copy<A, B>(a: &mut A, b: &mut B) -> Result<()>
pub async fn bidi_copy<A, B>(a: &mut A, b: &mut B) -> Result<(u64, u64)>
where
A: AsyncRead + AsyncWrite + Unpin,
B: AsyncRead + AsyncWrite + Unpin,
+1 -1
View File
@@ -161,7 +161,7 @@ mod tokio_net {
}
/// Copy data bidirectionally between two streams with pipe.
pub async fn bidi_zero_copy<A, B>(a: &mut A, b: &mut B) -> Result<()>
pub async fn bidi_zero_copy<A, B>(a: &mut A, b: &mut B) -> Result<(u64, u64)>
where
A: AsyncRawIO + Unpin,
B: AsyncRawIO + Unpin,