Skip to content

Commit

Permalink
Implement graceful shutdown
Browse files Browse the repository at this point in the history
Should close #33
  • Loading branch information
hannesdejager committed Nov 19, 2021
1 parent 210712d commit 68705bf
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 39 deletions.
123 changes: 95 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ authors = [
"Steven Meunier <[email protected]>",
"Rob klein Gunnewiek <[email protected]>"
]
edition = "2018"
edition = "2021"
description = "When you need to FTP, but don't want to. An async, cloud orientated FTP(S) server built on libunftp"
repository = "https://github.com/bolcom/unFTP"
homepage = "https://github.com/bolcom/unFTP"
Expand All @@ -36,7 +36,7 @@ http = "0.2.5"
hyper = { version = "0.14.13", features = ["server", "http1"] }
hyper-rustls = "^0.22"
lazy_static = "1.4.0"
libunftp = "0.18.1"
libunftp = "0.18.2"
prometheus = { version = "0.12.0", features = ["process"] }
serde = { version = "1.0.130", features = ["derive"] }
serde_json = { version = "1.0.68" }
Expand Down
18 changes: 16 additions & 2 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ const PATH_HEALTH: &str = "/health";
const PATH_READINESS: &str = "/ready";

// starts an HTTP server and exports Prometheus metrics.
pub async fn start(log: &Logger, bind_addr: &str, ftp_addr: SocketAddr) -> Result<(), String> {
pub async fn start(
log: &Logger,
bind_addr: &str,
ftp_addr: SocketAddr,
mut shutdown: tokio::sync::broadcast::Receiver<()>,
done: tokio::sync::mpsc::Sender<()>,
) -> Result<(), String> {
let http_addr: SocketAddr = bind_addr
.parse()
.map_err(|e| format!("unable to parse HTTP address {}: {}", bind_addr, e))?;
Expand All @@ -32,7 +38,12 @@ pub async fn start(log: &Logger, bind_addr: &str, ftp_addr: SocketAddr) -> Resul
}
});

let http_server = hyper::Server::bind(&http_addr).serve(make_svc);
let http_server = hyper::Server::bind(&http_addr)
.serve(make_svc)
.with_graceful_shutdown(async {
shutdown.recv().await.ok();
info!(log, "Shutting down HTTP server");
});

info!(log, "Starting HTTP service."; "address" => &http_addr);
info!(log, "Exposing {} service home.", app::NAME; "path" => PATH_HOME);
Expand All @@ -43,6 +54,9 @@ pub async fn start(log: &Logger, bind_addr: &str, ftp_addr: SocketAddr) -> Resul
if let Err(e) = http_server.await {
error!(log, "HTTP server error: {}", e)
}

info!(log, "HTTP shutdown OK");
drop(done);
Ok(())
}

Expand Down
Loading

0 comments on commit 68705bf

Please sign in to comment.