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

2862: GracefulShutdown utility #86

Closed
wants to merge 9 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ tracing = { version = "0.1", default-features = false, features = ["std"] }
tokio = { version = "1", optional = true, features = ["net", "rt", "time"] }
tower-service ={ version = "0.3", optional = true }
tower = { version = "0.4.1", optional = true, features = ["make", "util"] }
slab = { version = "0.4.9", optional = true }

[dev-dependencies]
hyper = { version = "1.1.0", features = ["full"] }
bytes = "1"
http-body-util = "0.1.0"
tokio = { version = "1", features = ["macros", "test-util"] }
tokio = { version = "1", features = ["macros", "test-util", "signal"] }
tokio-test = "0.4"
pretty_env_logger = "0.5"

Expand All @@ -50,6 +51,7 @@ full = [
"client-legacy",
"server",
"server-auto",
"server-graceful",
"service",
"http1",
"http2",
Expand All @@ -61,6 +63,7 @@ client-legacy = ["client"]

server = ["hyper/server"]
server-auto = ["server", "http1", "http2"]
server-graceful = ["dep:slab"]

service = ["dep:tower", "dep:tower-service"]

Expand All @@ -75,3 +78,7 @@ __internal_happy_eyeballs_tests = []
[[example]]
name = "client"
required-features = ["client-legacy", "http1", "tokio"]

[[example]]
name = "server_graceful"
required-features = ["tokio", "server-graceful", "server-auto"]
65 changes: 65 additions & 0 deletions examples/server_graceful.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use bytes::Bytes;
use std::convert::Infallible;
use std::pin::pin;
use std::time::Duration;
use tokio::net::TcpListener;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let listener = TcpListener::bind("127.0.0.1:8080").await?;

let graceful = hyper_util::server::graceful::GracefulShutdown::new();
let mut ctrl_c = pin!(tokio::signal::ctrl_c());

loop {
tokio::select! {
conn = listener.accept() => {
let (stream, peer_addr) = match conn {
Ok(conn) => conn,
Err(e) => {
eprintln!("accept error: {}", e);
tokio::time::sleep(Duration::from_secs(1)).await;
continue;
}
};
eprintln!("incomming connection accepted: {}", peer_addr);

let stream = hyper_util::rt::TokioIo::new(Box::pin(stream));
let watcher = graceful.watcher();

tokio::spawn(async move {
let server = hyper_util::server::conn::auto::Builder::new(hyper_util::rt::TokioExecutor::new());
let conn = server.serve_connection_with_upgrades(stream, hyper::service::service_fn(|_| async move {
tokio::time::sleep(Duration::from_secs(5)).await; // emulate slow request
let body = http_body_util::Full::<Bytes>::from("Hello World!".to_owned());
Ok::<_, Infallible>(http::Response::new(body))
}));

let conn = watcher.watch(conn);

if let Err(err) = conn.await {
eprintln!("connection error: {}", err);
}
eprintln!("connection dropped: {}", peer_addr);
});
},

_ = ctrl_c.as_mut() => {
drop(listener);
eprintln!("Ctrl-C received, starting shutdown");
break;
}
}
}

tokio::select! {
_ = graceful.shutdown() => {
eprintln!("Gracefully shutdown!");
},
_ = tokio::time::sleep(Duration::from_secs(10)) => {
eprintln!("Waited 10 seconds for graceful shutdown, aborting...");
}
}

Ok(())
}
Loading