Gracefully shutdown if LDAP connection is lost

This commit is contained in:
Dreaded_X 2025-04-20 00:24:32 +02:00
parent 7851d6bb12
commit c7b0cfc888
Signed by: Dreaded_X
GPG Key ID: 5A0CBFE3C3377FAA
2 changed files with 13 additions and 7 deletions

View File

@ -3,7 +3,7 @@ use russh::keys::PublicKey;
use tokio::select; use tokio::select;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
use tracing::{debug, warn}; use tracing::{debug, error};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Ldap { pub struct Ldap {
@ -51,9 +51,10 @@ impl Ldap {
select! { select! {
res = conn.drive() => { res = conn.drive() => {
if let Err(err) = res { if let Err(err) = res {
warn!("LDAP connection error: {}", err); error!("LDAP connection error: {}", err);
} else { } else {
debug!("LDAP drive has stopped, this should not happen?"); error!("LDAP connection lost");
token.cancel();
} }
} }
_ = token.cancelled() => { _ = token.cancelled() => {

View File

@ -15,15 +15,20 @@ use siranga::web::{ForwardAuth, Service};
use tokio::net::TcpListener; use tokio::net::TcpListener;
use tokio::select; use tokio::select;
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
use tracing::{error, info, warn}; use tracing::{debug, error, info, warn};
use tracing_subscriber::EnvFilter; use tracing_subscriber::EnvFilter;
use tracing_subscriber::layer::SubscriberExt; use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt; use tracing_subscriber::util::SubscriberInitExt;
async fn shutdown_task(token: CancellationToken) { async fn shutdown_task(token: CancellationToken) {
tokio::signal::ctrl_c() select! {
.await _ = tokio::signal::ctrl_c() => {
.expect("Failed to listen for ctrl-c event"); debug!("Received SIGINT");
}
_ = token.cancelled() => {
debug!("Application called for graceful shutdown");
}
}
info!("Starting graceful shutdown"); info!("Starting graceful shutdown");
token.cancel(); token.cancel();
tokio::time::sleep(Duration::from_secs(5)).await; tokio::time::sleep(Duration::from_secs(5)).await;