Skip to content

Commit d963e6a

Browse files
authoredAug 29, 2022
feat(body): make body::Sender and Body::channel private (#2970)
Closes #2962 BREAKING CHANGE: A channel body will be available in `hyper-util`.
1 parent 7a41da5 commit d963e6a

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed
 

‎src/body/body.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ enum Kind {
6161
/// [`Body::channel()`]: struct.Body.html#method.channel
6262
/// [`Sender::abort()`]: struct.Sender.html#method.abort
6363
#[must_use = "Sender does nothing unless sent on"]
64-
pub struct Sender {
64+
pub(crate) struct Sender {
6565
want_rx: watch::Receiver,
6666
data_tx: BodySender,
6767
trailers_tx: Option<TrailersSender>,
@@ -75,7 +75,8 @@ impl Recv {
7575
///
7676
/// Useful when wanting to stream chunks from another thread.
7777
#[inline]
78-
pub fn channel() -> (Sender, Recv) {
78+
#[allow(unused)]
79+
pub(crate) fn channel() -> (Sender, Recv) {
7980
Self::new_channel(DecodedLength::CHUNKED, /*wanter =*/ false)
8081
}
8182

@@ -289,7 +290,7 @@ impl fmt::Debug for Recv {
289290

290291
impl Sender {
291292
/// Check to see if this `Sender` can send more data.
292-
pub fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll<crate::Result<()>> {
293+
pub(crate) fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll<crate::Result<()>> {
293294
// Check if the receiver end has tried polling for the body yet
294295
ready!(self.poll_want(cx)?);
295296
self.data_tx
@@ -311,15 +312,17 @@ impl Sender {
311312
}
312313

313314
/// Send data on data channel when it is ready.
314-
pub async fn send_data(&mut self, chunk: Bytes) -> crate::Result<()> {
315+
#[allow(unused)]
316+
pub(crate) async fn send_data(&mut self, chunk: Bytes) -> crate::Result<()> {
315317
self.ready().await?;
316318
self.data_tx
317319
.try_send(Ok(chunk))
318320
.map_err(|_| crate::Error::new_closed())
319321
}
320322

321323
/// Send trailers on trailers channel.
322-
pub async fn send_trailers(&mut self, trailers: HeaderMap) -> crate::Result<()> {
324+
#[allow(unused)]
325+
pub(crate) async fn send_trailers(&mut self, trailers: HeaderMap) -> crate::Result<()> {
323326
let tx = match self.trailers_tx.take() {
324327
Some(tx) => tx,
325328
None => return Err(crate::Error::new_closed()),
@@ -339,14 +342,15 @@ impl Sender {
339342
/// This is mostly useful for when trying to send from some other thread
340343
/// that doesn't have an async context. If in an async context, prefer
341344
/// `send_data()` instead.
342-
pub fn try_send_data(&mut self, chunk: Bytes) -> Result<(), Bytes> {
345+
pub(crate) fn try_send_data(&mut self, chunk: Bytes) -> Result<(), Bytes> {
343346
self.data_tx
344347
.try_send(Ok(chunk))
345348
.map_err(|err| err.into_inner().expect("just sent Ok"))
346349
}
347350

348351
/// Aborts the body in an abnormal fashion.
349-
pub fn abort(self) {
352+
#[allow(unused)]
353+
pub(crate) fn abort(self) {
350354
let _ = self
351355
.data_tx
352356
// clone so the send works even if buffer is full

‎src/body/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ pub use http_body::Body as HttpBody;
2020
pub use http_body::SizeHint;
2121

2222
pub use self::aggregate::aggregate;
23-
pub use self::body::{Recv, Sender};
23+
pub use self::body::Recv;
24+
pub(crate) use self::body::Sender;
2425
pub(crate) use self::length::DecodedLength;
2526
pub use self::to_bytes::to_bytes;
2627

‎tests/client.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,7 @@ test! {
13251325
}
13261326

13271327
mod conn {
1328+
use std::error::Error;
13281329
use std::io::{self, Read, Write};
13291330
use std::net::{SocketAddr, TcpListener};
13301331
use std::pin::Pin;
@@ -1333,15 +1334,15 @@ mod conn {
13331334
use std::time::Duration;
13341335

13351336
use bytes::{Buf, Bytes};
1336-
use futures_channel::oneshot;
1337+
use futures_channel::{mpsc, oneshot};
13371338
use futures_util::future::{self, poll_fn, FutureExt, TryFutureExt};
1338-
use http_body_util::Empty;
1339-
use hyper::upgrade::OnUpgrade;
1339+
use http_body_util::{Empty, StreamBody};
13401340
use tokio::io::{AsyncRead, AsyncReadExt as _, AsyncWrite, AsyncWriteExt as _, ReadBuf};
13411341
use tokio::net::{TcpListener as TkTcpListener, TcpStream};
13421342

13431343
use hyper::body::HttpBody;
13441344
use hyper::client::conn;
1345+
use hyper::upgrade::OnUpgrade;
13451346
use hyper::{self, Method, Recv, Request, Response, StatusCode};
13461347

13471348
use super::{concat, s, support, tcp_connect, FutureHyperExt};
@@ -1524,17 +1525,23 @@ mod conn {
15241525

15251526
rt.spawn(conn.map_err(|e| panic!("conn error: {}", e)).map(|_| ()));
15261527

1527-
let (mut sender, body) = Recv::channel();
1528+
let (mut sender, recv) = mpsc::channel::<Result<Bytes, Box<dyn Error + Send + Sync>>>(0);
1529+
15281530
let sender = thread::spawn(move || {
1529-
sender.try_send_data("hello".into()).expect("try_send_data");
1531+
sender.try_send(Ok("hello".into())).expect("try_send_data");
15301532
support::runtime().block_on(rx).unwrap();
1531-
sender.abort();
1533+
1534+
// Aborts the body in an abnormal fashion.
1535+
let _ = sender.try_send(Err(Box::new(std::io::Error::new(
1536+
io::ErrorKind::Other,
1537+
"body write aborted",
1538+
))));
15321539
});
15331540

15341541
let req = Request::builder()
15351542
.method(Method::POST)
15361543
.uri("/")
1537-
.body(body)
1544+
.body(StreamBody::new(recv))
15381545
.unwrap();
15391546
let res = client.send_request(req);
15401547
rt.block_on(res).unwrap_err();
@@ -2111,7 +2118,7 @@ mod conn {
21112118
.http2_only(true)
21122119
.http2_keep_alive_interval(Duration::from_secs(1))
21132120
.http2_keep_alive_timeout(Duration::from_secs(1))
2114-
.handshake::<_, Recv>(io)
2121+
.handshake(io)
21152122
.await
21162123
.expect("http handshake");
21172124

@@ -2120,9 +2127,10 @@ mod conn {
21202127
});
21212128

21222129
// Use a channel to keep request stream open
2123-
let (_tx, body) = hyper::Recv::channel();
2124-
let req1 = http::Request::new(body);
2125-
let _resp = client.send_request(req1).await.expect("send_request");
2130+
let (_tx, recv) = mpsc::channel::<Result<Bytes, Box<dyn Error + Send + Sync>>>(0);
2131+
let req = http::Request::new(StreamBody::new(recv));
2132+
2133+
let _resp = client.send_request(req).await.expect("send_request");
21262134

21272135
// sleep longer than keepalive would trigger
21282136
tokio::time::sleep(Duration::from_secs(4)).await;

0 commit comments

Comments
 (0)
Please sign in to comment.