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

proxy: add http2 support #6335

Closed
wants to merge 10 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 2 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ http-types = { version = "2", default-features = false }
humantime = "2.1"
humantime-serde = "1.1.1"
hyper = "0.14"
hyper-tungstenite = "0.11"
inotify = "0.10.2"
ipnet = "2.9.0"
itertools = "0.10"
Expand Down Expand Up @@ -156,13 +155,15 @@ tokio-rustls = "0.24"
tokio-stream = "0.1"
tokio-tar = "0.3"
tokio-util = { version = "0.7.10", features = ["io", "rt"] }
tokio-tungstenite = "0.20"
toml = "0.7"
toml_edit = "0.19"
tonic = {version = "0.9", features = ["tls", "tls-roots"]}
tracing = "0.1"
tracing-error = "0.2.0"
tracing-opentelemetry = "0.19.0"
tracing-subscriber = { version = "0.3", default_features = false, features = ["smallvec", "fmt", "tracing-log", "std", "env-filter", "json"] }
tungstenite = "0.20"
url = "2.2"
uuid = { version = "1.6.1", features = ["v4", "v7", "serde"] }
walkdir = "2.3.2"
Expand Down
220 changes: 209 additions & 11 deletions poetry.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ hex.workspace = true
hmac.workspace = true
hostname.workspace = true
humantime.workspace = true
hyper-tungstenite.workspace = true
hyper.workspace = true
ipnet.workspace = true
itertools.workspace = true
Expand Down Expand Up @@ -66,11 +65,13 @@ tls-listener.workspace = true
tokio-postgres.workspace = true
tokio-rustls.workspace = true
tokio-util.workspace = true
tokio-tungstenite.workspace = true
tokio = { workspace = true, features = ["signal"] }
tracing-opentelemetry.workspace = true
tracing-subscriber.workspace = true
tracing-utils.workspace = true
tracing.workspace = true
tungstenite.workspace = true
url.workspace = true
utils.workspace = true
uuid.workspace = true
Expand Down
48 changes: 44 additions & 4 deletions proxy/src/serverless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ pub async fn task_main(
return Ok(());
}
};
let tls_acceptor: tokio_rustls::TlsAcceptor = tls_config.to_server_config().into();

let mut tls_server_config = rustls::ServerConfig::clone(&tls_config.to_server_config());
// prefer http2, but support http/1.1
tls_server_config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
let tls_acceptor: tokio_rustls::TlsAcceptor = Arc::new(tls_server_config).into();

let mut addr_incoming = AddrIncoming::from_listener(ws_listener)?;
let _ = addr_incoming.set_nodelay(true);
Expand All @@ -103,6 +107,9 @@ pub async fn task_main(
let client_addr = io.client_addr();
let remote_addr = io.inner.remote_addr();
let sni_name = tls.server_name().map(|s| s.to_string());
let protocol = tls
.alpn_protocol()
.map(|s| String::from_utf8_lossy(s).into_owned());
let conn_pool = conn_pool.clone();
let ws_connections = ws_connections.clone();
let endpoint_rate_limiter = endpoint_rate_limiter.clone();
Expand All @@ -116,6 +123,7 @@ pub async fn task_main(
Ok(MetricService::new(hyper::service::service_fn(
move |req: Request<Body>| {
let sni_name = sni_name.clone();
let protocol = protocol.clone();
let conn_pool = conn_pool.clone();
let ws_connections = ws_connections.clone();
let endpoint_rate_limiter = endpoint_rate_limiter.clone();
Expand All @@ -140,6 +148,7 @@ pub async fn task_main(
"serverless",
session = %session_id,
%peer_addr,
http_protocol = ?protocol,
))
.await
}
Expand All @@ -150,6 +159,7 @@ pub async fn task_main(
);

hyper::Server::builder(accept::from_stream(tls_listener))
.http2_enable_connect_protocol()
.serve(make_svc)
.with_graceful_shutdown(cancellation_token.cancelled())
.await?;
Expand Down Expand Up @@ -213,11 +223,13 @@ async fn request_handler(
.and_then(|h| h.split(':').next())
.map(|s| s.to_string());

let ws_config = None;

// Check if the request is a websocket upgrade request.
if hyper_tungstenite::is_upgrade_request(&request) {
if websocket::is_upgrade_request(&request) {
info!(session_id = ?session_id, "performing websocket upgrade");

let (response, websocket) = hyper_tungstenite::upgrade(&mut request, None)
let (response, websocket) = websocket::upgrade(&mut request, ws_config)
.map_err(|e| ApiError::BadRequest(e.into()))?;

ws_connections.spawn(
Expand All @@ -240,6 +252,34 @@ async fn request_handler(
.in_current_span(),
);

// Return the response so the spawned future can continue.
Ok(response)
} else if websocket::is_connect_request(&request) {
info!(session_id = ?session_id, "performing http2 websocket upgrade");

let (response, websocket) = websocket::connect(&mut request, ws_config)
.map_err(|e| ApiError::BadRequest(e.into()))?;

ws_connections.spawn(
async move {
let mut ctx = RequestMonitoring::new(session_id, peer_addr, "ws2", &config.region);

if let Err(e) = websocket::serve_websocket(
config,
&mut ctx,
websocket,
&cancel_map,
host,
endpoint_rate_limiter,
)
.await
{
error!(session_id = ?session_id, "error in http2 websocket connection: {e:#}");
}
}
.in_current_span(),
);

// Return the response so the spawned future can continue.
Ok(response)
} else if request.uri().path() == "/sql" && request.method() == Method::POST {
Expand All @@ -256,7 +296,7 @@ async fn request_handler(
.await
} else if request.uri().path() == "/sql" && request.method() == Method::OPTIONS {
Response::builder()
.header("Allow", "OPTIONS, POST")
.header("Allow", "OPTIONS, POST, CONNECT")
.header("Access-Control-Allow-Origin", "*")
.header(
"Access-Control-Allow-Headers",
Expand Down