Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: impl AsyncWrite for BodySender #452

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion crates/core/src/http/body/channel.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt;
use std::io::{Error as IoError, ErrorKind, Result as IoResult};
use std::pin::Pin;
use std::task::{Context, Poll};

use bytes::Bytes;
Expand Down Expand Up @@ -33,12 +34,25 @@ impl BodySender {
.map_err(|e| IoError::new(ErrorKind::Other, format!("failed to poll ready: {}", e)))
}

/// Returns whether this channel is closed without needing a context.
pub fn is_closed(&self) -> bool {
self.data_tx.is_closed()
}
/// Closes this channel from the sender side, preventing any new messages.
pub fn close(&mut self) {
self.data_tx.close_channel();
}
/// Disconnects this sender from the channel, closing it if there are no more senders left.
pub fn disconnect(&mut self) {
self.data_tx.disconnect();
}

async fn ready(&mut self) -> IoResult<()> {
futures_util::future::poll_fn(|cx| self.poll_ready(cx)).await
}

/// Send data on data channel when it is ready.
pub async fn send_data(&mut self, chunk: impl Into<Bytes>) -> IoResult<()> {
pub async fn send_data(&mut self, chunk: impl Into<Bytes> + Send) -> IoResult<()> {
self.ready().await?;
self.data_tx
.try_send(Ok(chunk.into()))
Expand All @@ -65,6 +79,74 @@ impl BodySender {
}
}

impl futures_util::AsyncWrite for BodySender {
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<IoResult<usize>> {
match self.data_tx.poll_ready(cx) {
Poll::Ready(Ok(())) => {
let data: Bytes = Bytes::from(buf.to_vec());
let len = buf.len();
Poll::Ready(
self.data_tx
.try_send(Ok(data))
.map(|_| len)
.map_err(|e| IoError::new(ErrorKind::Other, format!("failed to send data: {}", e))),
)
}
Poll::Ready(Err(e)) => Poll::Ready(Err(IoError::new(
ErrorKind::Other,
format!("failed to poll ready: {}", e),
))),
Poll::Pending => Poll::Pending,
}
}

fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<IoResult<()>> {
Poll::Ready(Ok(()))
}

fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<IoResult<()>> {
if self.data_tx.is_closed() {
Poll::Ready(Ok(()))
} else {
Poll::Pending
}
}
}

impl tokio::io::AsyncWrite for BodySender {
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<IoResult<usize>> {
match self.data_tx.poll_ready(cx) {
Poll::Ready(Ok(())) => {
let data: Bytes = Bytes::from(buf.to_vec());
let len = buf.len();
Poll::Ready(
self.data_tx
.try_send(Ok(data))
.map(|_| len)
.map_err(|e| IoError::new(ErrorKind::Other, format!("failed to send data: {}", e))),
)
}
Poll::Ready(Err(e)) => Poll::Ready(Err(IoError::new(
ErrorKind::Other,
format!("failed to poll ready: {}", e),
))),
Poll::Pending => Poll::Pending,
}
}

fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<IoResult<()>> {
Poll::Ready(Ok(()))
}

fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<IoResult<()>> {
if self.data_tx.is_closed() {
Poll::Ready(Ok(()))
} else {
Poll::Pending
}
}
}

impl fmt::Debug for BodySender {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut builder = f.debug_tuple("BodySender");
Expand Down
3 changes: 2 additions & 1 deletion crates/core/src/writing/seek.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::cmp;
use std::io::SeekFrom;
use std::time::SystemTime;

use headers::*;
use tokio::io::{AsyncRead, AsyncSeek, AsyncSeekExt, SeekFrom};
use tokio::io::{AsyncRead, AsyncSeek, AsyncSeekExt};
use tokio_util::io::ReaderStream;

use crate::http::header::{IF_NONE_MATCH, RANGE};
Expand Down
Loading