Skip to content

Commit

Permalink
feat(client): deprecate client::conn::{Builder, handshake} (#3053)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kxt authored and seanmonstar committed Mar 2, 2023
1 parent 99b76f7 commit 6f8be87
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ ffi = ["libc"]
# 1.0 compatibile client API
backports = []

# whether or not to display deprecation warnings
deprecated = []

# internal features used in CI
nightly = []
__internal_happy_eyeballs_tests = []
Expand Down
48 changes: 40 additions & 8 deletions examples/tower_client.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#![deny(warnings)]

use hyper::client::conn::Builder;
use hyper::client::connect::HttpConnector;
use hyper::client::service::Connect;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

use hyper::service::Service;
use hyper::{Body, Request};
use hyper::{Body, Request, Response};
use tokio::net::TcpStream;

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

let mut mk_svc = Connect::new(HttpConnector::new(), Builder::new());

let uri = "http://127.0.0.1:8080".parse::<http::Uri>()?;

let mut svc = mk_svc.call(uri.clone()).await?;
let mut svc = Connector;

let body = Body::empty();

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

Ok(())
}

struct Connector;

impl Service<Request<Body>> for Connector {
type Response = Response<Body>;
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;

fn poll_ready(&mut self, _cx: &mut Context<'_>) -> std::task::Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

fn call(&mut self, req: Request<Body>) -> Self::Future {
Box::pin(async move {
let host = req.uri().host().expect("no host in uri");
let port = req.uri().port_u16().expect("no port in uri");

let stream = TcpStream::connect(format!("{}:{}", host, port)).await?;

let (mut sender, conn) = hyper::client::conn::http1::handshake(stream).await?;

tokio::task::spawn(async move {
if let Err(err) = conn.await {
println!("Connection error: {:?}", err);
}
});

let res = sender.send_request(req).await?;
Ok(res)
})
}
}
1 change: 1 addition & 0 deletions src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,7 @@ impl Default for Builder {
set_host: true,
ver: Ver::Auto,
},
#[allow(deprecated)]
conn_builder: conn::Builder::new(),
pool_config: pool::Config {
idle_timeout: Some(Duration::from_secs(90)),
Expand Down
13 changes: 13 additions & 0 deletions src/client/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,19 @@ pin_project! {
///
/// This is a shortcut for `Builder::new().handshake(io)`.
/// See [`client::conn`](crate::client::conn) for more.
#[cfg_attr(
feature = "deprecated",
deprecated(
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."
)
)]
pub async fn handshake<T>(
io: T,
) -> crate::Result<(SendRequest<crate::Body>, Connection<T, crate::Body>)>
where
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
#[allow(deprecated)]
Builder::new().handshake(io).await
}

Expand Down Expand Up @@ -555,6 +562,12 @@ where

impl Builder {
/// Creates a new connection builder.
#[cfg_attr(
feature = "deprecated",
deprecated(
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."
)
)]
#[inline]
pub fn new() -> Builder {
Builder {
Expand Down
1 change: 1 addition & 0 deletions src/ffi/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ unsafe impl AsTaskType for hyper_clientconn {
ffi_fn! {
/// Creates a new set of HTTP clientconn options to be used in a handshake.
fn hyper_clientconn_options_new() -> *mut hyper_clientconn_options {
#[allow(deprecated)]
let builder = conn::Builder::new();

Box::into_raw(Box::new(hyper_clientconn_options {
Expand Down
2 changes: 2 additions & 0 deletions tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2181,6 +2181,7 @@ mod dispatch_impl {
}
}

#[allow(deprecated)]
mod conn {
use std::io::{self, Read, Write};
use std::net::{SocketAddr, TcpListener};
Expand Down Expand Up @@ -2246,6 +2247,7 @@ mod conn {
future::join(server, client).await;
}

#[deny(deprecated)]
#[cfg(feature = "backports")]
mod backports {
use super::*;
Expand Down
1 change: 1 addition & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2536,6 +2536,7 @@ async fn http2_keep_alive_with_responsive_client() {
});

let tcp = connect_async(addr).await;
#[allow(deprecated)]
let (mut client, conn) = hyper::client::conn::Builder::new()
.http2_only(true)
.handshake::<_, Body>(tcp)
Expand Down

0 comments on commit 6f8be87

Please sign in to comment.