Skip to content

Commit

Permalink
Gracefully shutdown http services
Browse files Browse the repository at this point in the history
  • Loading branch information
MarinPostma committed Nov 13, 2023
1 parent acf5f4f commit 912ac4e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions libsql-server/src/http/admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::io::ErrorKind;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Notify;
use tokio_util::io::ReaderStream;
use url::Url;

Expand Down Expand Up @@ -60,6 +61,7 @@ pub async fn run<M, A, C>(
namespaces: NamespaceStore<M>,
connector: C,
disable_metrics: bool,
shutdown: Arc<Notify>,
) -> anyhow::Result<()>
where
A: crate::net::Accept,
Expand Down Expand Up @@ -124,6 +126,7 @@ where

hyper::server::Server::builder(acceptor)
.serve(router.into_make_service())
.with_graceful_shutdown(shutdown.notified())
.await
.context("Could not bind admin HTTP API server")?;
Ok(())
Expand Down
4 changes: 3 additions & 1 deletion libsql-server/src/http/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use hyper::{header, Body, Request, Response, StatusCode};
use serde::de::DeserializeOwned;
use serde::Serialize;
use serde_json::Number;
use tokio::sync::{mpsc, oneshot};
use tokio::sync::{mpsc, oneshot, Notify};
use tokio::task::JoinSet;
use tonic::transport::Server;
use tower_http::trace::DefaultOnResponse;
Expand Down Expand Up @@ -237,6 +237,7 @@ pub struct UserApi<M: MakeNamespace, A, P, S> {
pub enable_console: bool,
pub self_url: Option<String>,
pub path: Arc<Path>,
pub shutdown: Arc<Notify>,
}

impl<M, A, P, S> UserApi<M, A, P, S>
Expand Down Expand Up @@ -441,6 +442,7 @@ where
join_set.spawn(async move {
hyper::server::Server::builder(acceptor)
.serve(h2c)
.with_graceful_shutdown(self.shutdown.notified())
.await
.context("http server")?;
Ok(())
Expand Down
6 changes: 6 additions & 0 deletions libsql-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ struct Services<M: MakeNamespace, A, P, S, C> {
db_config: DbConfig,
auth: Arc<Auth>,
path: Arc<Path>,
shutdown: Arc<Notify>,
}

impl<M, A, P, S, C> Services<M, A, P, S, C>
Expand All @@ -156,6 +157,7 @@ where
enable_console: self.user_api_config.enable_http_console,
self_url: self.user_api_config.self_url,
path: self.path.clone(),
shutdown: self.shutdown.clone(),
};

let user_http_service = user_http.configure(join_set);
Expand All @@ -166,12 +168,14 @@ where
disable_metrics,
}) = self.admin_api_config
{
let shutdown = self.shutdown.clone();
join_set.spawn(http::admin::run(
acceptor,
user_http_service,
self.namespaces,
connector,
disable_metrics,
shutdown,
));
}
}
Expand Down Expand Up @@ -398,6 +402,7 @@ where
db_config: self.db_config,
auth,
path: self.path.clone(),
shutdown: self.shutdown.clone(),
};

services.configure(&mut join_set);
Expand Down Expand Up @@ -433,6 +438,7 @@ where
db_config: self.db_config,
auth,
path: self.path.clone(),
shutdown: self.shutdown.clone(),
};

services.configure(&mut join_set);
Expand Down

0 comments on commit 912ac4e

Please sign in to comment.