From 889fa2d87252108eb7668b8bf034ffcc30985117 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Fri, 12 Aug 2022 13:54:45 -0700 Subject: [PATCH] feat(client): remove `hyper::client::server` (#2940) BREAKING CHANGE: Tower `Service` utilities will exist in `hyper-util`. --- Cargo.toml | 5 --- examples/tower_client.rs | 59 -------------------------- src/client/mod.rs | 1 - src/client/service.rs | 89 ---------------------------------------- src/service/make.rs | 39 ------------------ src/service/mod.rs | 5 --- 6 files changed, 198 deletions(-) delete mode 100644 examples/tower_client.rs delete mode 100644 src/client/service.rs delete mode 100644 src/service/make.rs diff --git a/Cargo.toml b/Cargo.toml index f3538a31c2..e91f9080ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -181,11 +181,6 @@ name = "state" path = "examples/state.rs" required-features = ["full"] -[[example]] -name = "tower_client" -path = "examples/tower_client.rs" -required-features = ["full"] - [[example]] name = "tower_server" path = "examples/tower_server.rs" diff --git a/examples/tower_client.rs b/examples/tower_client.rs deleted file mode 100644 index 81b7488f61..0000000000 --- a/examples/tower_client.rs +++ /dev/null @@ -1,59 +0,0 @@ -#![deny(warnings)] - -use std::future::Future; -use std::pin::Pin; -use std::task::{Context, Poll}; - -use hyper::service::Service; -use hyper::{Body, Request, Response}; -use tokio::net::TcpStream; - -#[tokio::main] -async fn main() -> Result<(), Box> { - pretty_env_logger::init(); - - let uri = "http://127.0.0.1:8080".parse::()?; - - let mut svc = Connector; - - let body = Body::empty(); - - let req = Request::get(uri).body(body)?; - let res = svc.call(req).await?; - - println!("RESPONSE={:?}", res); - - Ok(()) -} - -struct Connector; - -impl Service> for Connector { - type Response = Response; - type Error = Box; - type Future = Pin>>>; - - fn poll_ready(&mut self, _cx: &mut Context<'_>) -> std::task::Poll> { - Poll::Ready(Ok(())) - } - - fn call(&mut self, req: Request) -> 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::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) - }) - } -} diff --git a/src/client/mod.rs b/src/client/mod.rs index e4499ec736..fbd8eae6ac 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -40,5 +40,4 @@ cfg_feature! { pub mod conn; pub(super) mod dispatch; mod pool; - pub mod service; } diff --git a/src/client/service.rs b/src/client/service.rs deleted file mode 100644 index 406f61edc9..0000000000 --- a/src/client/service.rs +++ /dev/null @@ -1,89 +0,0 @@ -//! Utilities used to interact with the Tower ecosystem. -//! -//! This module provides `Connect` which hook-ins into the Tower ecosystem. - -use std::error::Error as StdError; -use std::future::Future; -use std::marker::PhantomData; - -use tracing::debug; - -use super::conn::{Builder, SendRequest}; -use crate::{ - body::HttpBody, - common::{task, Pin, Poll}, - service::{MakeConnection, Service}, -}; - -/// Creates a connection via `SendRequest`. -/// -/// This accepts a `hyper::client::conn::Builder` and provides -/// a `MakeService` implementation to create connections from some -/// target `T`. -#[derive(Debug)] -pub struct Connect { - inner: C, - builder: Builder, - _pd: PhantomData, -} - -impl Connect { - /// Create a new `Connect` with some inner connector `C` and a connection - /// builder. - pub fn new(inner: C, builder: Builder) -> Self { - Self { - inner, - builder, - _pd: PhantomData, - } - } -} - -impl Service for Connect -where - C: MakeConnection, - C::Connection: Unpin + Send + 'static, - C::Future: Send + 'static, - C::Error: Into> + Send, - B: HttpBody + Unpin + Send + 'static, - B::Data: Send + Unpin, - B::Error: Into>, -{ - type Response = SendRequest; - type Error = crate::Error; - type Future = - Pin> + Send + 'static>>; - - fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll> { - self.inner - .poll_ready(cx) - .map_err(|e| crate::Error::new(crate::error::Kind::Connect).with(e.into())) - } - - fn call(&mut self, req: T) -> Self::Future { - let builder = self.builder.clone(); - let io = self.inner.make_connection(req); - - let fut = async move { - match io.await { - Ok(io) => match builder.handshake(io).await { - Ok((sr, conn)) => { - builder.exec.execute(async move { - if let Err(e) = conn.await { - debug!("connection error: {:?}", e); - } - }); - Ok(sr) - } - Err(e) => Err(e), - }, - Err(e) => { - let err = crate::Error::new(crate::error::Kind::Connect).with(e.into()); - Err(err) - } - } - }; - - Box::pin(fut) - } -} diff --git a/src/service/make.rs b/src/service/make.rs deleted file mode 100644 index 2b0f8ef19f..0000000000 --- a/src/service/make.rs +++ /dev/null @@ -1,39 +0,0 @@ -use tokio::io::{AsyncRead, AsyncWrite}; - -use super::Service; -use crate::common::{task, Future, Poll}; - -// The same "trait alias" as tower::MakeConnection, but inlined to reduce -// dependencies. -pub trait MakeConnection: self::sealed::Sealed<(Target,)> { - type Connection: AsyncRead + AsyncWrite; - type Error; - type Future: Future>; - - fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll>; - fn make_connection(&mut self, target: Target) -> Self::Future; -} - -impl self::sealed::Sealed<(Target,)> for S where S: Service {} - -impl MakeConnection for S -where - S: Service, - S::Response: AsyncRead + AsyncWrite, -{ - type Connection = S::Response; - type Error = S::Error; - type Future = S::Future; - - fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll> { - Service::poll_ready(self, cx) - } - - fn make_connection(&mut self, target: Target) -> Self::Future { - Service::call(self, target) - } -} - -mod sealed { - pub trait Sealed {} -} diff --git a/src/service/mod.rs b/src/service/mod.rs index baa093cd25..2d698a17a3 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -10,8 +10,6 @@ //! //! - `HttpService`: This is blanketly implemented for all types that //! implement `Service, Response = http::Response>`. -//! - `MakeConnection`: A `Service` that returns a "connection", a type that -//! implements `AsyncRead` and `AsyncWrite`. //! //! # HttpService //! @@ -26,7 +24,6 @@ pub use tower_service::Service; mod http; -mod make; #[cfg(all(any(feature = "http1", feature = "http2"), feature = "client"))] mod oneshot; mod util; @@ -34,8 +31,6 @@ mod util; #[cfg(all(any(feature = "http1", feature = "http2"), feature = "server"))] pub(super) use self::http::HttpService; #[cfg(all(any(feature = "http1", feature = "http2"), feature = "client"))] -pub(super) use self::make::MakeConnection; -#[cfg(all(any(feature = "http1", feature = "http2"), feature = "client"))] pub(super) use self::oneshot::{oneshot, Oneshot}; pub use self::util::service_fn;