Skip to content

Commit

Permalink
some pr nits
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgb committed Sep 24, 2024
1 parent 262e895 commit 7acd030
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
4 changes: 4 additions & 0 deletions dynamic-proxy/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ impl SimpleHttpServer {

impl Drop for SimpleHttpServer {
fn drop(&mut self) {
if self.graceful_shutdown.is_some() {
tracing::warn!("Shutting down SimpleHttpServer without a call to graceful_shutdown. Connections will be dropped abruptly!");
}

self.handle.abort();
}
}
Expand Down
18 changes: 10 additions & 8 deletions dynamic-proxy/tests/graceful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ use std::net::SocketAddr;
use tokio::net::TcpListener;
use tokio::time::Duration;

mod common;

// Ref: https://github.com/hyperium/hyper-util/blob/master/examples/server_graceful.rs

async fn slow_hello_world(
_: hyper::Request<hyper::body::Incoming>,
) -> Result<hyper::Response<dynamic_proxy::body::SimpleBody>, Infallible> {
tokio::time::sleep(Duration::from_secs(1)).await; // emulate slow request
let body = http_body_util::Full::<Bytes>::from("Hello, world!".to_owned());
let body = to_simple_body(body);
Ok(hyper::Response::new(body))
}

#[tokio::test]
async fn test_graceful_shutdown() {
// Start the server
let addr = SocketAddr::from(([127, 0, 0, 1], 0));
let listener = TcpListener::bind(addr).await.unwrap();
let addr = listener.local_addr().unwrap();
let server = SimpleHttpServer::new(
hyper::service::service_fn(|_| async move {
tokio::time::sleep(Duration::from_secs(1)).await; // emulate slow request
let body = http_body_util::Full::<Bytes>::from("Hello, world!".to_owned());
let body = to_simple_body(body);
Ok::<_, Infallible>(hyper::Response::new(body))
}),
hyper::service::service_fn(slow_hello_world),
listener,
HttpsConfig::Http,
)
Expand Down

0 comments on commit 7acd030

Please sign in to comment.