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

Handle SIGTERM properly #183

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 13 additions & 4 deletions src/wagi_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ use hyper::{
use hyper::{Body, Response, Server};
use tokio::net::TcpStream;
use tokio_rustls::server::TlsStream;
use tokio::signal::unix::SignalKind;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work on Windows? Might need to be conditionally compiled...


pub struct WagiServer {
routing_table: RoutingTable,
tls: Option<TlsConfiguration>,
address: SocketAddr,
}

async fn shutdown_signal() {
// Wait for the SIGTERM signal
tokio::signal::unix::signal(SignalKind::terminate()).unwrap().recv().await;
}

impl WagiServer {
pub async fn new(configuration: &WagiConfiguration, routing_table: RoutingTable) -> anyhow::Result<Self> {
Ok(Self {
Expand Down Expand Up @@ -65,9 +71,10 @@ impl WagiServer {
}))
})
});
Server::builder(tls::TlsHyperAcceptor::new(&self.address, &tls.cert_path, &tls.key_path).await?)
.serve(mk_svc)
.await?;
let server = Server::builder(tls::TlsHyperAcceptor::new(&self.address, &tls.cert_path, &tls.key_path).await?)
.serve(mk_svc);
let graceful = server.with_graceful_shutdown(shutdown_signal());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had problems with with_graceful_shutdown in Spin because it kept the process alive until all connections had closed, which could be a couple of minutes due to keep-alives. See fermyon/spin#73 for the problem and links to relevant Hyper issues, and fermyon/spin#232 for how we solved it there.

graceful.await?;
},
None => {
let mk_svc = make_service_fn(move |conn: &AddrStream| {
Expand All @@ -80,7 +87,9 @@ impl WagiServer {
}))
}
});
Server::bind(&self.address).serve(mk_svc).await?;
let server = Server::bind(&self.address).serve(mk_svc);
let graceful = server.with_graceful_shutdown(shutdown_signal());
graceful.await?;
},
}

Expand Down