Skip to content

Commit

Permalink
Upgrade to Hyper 1.0 & Axum 0.7 (#1670)
Browse files Browse the repository at this point in the history
* Upgrade to hyper 1 and http 1

Upgrades only in Cargo.toml

Co-authored-by: Ivan Krivosheev <[email protected]>
Co-authored-by: Allan Zhang <[email protected]>

* Convert from hyper::Body to http_body::BoxedBody

When appropriate, we replace `hyper::Body` with `http_body::BoxedBody`, a good general purpose replacement for `hyper::Body`.

Hyper does provide `hyper::body::Incoming`, but we cannot construct that, so anywhere we might need a body that we can construct (even most Service trait impls) we must use something like `http_body::BoxedBody`.

When a service accepts `BoxedBody` and not `Incoming`, this indicates that the service is designed to run in places where it is not adjacent to hyper, for example, after routing (which is managed by Axum)

Additionally, http >= 1 requires that extension types are `Clone`, so this bound has been added where appropriate.

Co-authored-by: Ivan Krivosheev <[email protected]>
Co-authored-by: Allan Zhang <[email protected]>

* Convert tonic::codec::decode to use http >= 1 body types

The Body trait has changed (removed `poll_data` and `poll_trailers`, they are now combined in `poll_frame`) and so the codec must be re-written to merge those two methods.

Co-authored-by: Ivan Krivosheev <[email protected]>

* Convert tonic::transport::channel to use http >= 1 body types

tonic::transport::channel previously used `hyper::Body` as the response body type. This type no longer exists in hyper >= 1, and so has been converted to a `BoxBody` provided by `http_body_util` designed for interoperability between http crates.

Co-authored-by: Ivan Krivosheev <[email protected]>
Co-authored-by: Allan Zhang <[email protected]>

* [tests] Convert tonic::codec::prost::tests to use http >= 1 body types

The Body trait has changed (removed `poll_data` and `poll_trailers`, they are now combined in `poll_frame`) and so the codec must be re-written to merge those two methods. This also handles the return types which should now be wrapped in `Frame` when appropriate.

Co-authored-by: Ivan Krivosheev <[email protected]>
Co-authored-by: Allan Zhang <[email protected]>

* Convert tonic::codec::encode to use http >= 1 body types

The Body trait has changed (removed `poll_data` and `poll_trailers`, they are now combined in `poll_frame`) and so the codec must be re-written to merge those two methods.

Co-authored-by: Ivan Krivosheev <[email protected]>
Co-authored-by: Allan Zhang <[email protected]>

* [tests] Convert tonic::service::interceptor::tests to use http >= 1 body types

The Body trait has changed (removed `poll_data` and `poll_trailers`, they are now combined in `poll_frame`) and so the codec must be re-written to merge those two methods. This also handles the return types which should now be wrapped in `Frame` when appropriate.

Co-authored-by: Ivan Krivosheev <[email protected]>
Co-authored-by: Allan Zhang <[email protected]>

* Convert tonic::transport to use http >= 1 body types

Here, we must update some body types which are no longer valid. (A) BoxBody no longer has an `empty` method, instead we provide a helper in `tonic::body` for creating an empty boxed body via `http_body_util`. As well, `hyper::Body` is no longer a type, and instead, `hyper::Incoming` is used when directly recieving a Request from hyper, and `BoxBody` is used when the request may have passed through an axum router. In tonic, we prefer `BoxBody` as it allows for services to be used downstream from other components which enforce a specific body type (e.g. Axum), at the cost of making Body streaming opaque.

Co-authored-by: Ivan Krivosheev <[email protected]>
Co-authored-by: Allan Zhang <[email protected]>

* Convert tonic::transport::server::recover_error to use http >= 1 body types

The Body trait has changed (removed `poll_data` and `poll_trailers`, they are now combined in `poll_frame`) and so the codec must be re-written to merge those two methods.

Co-authored-by: Ivan Krivosheev <[email protected]>
Co-authored-by: Ludea <[email protected]>

* Convert h2c examples to use http >= 1 body types

In h2c, when a service is receiving from hyper, it has to accept a `hyper::body::Incoming` in hyper >= 1.

Additionally, response bodies must be built from `http_body_util` combinators and become BoxBody objects.

* [tests] Convert MergeTrailers body wrapper in interop server

The Body trait has changed (removed `poll_data` and `poll_trailers`, they are now combined in `poll_frame`) and so the codec must be re-written to merge those two methods.

* [tests] Convert compression tests to use hyper 1 body types

The Body trait has changed (removed `poll_data` and `poll_trailers`, they are now combined in `poll_frame`) and so the codec must be re-written to merge those two methods.

* [tests] Convert complex_tower_middleware Body for hyper 1

The Body trait has changed (removed `poll_data` and `poll_trailers`, they are now combined in `poll_frame`) and so the codec must be re-written to merge those two methods.

* [tests] Convert integration_tests::origin to use http >= 1 body types

The Body trait has changed (removed `poll_data` and `poll_trailers`, they are now combined in `poll_frame`) and so the codec must be re-written to merge those two methods.

* Convert tonic-web to use http >= 1 body types

The Body trait has changed (removed `poll_data` and `poll_trailers`, they are now combined in `poll_frame`) and so the codec must be re-written to merge those two methods.

* Adapt for hyper-specific IO traits

hyper >= 1 provides its own I/O traits (Read & Write) instead of relying on the equivalent traits from `tokio`. Then, `hyper-util` provides adaptor structs to wrap `tokio` I/O objects and implement the hyper equivalents. Therefore, we update the appropriate bounds to use the hyper traits, and update the I/O objects so that they are wrapped in the tokio to hyper adaptor.

Co-authored-by: Ivan Krivosheev <[email protected]>
Co-authored-by: Allan Zhang <[email protected]>

* Upgrade axum to 0.7

Axum must be >= 0.7 to support hyper >= 1

Doing this also involves changing the Body type used. Since hyper >= 1 does not provide a generic body type, Axum and tonic both use `BoxBody` to provide a pointer to a Body.

This changes the trait bounds required for methods which accept additional Serivces to be run alongside the primary GRPC service, since those will be routed with Axum, and therefore must accept a BoxBody.

Co-authored-by: Ivan Krivosheev <[email protected]>
Co-authored-by: Allan Zhang <[email protected]>

* Convert service connector for hyper-1.0

Hyper >= 1 no longer includes automatic http2/http1 combined connections, and so we must swtich to the `http2::Builder` type (this is okay, we set http2_only(true) anyhow).

As well, hyper >= 1 is generic over executors and does not directly depend on tokio. Since http2 connections can be multiplexed, they require some additional background task to handle sending and receiving requests. Additionally, these background tasks do not natively implement `tower::Service` since hyper >= 1 does not depend on `tower`.

Therefore, we re-implement the `SendRequest` task as a tower::Service, so that it can be used within `Connection`, which expects to operate on a tower::Service to serve connections.

Co-authored-by: Ivan Krivosheev <[email protected]>
Co-authored-by: Allan Zhang <[email protected]>

* Convert hyper::Client to hyper_util::legacy::Client

`hyper::Client` has been moved to `hyper_util::legacy::Client` in version 1.

Co-authored-by: Ivan Krivosheev <[email protected]>
Co-authored-by: Allan Zhang <[email protected]>

* Identify and propogate connect errors

hyper::Error no longer provides information about Connect errors, especially since hyper_util now contains the connection implementation, it does not provide a separate error type.

Instead, we create an internal Error type which is used in our own connectors, and then checked when figuring out what the gRPC status should be.

* Remove hyper::server::conn::AddrStream

hyper >= 1 has deprecated all of `hyper::server`, including `AddrStream`

Co-authored-by: Ivan Krivosheev <[email protected]>
Co-authored-by: Allan Zhang <[email protected]>

Replace hyper::server::Accept

hyper::server is deprectaed. Instead, we implement our own TCP-incoming based on the now removed hyper::server::Accept.

In order to set `TCP_KEEPALIVE` we require the socket2 crate, since this option is not exposed in the standard library’s API. The implementaiton is inspired by that of hyper v0.14

* [examples] In h2c, replace hyper::Server with an accept loop

hyper::Server is deprecated, with no current common replacement. Instead of implementing (or using tonic’s new) full server in here, we write a simple accept loop, which is sufficient to demonstrate the functionality of h2c.

* Upgrade tls dependencies

hyper-rustls requires version 0.27.0 to support hyper >= 1, bringing a few other tls bumps along. Importantly, we add the “ring” and “tls12” features to use ring as the crypto backend, consistent with previous versions of tonic. A future version of tonic might support selecting backends via features.

Co-authored-by: Ivan Krivosheev <[email protected]>

* Combine trailers when streaming decode body

We aren't sure if multiple trailers should even be legal, but if we get multiple trailers in an HTTP body stream, we'll combine them all, to preserve their data.

Alternatively we'd have to pick the first or last trailers, and that might lose information.

* Tweak imports in transport example

Example used `empty_body()`, which is now fully qualified as `tonic::body::empty_body()` to make clear that this is a tonic helper method for creating an empty BoxBody.

* Remove commented out code from examples/h2c

* tonic-web: avoid copy to vector to base64 encode

* tonic-web: Merge subsequent trailer frames

Ideally, a body should only return a single trailer frame. If multiple trailers are returned, merge them together.

* Comment in tonic::status::find_status_in_source_chain

Comment mentions why we choose “Unavailable” for connection errors

* Make TowerToHyperService crate-private

This also requires vendoring it in the rustls example, which doesn’t use a server type.

Making the type crate-private means we can delete some unused methods.

* Fixup imports in tonic::transport

---------

Co-authored-by: Ivan Krivosheev <[email protected]>
Co-authored-by: Allan Zhang <[email protected]>
Co-authored-by: Ludea <[email protected]>
  • Loading branch information
4 people authored Jun 12, 2024
1 parent df85623 commit 9c1f2f9
Show file tree
Hide file tree
Showing 54 changed files with 1,104 additions and 601 deletions.
29 changes: 16 additions & 13 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -271,21 +271,21 @@ routeguide = ["dep:async-stream", "tokio-stream", "dep:rand", "dep:serde", "dep:
reflection = ["dep:tonic-reflection"]
autoreload = ["tokio-stream/net", "dep:listenfd"]
health = ["dep:tonic-health"]
grpc-web = ["dep:tonic-web", "dep:bytes", "dep:http", "dep:hyper", "dep:tracing-subscriber", "dep:tower"]
grpc-web = ["dep:tonic-web", "dep:bytes", "dep:http", "dep:hyper", "dep:hyper-util", "dep:tracing-subscriber", "dep:tower"]
tracing = ["dep:tracing", "dep:tracing-subscriber"]
uds = ["tokio-stream/net", "dep:tower", "dep:hyper"]
uds = ["tokio-stream/net", "dep:tower", "dep:hyper", "dep:hyper-util"]
streaming = ["tokio-stream", "dep:h2"]
mock = ["tokio-stream", "dep:tower"]
tower = ["dep:hyper", "dep:tower", "dep:http"]
mock = ["tokio-stream", "dep:tower", "dep:hyper-util"]
tower = ["dep:hyper", "dep:hyper-util", "dep:tower", "dep:http"]
json-codec = ["dep:serde", "dep:serde_json", "dep:bytes"]
compression = ["tonic/gzip"]
tls = ["tonic/tls"]
tls-rustls = ["dep:hyper", "dep:hyper-rustls", "dep:tower", "tower-http/util", "tower-http/add-extension", "dep:rustls-pemfile", "dep:tokio-rustls"]
tls-rustls = ["dep:hyper", "dep:hyper-util", "dep:hyper-rustls", "dep:tower", "tower-http/util", "tower-http/add-extension", "dep:rustls-pemfile", "dep:tokio-rustls", "dep:pin-project", "dep:http-body-util"]
dynamic-load-balance = ["dep:tower"]
timeout = ["tokio/time", "dep:tower"]
tls-client-auth = ["tonic/tls"]
types = ["dep:tonic-types"]
h2c = ["dep:hyper", "dep:tower", "dep:http"]
h2c = ["dep:hyper", "dep:tower", "dep:http", "dep:hyper-util"]
cancellation = ["dep:tokio-util"]

full = ["gcp", "routeguide", "reflection", "autoreload", "health", "grpc-web", "tracing", "uds", "streaming", "mock", "tower", "json-codec", "compression", "tls", "tls-rustls", "dynamic-load-balance", "timeout", "tls-client-auth", "types", "cancellation", "h2c"]
Expand All @@ -311,16 +311,19 @@ serde_json = { version = "1.0", optional = true }
tracing = { version = "0.1.16", optional = true }
tracing-subscriber = { version = "0.3", features = ["tracing-log", "fmt"], optional = true }
prost-types = { version = "0.12", optional = true }
http = { version = "0.2", optional = true }
http-body = { version = "0.4.2", optional = true }
hyper = { version = "0.14", optional = true }
http = { version = "1", optional = true }
http-body = { version = "1", optional = true }
http-body-util = { version = "0.1", optional = true }
hyper = { version = "1", optional = true }
hyper-util = { version = ">=0.1.4, <0.2", optional = true }
listenfd = { version = "1.0", optional = true }
bytes = { version = "1", optional = true }
h2 = { version = "0.3", optional = true }
tokio-rustls = { version = "0.24.0", optional = true }
hyper-rustls = { version = "0.24.0", features = ["http2"], optional = true }
rustls-pemfile = { version = "1", optional = true }
tower-http = { version = "0.4", optional = true }
tokio-rustls = { version = "0.26", optional = true, features = ["ring", "tls12"], default-features = false }
hyper-rustls = { version = "0.27.0", features = ["http2", "ring", "tls12"], optional = true, default-features = false }
rustls-pemfile = { version = "2.0.0", optional = true }
tower-http = { version = "0.5", optional = true }
pin-project = { version = "1.0.11", optional = true }

[build-dependencies]
tonic-build = { path = "../tonic-build", features = ["prost"] }
3 changes: 2 additions & 1 deletion examples/src/grpc-web/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use hello_world::{greeter_client::GreeterClient, HelloRequest};
use hyper_util::rt::TokioExecutor;
use tonic_web::GrpcWebClientLayer;

pub mod hello_world {
Expand All @@ -8,7 +9,7 @@ pub mod hello_world {
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Must use hyper directly...
let client = hyper::Client::builder().build_http();
let client = hyper_util::client::legacy::Client::builder(TokioExecutor::new()).build_http();

let svc = tower::ServiceBuilder::new()
.layer(GrpcWebClientLayer::new())
Expand Down
29 changes: 17 additions & 12 deletions examples/src/h2c/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use hello_world::greeter_client::GreeterClient;
use hello_world::HelloRequest;
use http::Uri;
use hyper::Client;
use hyper_util::client::legacy::Client;
use hyper_util::rt::TokioExecutor;

pub mod hello_world {
tonic::include_proto!("helloworld");
Expand All @@ -11,7 +12,7 @@ pub mod hello_world {
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let origin = Uri::from_static("http://[::1]:50051");
let h2c_client = h2c::H2cChannel {
client: Client::new(),
client: Client::builder(TokioExecutor::new()).build_http(),
};

let mut client = GreeterClient::with_origin(h2c_client, origin);
Expand All @@ -33,16 +34,20 @@ mod h2c {
task::{Context, Poll},
};

use hyper::{client::HttpConnector, Client};
use tonic::body::BoxBody;
use hyper::body::Incoming;
use hyper_util::{
client::legacy::{connect::HttpConnector, Client},
rt::TokioExecutor,
};
use tonic::body::{empty_body, BoxBody};
use tower::Service;

pub struct H2cChannel {
pub client: Client<HttpConnector>,
pub client: Client<HttpConnector, BoxBody>,
}

impl Service<http::Request<BoxBody>> for H2cChannel {
type Response = http::Response<hyper::Body>;
type Response = http::Response<Incoming>;
type Error = hyper::Error;
type Future =
Pin<Box<dyn std::future::Future<Output = Result<Self::Response, Self::Error>> + Send>>;
Expand All @@ -60,7 +65,7 @@ mod h2c {
let h2c_req = hyper::Request::builder()
.uri(origin)
.header(http::header::UPGRADE, "h2c")
.body(hyper::Body::empty())
.body(empty_body())
.unwrap();

let res = client.request(h2c_req).await.unwrap();
Expand All @@ -72,11 +77,11 @@ mod h2c {
let upgraded_io = hyper::upgrade::on(res).await.unwrap();

// In an ideal world you would somehow cache this connection
let (mut h2_client, conn) = hyper::client::conn::Builder::new()
.http2_only(true)
.handshake(upgraded_io)
.await
.unwrap();
let (mut h2_client, conn) =
hyper::client::conn::http2::Builder::new(TokioExecutor::new())
.handshake(upgraded_io)
.await
.unwrap();
tokio::spawn(conn);

h2_client.send_request(request).await
Expand Down
54 changes: 36 additions & 18 deletions examples/src/h2c/server.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use std::net::SocketAddr;

use hyper_util::rt::{TokioExecutor, TokioIo};
use hyper_util::server::conn::auto::Builder;
use hyper_util::service::TowerToHyperService;
use tokio::net::TcpListener;
use tonic::{transport::Server, Request, Response, Status};

use hello_world::greeter_server::{Greeter, GreeterServer};
use hello_world::{HelloReply, HelloRequest};
use tower::make::Shared;

pub mod hello_world {
tonic::include_proto!("helloworld");
Expand All @@ -28,28 +33,45 @@ impl Greeter for MyGreeter {

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse().unwrap();
let addr: SocketAddr = "[::1]:50051".parse().unwrap();
let greeter = MyGreeter::default();

println!("GreeterServer listening on {}", addr);

let incoming = TcpListener::bind(addr).await?;
let svc = Server::builder()
.add_service(GreeterServer::new(greeter))
.into_router();

let h2c = h2c::H2c { s: svc };

let server = hyper::Server::bind(&addr).serve(Shared::new(h2c));
server.await.unwrap();

Ok(())
loop {
match incoming.accept().await {
Ok((io, _)) => {
let router = h2c.clone();
tokio::spawn(async move {
let builder = Builder::new(TokioExecutor::new());
let conn = builder.serve_connection_with_upgrades(
TokioIo::new(io),
TowerToHyperService::new(router),
);
let _ = conn.await;
});
}
Err(e) => {
eprintln!("Error accepting connection: {}", e);
}
}
}
}

mod h2c {
use std::pin::Pin;

use http::{Request, Response};
use hyper::Body;
use hyper::body::Incoming;
use hyper_util::{rt::TokioExecutor, service::TowerToHyperService};
use tonic::{body::empty_body, transport::AxumBoxBody};
use tower::Service;

#[derive(Clone)]
Expand All @@ -59,17 +81,14 @@ mod h2c {

type BoxError = Box<dyn std::error::Error + Send + Sync>;

impl<S> Service<Request<Body>> for H2c<S>
impl<S> Service<Request<Incoming>> for H2c<S>
where
S: Service<Request<Body>, Response = Response<tonic::transport::AxumBoxBody>>
+ Clone
+ Send
+ 'static,
S: Service<Request<Incoming>, Response = Response<AxumBoxBody>> + Clone + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<BoxError> + Sync + Send + 'static,
S::Response: Send + 'static,
{
type Response = hyper::Response<Body>;
type Response = hyper::Response<tonic::body::BoxBody>;
type Error = hyper::Error;
type Future =
Pin<Box<dyn std::future::Future<Output = Result<Self::Response, Self::Error>> + Send>>;
Expand All @@ -81,20 +100,19 @@ mod h2c {
std::task::Poll::Ready(Ok(()))
}

fn call(&mut self, mut req: hyper::Request<Body>) -> Self::Future {
fn call(&mut self, mut req: hyper::Request<Incoming>) -> Self::Future {
let svc = self.s.clone();
Box::pin(async move {
tokio::spawn(async move {
let upgraded_io = hyper::upgrade::on(&mut req).await.unwrap();

hyper::server::conn::Http::new()
.http2_only(true)
.serve_connection(upgraded_io, svc)
hyper::server::conn::http2::Builder::new(TokioExecutor::new())
.serve_connection(upgraded_io, TowerToHyperService::new(svc))
.await
.unwrap();
});

let mut res = hyper::Response::new(hyper::Body::empty());
let mut res = hyper::Response::new(empty_body());
*res.status_mut() = http::StatusCode::SWITCHING_PROTOCOLS;
res.headers_mut().insert(
hyper::header::UPGRADE,
Expand Down
1 change: 1 addition & 0 deletions examples/src/interceptor/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ fn intercept(mut req: Request<()>) -> Result<Request<()>, Status> {
Ok(req)
}

#[derive(Clone)]
struct MyExtension {
some_piece_of_data: String,
}
3 changes: 2 additions & 1 deletion examples/src/mock/mock.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use hyper_util::rt::TokioIo;
use tonic::{
transport::{Endpoint, Server, Uri},
Request, Response, Status,
Expand Down Expand Up @@ -36,7 +37,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

async move {
if let Some(client) = client {
Ok(client)
Ok(TokioIo::new(client))
} else {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
Expand Down
10 changes: 5 additions & 5 deletions examples/src/tls_rustls/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ pub mod pb {
tonic::include_proto!("/grpc.examples.unaryecho");
}

use hyper::{client::HttpConnector, Uri};
use hyper::Uri;
use hyper_util::{client::legacy::connect::HttpConnector, rt::TokioExecutor};
use pb::{echo_client::EchoClient, EchoRequest};
use tokio_rustls::rustls::{ClientConfig, RootCertStore};

Expand All @@ -17,11 +18,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut roots = RootCertStore::empty();

let mut buf = std::io::BufReader::new(&fd);
let certs = rustls_pemfile::certs(&mut buf)?;
roots.add_parsable_certificates(&certs);
let certs = rustls_pemfile::certs(&mut buf).collect::<Result<Vec<_>, _>>()?;
roots.add_parsable_certificates(certs.into_iter());

let tls = ClientConfig::builder()
.with_safe_defaults()
.with_root_certificates(roots)
.with_no_client_auth();

Expand All @@ -47,7 +47,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.map_request(|_| Uri::from_static("https://[::1]:50051"))
.service(http);

let client = hyper::Client::builder().build(connector);
let client = hyper_util::client::legacy::Client::builder(TokioExecutor::new()).build(connector);

// Using `with_origin` will let the codegenerated client set the `scheme` and
// `authority` from the porvided `Uri`.
Expand Down
Loading

0 comments on commit 9c1f2f9

Please sign in to comment.