feat: shutdown Database and Server on drop (#2241)

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
pull/24376/head
Raphael Taylor-Davies 2021-08-12 13:37:47 +01:00 committed by GitHub
parent caa47cf640
commit 2c4384625a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 1 deletions

View File

@ -10,7 +10,7 @@ use futures::{FutureExt, TryFutureExt};
use generated_types::database_rules::encode_database_rules;
use internal_types::freezable::Freezable;
use object_store::path::{ObjectStorePath, Path};
use observability_deps::tracing::{error, info};
use observability_deps::tracing::{error, info, warn};
use parking_lot::RwLock;
use persistence_windows::checkpoint::ReplayPlan;
use snafu::{ResultExt, Snafu};
@ -250,6 +250,20 @@ impl Database {
}
}
impl Drop for Database {
fn drop(&mut self) {
let db_name = &self.shared.config.name;
if !self.shared.shutdown.is_cancelled() {
warn!(%db_name, "database dropped without calling shutdown()");
self.shared.shutdown.cancel();
}
if self.join.clone().now_or_never().is_none() {
warn!(%db_name, "database dropped without waiting for worker termination");
}
}
}
/// State shared with the `Database` background worker
#[derive(Debug)]
struct DatabaseShared {

View File

@ -366,6 +366,19 @@ pub struct Server<M: ConnectionManager> {
shared: Arc<ServerShared>,
}
impl<M: ConnectionManager> Drop for Server<M> {
fn drop(&mut self) {
if !self.shared.shutdown.is_cancelled() {
warn!("server dropped without calling shutdown()");
self.shared.shutdown.cancel();
}
if self.join.clone().now_or_never().is_none() {
warn!("server dropped without waiting for worker termination");
}
}
}
#[derive(Debug)]
struct ServerShared {
/// A token that is used to trigger shutdown of the background worker