Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade to Hyper 1.0 & Axum 0.7 (#1670)
* 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