mirror of
https://github.com/tokio-rs/axum.git
synced 2024-11-26 00:56:27 +01:00
Simplify graceful shutdown (#673)
This commit is contained in:
parent
5698fb8be9
commit
616a43aaa3
2 changed files with 18 additions and 18 deletions
|
@ -7,6 +7,7 @@
|
|||
|
||||
use axum::{response::Html, routing::get, Router};
|
||||
use std::net::SocketAddr;
|
||||
use tokio::signal;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
@ -27,29 +28,28 @@ async fn handler() -> Html<&'static str> {
|
|||
Html("<h1>Hello, World!</h1>")
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
pub async fn shutdown_signal() {
|
||||
use std::io;
|
||||
use tokio::signal::unix::SignalKind;
|
||||
async fn shutdown_signal() {
|
||||
let ctrl_c = async {
|
||||
signal::ctrl_c()
|
||||
.await
|
||||
.expect("failed to install Ctrl+C handler");
|
||||
};
|
||||
|
||||
async fn terminate() -> io::Result<()> {
|
||||
tokio::signal::unix::signal(SignalKind::terminate())?
|
||||
#[cfg(unix)]
|
||||
let terminate = async {
|
||||
signal::unix::signal(signal::unix::SignalKind::terminate())
|
||||
.expect("failed to install signal handler")
|
||||
.recv()
|
||||
.await;
|
||||
Ok(())
|
||||
}
|
||||
};
|
||||
|
||||
#[cfg(not(unix))]
|
||||
let terminate = std::future::pending::<()>();
|
||||
|
||||
tokio::select! {
|
||||
_ = terminate() => {},
|
||||
_ = tokio::signal::ctrl_c() => {},
|
||||
_ = ctrl_c => {},
|
||||
_ = terminate => {},
|
||||
}
|
||||
println!("signal received, starting graceful shutdown")
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
pub async fn shutdown_signal() {
|
||||
tokio::signal::ctrl_c()
|
||||
.await
|
||||
.expect("faild to install CTRL+C handler");
|
||||
println!("signal received, starting graceful shutdown")
|
||||
println!("signal received, starting graceful shutdown");
|
||||
}
|
Loading…
Reference in a new issue