-
Notifications
You must be signed in to change notification settings - Fork 42
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
Update Axum version in proxy #817
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,16 +1,20 @@ | ||||||||||||
use axum::{ | ||||||||||||
body::{Body, BoxBody, Bytes}, | ||||||||||||
extract::State, | ||||||||||||
http::{request, HeaderValue, Request}, | ||||||||||||
body::{Body, Bytes}, | ||||||||||||
extract::{Request, State}, | ||||||||||||
http::Uri, | ||||||||||||
http::{request, HeaderValue, StatusCode}, | ||||||||||||
middleware::Next, | ||||||||||||
response::Response, | ||||||||||||
}; | ||||||||||||
use hyper::{Client, StatusCode, Uri}; | ||||||||||||
use hyper_util::{ | ||||||||||||
client::legacy::{connect::HttpConnector, Client}, | ||||||||||||
rt::TokioExecutor, | ||||||||||||
}; | ||||||||||||
Comment on lines
+9
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hyper 1.x removed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @paulgb, thank you for pointing that out. You're correct that in hyper 1.x, the (_/) ✏️ Learnings added
Comment on lines
+9
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider updating to the current The code is still using You could replace the client construction with: -use hyper_util::{
- client::legacy::{connect::HttpConnector, Client},
- rt::TokioExecutor,
-};
+use hyper::{Client, client::HttpConnector};
// ... later in the code ...
- let client = Client::builder(TokioExecutor::new()).build(HttpConnector::new());
+ let client = Client::builder().build(HttpConnector::new()); This change simplifies the client creation and removes the dependency on 📝 Committable suggestion
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hyper::Client no longer exists. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @paulgb, thank you for pointing that out. You are correct; (_/) 🧠 Learnings used
|
||||||||||||
use url::Url; | ||||||||||||
|
||||||||||||
pub fn clone_request_with_empty_body(parts: &request::Parts) -> request::Request<Body> { | ||||||||||||
pub fn clone_request_with_empty_body(parts: &request::Parts) -> hyper::http::Request<Body> { | ||||||||||||
// Copy method and URL. | ||||||||||||
let mut builder = request::Builder::new() | ||||||||||||
let mut builder = hyper::http::request::Builder::new() | ||||||||||||
.method(parts.method.clone()) | ||||||||||||
.uri(parts.uri.clone()); | ||||||||||||
|
||||||||||||
|
@@ -32,11 +36,7 @@ pub fn clone_request_with_empty_body(parts: &request::Parts) -> request::Request | |||||||||||
.expect("Request is always valid.") | ||||||||||||
} | ||||||||||||
|
||||||||||||
pub async fn forward_layer<B>( | ||||||||||||
State(forward_url): State<Url>, | ||||||||||||
req: Request<B>, | ||||||||||||
next: Next<B>, | ||||||||||||
) -> Response<BoxBody> { | ||||||||||||
pub async fn forward_layer(State(forward_url): State<Url>, req: Request, next: Next) -> Response { | ||||||||||||
let (parts, body) = req.into_parts(); | ||||||||||||
let mut forward_req = clone_request_with_empty_body(&parts); | ||||||||||||
let req = Request::from_parts(parts, body); | ||||||||||||
|
@@ -48,7 +48,7 @@ pub async fn forward_layer<B>( | |||||||||||
*forward_req.uri_mut() = uri; | ||||||||||||
|
||||||||||||
// Create a client | ||||||||||||
let client = Client::new(); | ||||||||||||
let client = Client::builder(TokioExecutor::new()).build(HttpConnector::new()); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not work in hyper 1.x. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider simplifying the HTTP client construction The client is currently constructed using a specific executor. It's possible to simplify this by using You can modify the client creation as follows: - let client = Client::builder(TokioExecutor::new()).build(HttpConnector::new());
+ let client = Client::builder().build(HttpConnector::new()); This change removes the need for 📝 Committable suggestion
Suggested change
|
||||||||||||
|
||||||||||||
// Forward the request | ||||||||||||
let forwarded_resp = client.request(forward_req).await; | ||||||||||||
|
@@ -68,13 +68,8 @@ pub async fn forward_layer<B>( | |||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
fn response_helper(status: StatusCode, body: &'static [u8]) -> Response<BoxBody> { | ||||||||||||
// This is a bit ugly. There seems to be no way to construct an http_body with an axum::Error error type (?), | ||||||||||||
// but we can use map_err from http_body::Body to convert the hyper::error::Error to an axum::Error. | ||||||||||||
// Then, we need to box it up for Axum. | ||||||||||||
let body = http_body::Full::new(Bytes::from_static(body)); | ||||||||||||
let body = http_body::Body::map_err(body, axum::Error::new); | ||||||||||||
let body: BoxBody = BoxBody::new(body); | ||||||||||||
fn response_helper(status: StatusCode, body: &'static [u8]) -> Response { | ||||||||||||
let body = Body::from(Bytes::from_static(body)); | ||||||||||||
|
||||||||||||
Response::builder() | ||||||||||||
.status(status.as_u16()) | ||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -23,17 +23,17 @@ use anyhow::{Context, Result}; | |||||||||||||||||||||||||||||||||||||
use axum::{ | ||||||||||||||||||||||||||||||||||||||
extract::State, | ||||||||||||||||||||||||||||||||||||||
http::{header, Method}, | ||||||||||||||||||||||||||||||||||||||
middleware::from_fn_with_state, | ||||||||||||||||||||||||||||||||||||||
response::Response, | ||||||||||||||||||||||||||||||||||||||
routing::{get, post}, | ||||||||||||||||||||||||||||||||||||||
Json, Router, Server, | ||||||||||||||||||||||||||||||||||||||
Json, Router, | ||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||
use forward_auth::forward_layer; | ||||||||||||||||||||||||||||||||||||||
use futures_util::never::Never; | ||||||||||||||||||||||||||||||||||||||
use serde::{Deserialize, Serialize}; | ||||||||||||||||||||||||||||||||||||||
use serde_json::{json, Value}; | ||||||||||||||||||||||||||||||||||||||
use std::net::{SocketAddr, TcpListener}; | ||||||||||||||||||||||||||||||||||||||
use std::net::SocketAddr; | ||||||||||||||||||||||||||||||||||||||
use tokio::{ | ||||||||||||||||||||||||||||||||||||||
net::TcpListener, | ||||||||||||||||||||||||||||||||||||||
sync::oneshot::{self}, | ||||||||||||||||||||||||||||||||||||||
task::JoinHandle, | ||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||
|
@@ -146,13 +146,13 @@ pub struct ControllerServer { | |||||||||||||||||||||||||||||||||||||
heartbeat_handle: HeartbeatSender, | ||||||||||||||||||||||||||||||||||||||
// server_handle is wrapped in an Option<> because we need to take ownership of it to join it | ||||||||||||||||||||||||||||||||||||||
// when gracefully terminating. | ||||||||||||||||||||||||||||||||||||||
server_handle: Option<JoinHandle<hyper::Result<()>>>, | ||||||||||||||||||||||||||||||||||||||
server_handle: Option<JoinHandle<Result<(), std::io::Error>>>, | ||||||||||||||||||||||||||||||||||||||
_cleanup_handle: GuardHandle, | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
impl ControllerServer { | ||||||||||||||||||||||||||||||||||||||
pub async fn run(config: ControllerConfig) -> Result<Self> { | ||||||||||||||||||||||||||||||||||||||
let listener = TcpListener::bind(config.bind_addr)?; | ||||||||||||||||||||||||||||||||||||||
let listener = TcpListener::bind(config.bind_addr).await?; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
tracing::info!("Attempting to connect to database..."); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
|
@@ -239,7 +239,11 @@ impl ControllerServer { | |||||||||||||||||||||||||||||||||||||
if let Some(forward_auth_url) = forward_auth { | ||||||||||||||||||||||||||||||||||||||
tracing::info!(?forward_auth_url, "Forward auth enabled"); | ||||||||||||||||||||||||||||||||||||||
let forward_url = forward_auth_url.clone(); | ||||||||||||||||||||||||||||||||||||||
control_routes = control_routes.layer(from_fn_with_state(forward_url, forward_layer)); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
control_routes = control_routes.layer(axum::middleware::from_fn_with_state( | ||||||||||||||||||||||||||||||||||||||
forward_url.clone(), | ||||||||||||||||||||||||||||||||||||||
forward_layer, | ||||||||||||||||||||||||||||||||||||||
)); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
let cors_public = CorsLayer::new() | ||||||||||||||||||||||||||||||||||||||
|
@@ -265,13 +269,16 @@ impl ControllerServer { | |||||||||||||||||||||||||||||||||||||
.layer(trace_layer) | ||||||||||||||||||||||||||||||||||||||
.with_state(controller); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
let server_handle = tokio::spawn( | ||||||||||||||||||||||||||||||||||||||
Server::from_tcp(listener)? | ||||||||||||||||||||||||||||||||||||||
.serve(app.into_make_service_with_connect_info::<SocketAddr>()) | ||||||||||||||||||||||||||||||||||||||
.with_graceful_shutdown(async { | ||||||||||||||||||||||||||||||||||||||
graceful_terminate_receiver.await.ok(); | ||||||||||||||||||||||||||||||||||||||
}), | ||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||
let server_handle = tokio::spawn(async { | ||||||||||||||||||||||||||||||||||||||
axum::serve( | ||||||||||||||||||||||||||||||||||||||
listener, | ||||||||||||||||||||||||||||||||||||||
app.into_make_service_with_connect_info::<SocketAddr>(), | ||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||
.with_graceful_shutdown(async { | ||||||||||||||||||||||||||||||||||||||
graceful_terminate_receiver.await.ok(); | ||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||
.await | ||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+272
to
+281
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace The function Apply this diff to fix the server startup: -let server_handle = tokio::spawn(async {
- axum::serve(
- listener,
- app.into_make_service_with_connect_info::<SocketAddr>(),
- )
- .with_graceful_shutdown(async {
- graceful_terminate_receiver.await.ok();
- })
- .await
-});
+let server_handle = tokio::spawn(async {
+ hyper::Server::from_tcp(listener)?
+ .serve(app.into_make_service_with_connect_info::<SocketAddr>())
+ .with_graceful_shutdown(async {
+ graceful_terminate_receiver.await.ok();
+ })
+ .await
+}); 📝 Committable suggestion
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this axum API has been changed, this is the right way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @paulgb, understood. Thank you for the clarification that 🐰❓ ✏️ Learnings added
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
Ok(Self { | ||||||||||||||||||||||||||||||||||||||
graceful_terminate_sender: Some(graceful_terminate_sender), | ||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
Inconsistent StatusCode Imports Detected
There are remaining imports of
hyper::StatusCode
in the following files:dynamic-proxy/tests/graceful_https.rs
dynamic-proxy/tests/graceful.rs
dynamic-proxy/tests/test_http_versions.rs
dynamic-proxy/tests/hello_world_http.rs
Please update these imports to
reqwest::StatusCode
to ensure consistency across the codebase.🔗 Analysis chain
LGTM! Verify consistency across the codebase.
The change from
hyper::StatusCode
toreqwest::StatusCode
looks good. This modification aligns with the use ofreqwest
as the HTTP client library. TheStatusCode
enum should have the same variants for standard HTTP status codes in both libraries, ensuring that the existing test cases remain valid.To ensure consistency across the codebase, please run the following script:
This script will help identify any inconsistencies in
StatusCode
usage across the project.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 7586
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is intentional; we want to avoid breaking things if the version of
hyper
depended on byreqwest
andaxum
differs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@paulgb, understood. Thank you for the clarification. I'll keep this in mind for future reviews.
(_/)
(o.o)
( )?
✏️ Learnings added