Skip to content

Commit 3740cc8

Browse files
kxtseanmonstar
authored andcommitted
feat(client): deprecate client::conn::{Builder, handshake} (#3053)
client::conn::Builder and client:conn:handshake are deprecated as they are removed in 1.0. tower_client is updated so it does not use the deprecated API.
1 parent 253cc74 commit 3740cc8

File tree

7 files changed

+61
-8
lines changed

7 files changed

+61
-8
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ ffi = ["libc"]
112112
# enable 1.0 backports
113113
backports = []
114114

115+
# whether or not to display deprecation warnings
116+
deprecated = []
117+
115118
# internal features used in CI
116119
nightly = []
117120
__internal_happy_eyeballs_tests = []

examples/tower_client.rs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
#![deny(warnings)]
22

3-
use hyper::client::conn::Builder;
4-
use hyper::client::connect::HttpConnector;
5-
use hyper::client::service::Connect;
3+
use std::future::Future;
4+
use std::pin::Pin;
5+
use std::task::{Context, Poll};
6+
67
use hyper::service::Service;
7-
use hyper::{Body, Request};
8+
use hyper::{Body, Request, Response};
9+
use tokio::net::TcpStream;
810

911
#[tokio::main]
10-
async fn main() -> Result<(), Box<dyn std::error::Error>> {
12+
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
1113
pretty_env_logger::init();
1214

13-
let mut mk_svc = Connect::new(HttpConnector::new(), Builder::new());
14-
1515
let uri = "http://127.0.0.1:8080".parse::<http::Uri>()?;
1616

17-
let mut svc = mk_svc.call(uri.clone()).await?;
17+
let mut svc = Connector;
1818

1919
let body = Body::empty();
2020

@@ -25,3 +25,35 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2525

2626
Ok(())
2727
}
28+
29+
struct Connector;
30+
31+
impl Service<Request<Body>> for Connector {
32+
type Response = Response<Body>;
33+
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
34+
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
35+
36+
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> std::task::Poll<Result<(), Self::Error>> {
37+
Poll::Ready(Ok(()))
38+
}
39+
40+
fn call(&mut self, req: Request<Body>) -> Self::Future {
41+
Box::pin(async move {
42+
let host = req.uri().host().expect("no host in uri");
43+
let port = req.uri().port_u16().expect("no port in uri");
44+
45+
let stream = TcpStream::connect(format!("{}:{}", host, port)).await?;
46+
47+
let (mut sender, conn) = hyper::client::conn::http1::handshake(stream).await?;
48+
49+
tokio::task::spawn(async move {
50+
if let Err(err) = conn.await {
51+
println!("Connection error: {:?}", err);
52+
}
53+
});
54+
55+
let res = sender.send_request(req).await?;
56+
Ok(res)
57+
})
58+
}
59+
}

src/client/client.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,7 @@ impl Default for Builder {
917917
set_host: true,
918918
ver: Ver::Auto,
919919
},
920+
#[allow(deprecated)]
920921
conn_builder: conn::Builder::new(),
921922
pool_config: pool::Config {
922923
idle_timeout: Some(Duration::from_secs(90)),

src/client/conn.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,19 @@ pin_project! {
123123
///
124124
/// This is a shortcut for `Builder::new().handshake(io)`.
125125
/// See [`client::conn`](crate::client::conn) for more.
126+
#[cfg_attr(
127+
feature = "deprecated",
128+
deprecated(
129+
note = "This function will be replaced with `client::conn::http1::handshake` and `client::conn::http2::handshake` in 1.0, enable the \"backports\" feature to use them now."
130+
)
131+
)]
126132
pub async fn handshake<T>(
127133
io: T,
128134
) -> crate::Result<(SendRequest<crate::Body>, Connection<T, crate::Body>)>
129135
where
130136
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
131137
{
138+
#[allow(deprecated)]
132139
Builder::new().handshake(io).await
133140
}
134141

@@ -555,6 +562,12 @@ where
555562

556563
impl Builder {
557564
/// Creates a new connection builder.
565+
#[cfg_attr(
566+
feature = "deprecated",
567+
deprecated(
568+
note = "This type will be replaced with `client::conn::http1::Builder` and `client::conn::http2::Builder` in 1.0, enable the \"backports\" feature to use them now."
569+
)
570+
)]
558571
#[inline]
559572
pub fn new() -> Builder {
560573
Builder {

src/ffi/client.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ unsafe impl AsTaskType for hyper_clientconn {
9393
ffi_fn! {
9494
/// Creates a new set of HTTP clientconn options to be used in a handshake.
9595
fn hyper_clientconn_options_new() -> *mut hyper_clientconn_options {
96+
#[allow(deprecated)]
9697
let builder = conn::Builder::new();
9798

9899
Box::into_raw(Box::new(hyper_clientconn_options {

tests/client.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,6 +2181,7 @@ mod dispatch_impl {
21812181
}
21822182
}
21832183

2184+
#[allow(deprecated)]
21842185
mod conn {
21852186
use std::io::{self, Read, Write};
21862187
use std::net::{SocketAddr, TcpListener};
@@ -2246,6 +2247,7 @@ mod conn {
22462247
future::join(server, client).await;
22472248
}
22482249

2250+
#[deny(deprecated)]
22492251
#[cfg(feature = "backports")]
22502252
mod backports {
22512253
use super::*;

tests/server.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,6 +2536,7 @@ async fn http2_keep_alive_with_responsive_client() {
25362536
});
25372537

25382538
let tcp = connect_async(addr).await;
2539+
#[allow(deprecated)]
25392540
let (mut client, conn) = hyper::client::conn::Builder::new()
25402541
.http2_only(true)
25412542
.handshake::<_, Body>(tcp)

0 commit comments

Comments
 (0)