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(client): backport split client conn modules #3063

Closed
wants to merge 3 commits into from
Closed
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
6 changes: 3 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ jobs:

include:
- rust: stable
features: "--features full"
features: "--features full,backports"
- rust: beta
features: "--features full"
features: "--features full,backports"
- rust: nightly
features: "--features full,nightly"
features: "--features full,nightly,backports"
benches: true

runs-on: ${{ matrix.os }}
Expand Down
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ tcp = [
# C-API support (currently unstable (no semver))
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 @@ -906,6 +906,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
18 changes: 18 additions & 0 deletions src/client/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
//! # }
//! ```

#[cfg(all(feature = "backports", feature = "http1"))]
pub mod http1;
#[cfg(all(feature = "backports", feature = "http2"))]
pub mod http2;

use std::error::Error as StdError;
use std::fmt;
#[cfg(not(all(feature = "http1", feature = "http2")))]
Expand Down Expand Up @@ -118,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 @@ -550,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
Loading