Skip to content

Commit

Permalink
Proof of concept to get h2c functional
Browse files Browse the repository at this point in the history
  • Loading branch information
theJC committed Aug 5, 2023
1 parent f07b835 commit fa00f4d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
2 changes: 1 addition & 1 deletion apollo-router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ http-body = "0.4.5"
heck = "0.4.1"
humantime = "2.1.0"
humantime-serde = "1.1.1"
hyper = { version = "0.14.27", features = ["server", "client"] }
hyper = { version = "0.14.27", features = ["server", "client", "http2"] }
hyper-rustls = { version = "0.23.2", features = ["http1", "http2"] }
indexmap = { version = "1.9.3", features = ["serde-1"] }
itertools = "0.10.5"
Expand Down
61 changes: 34 additions & 27 deletions apollo-router/src/services/subgraph_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ use http::HeaderValue;
use http::Request;
use hyper::client::HttpConnector;
use hyper::Body;
use hyper::Client;
use hyper_rustls::ConfigBuilderExt;
use hyper_rustls::HttpsConnector;
use hyper::client::Client;
//use hyper_rustls::ConfigBuilderExt;
//use hyper_rustls::HttpsConnector;
use mediatype::names::APPLICATION;
use mediatype::names::JSON;
use mediatype::MediaType;
Expand Down Expand Up @@ -134,7 +134,7 @@ pub(crate) struct SubgraphService {
// Note: We use hyper::Client here in preference to reqwest to avoid expensive URL translation
// in the hot path. We use reqwest elsewhere because it's convenient and some of the
// opentelemetry crate require reqwest clients to work correctly (at time of writing).
client: Decompression<hyper::Client<HttpsConnector<HttpConnector>>>,
client: Decompression<hyper::Client<HttpConnector>>,
service: Arc<String>,

/// Whether apq is enabled in the router for subgraph calls
Expand Down Expand Up @@ -162,30 +162,37 @@ impl SubgraphService {
http_connector.set_nodelay(true);
http_connector.set_keepalive(Some(std::time::Duration::from_secs(60)));
http_connector.enforce_http(false);
let tls_config = match tls_cert_store {
None => rustls::ClientConfig::builder()
.with_safe_defaults()
.with_native_roots()
.with_no_client_auth(),
Some(store) => rustls::ClientConfig::builder()
.with_safe_defaults()
.with_root_certificates(store)
.with_no_client_auth(),
};
let builder = hyper_rustls::HttpsConnectorBuilder::new()
.with_tls_config(tls_config)
.https_or_http()
.enable_http1();

let connector = if enable_http2 {
builder.enable_http2().wrap_connector(http_connector)
} else {
builder.wrap_connector(http_connector)
};
// let tls_config = match tls_cert_store {
// None => rustls::ClientConfig::builder()
// .with_safe_defaults()
// .with_native_roots()
// .with_no_client_auth(),
// Some(store) => rustls::ClientConfig::builder()
// .with_safe_defaults()
// .with_root_certificates(store)
// .with_no_client_auth(),
// };
// let builder = hyper_rustls::HttpsConnectorBuilder::new()
// .with_tls_config(tls_config)
// .https_or_http().
// // TODO: Control from config .enable_http1();
//
//
// let connector = if enable_http2 {
// builder.enable_http2().wrap_connector(http_connector)
// } else {
// builder.wrap_connector(http_connector)
// };


let http_client = hyper::Client::builder()
.pool_idle_timeout(POOL_IDLE_TIMEOUT_DURATION)
.build(connector);
.pool_idle_timeout(Some(Duration::from_secs(10000)))
.http2_only(true)
.http2_keep_alive_while_idle(true)
.http2_keep_alive_interval(Some(Duration::from_secs(1)))
.build(http_connector);

Self {
client: ServiceBuilder::new()
.layer(DecompressionLayer::new())
Expand Down Expand Up @@ -574,7 +581,7 @@ async fn call_http(
request: SubgraphRequest,
body: graphql::Request,
context: Context,
client: Decompression<Client<HttpsConnector<HttpConnector>>>,
client: Decompression<Client<HttpConnector>>,
service_name: &str,
) -> Result<SubgraphResponse, BoxError> {
let SubgraphRequest {
Expand Down Expand Up @@ -792,7 +799,7 @@ fn get_graphql_content_type(service_name: &str, parts: &Parts) -> Result<Content
}

async fn do_fetch(
mut client: Decompression<Client<HttpsConnector<HttpConnector>>>,
mut client: Decompression<Client<HttpConnector>>,
context: &Context,
service_name: &str,
request: Request<Body>,
Expand Down

0 comments on commit fa00f4d

Please sign in to comment.