fix(connection): recover connection state after VPN failures (#641)
This commit is contained in:
1 parent
3ebfae05a2
commit
9a0a32e04c
8 files changed
+469
-26
No files matched your search
@@ -126,6 +126,7 @@ pub struct AppState {
|
||||
pub connections: Arc<RwLock<HashMap<String, PoolKind>>>,
|
||||
keepalive_tasks: Arc<RwLock<HashMap<String, JoinHandle<()>>>>,
|
||||
pool_activity: Arc<RwLock<HashMap<String, PoolActivity>>>,
|
||||
connection_attempts: RwLock<HashMap<String, u64>>,
|
||||
pub configs: RwLock<HashMap<String, ConnectionConfig>>,
|
||||
pub running_queries: RunningQueries,
|
||||
pub tunnels: TunnelManager,
|
||||
@@ -351,6 +352,7 @@ impl AppState {
|
||||
connections: Arc::new(RwLock::new(HashMap::new())),
|
||||
keepalive_tasks: Arc::new(RwLock::new(HashMap::new())),
|
||||
pool_activity: Arc::new(RwLock::new(HashMap::new())),
|
||||
connection_attempts: RwLock::new(HashMap::new()),
|
||||
configs: RwLock::new(HashMap::new()),
|
||||
running_queries: RunningQueries::default(),
|
||||
tunnels: TunnelManager::new(),
|
||||
@@ -423,6 +425,48 @@ impl AppState {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn begin_connection_attempt(&self, connection_id: &str) -> u64 {
|
||||
let mut attempts = self.connection_attempts.write().await;
|
||||
let next = attempts.get(connection_id).copied().unwrap_or(0).wrapping_add(1);
|
||||
attempts.insert(connection_id.to_string(), next);
|
||||
next
|
||||
}
|
||||
|
||||
pub async fn supersede_connection_attempt(&self, connection_id: &str) {
|
||||
self.begin_connection_attempt(connection_id).await;
|
||||
}
|
||||
|
||||
async fn connection_attempt_is_current(&self, connection_id: &str, attempt: u64) -> bool {
|
||||
self.connection_attempts.read().await.get(connection_id).copied() == Some(attempt)
|
||||
}
|
||||
|
||||
async fn ensure_current_connection_attempt(&self, connection_id: &str, attempt: Option<u64>) -> Result<(), String> {
|
||||
let Some(attempt) = attempt else {
|
||||
return Ok(());
|
||||
};
|
||||
if self.connection_attempt_is_current(connection_id, attempt).await {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("Connection attempt was superseded by a newer attempt".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn insert_connection_pool_for_attempt(
|
||||
&self,
|
||||
connection_id: &str,
|
||||
attempt: u64,
|
||||
pool_key: String,
|
||||
pool: PoolKind,
|
||||
config: &ConnectionConfig,
|
||||
) -> Result<(), String> {
|
||||
if let Err(err) = self.ensure_current_connection_attempt(connection_id, Some(attempt)).await {
|
||||
close_pool_kind(pool).await;
|
||||
return Err(err);
|
||||
}
|
||||
self.insert_connection_pool(pool_key, pool, config).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn start_keepalive_task(&self, pool_key: &str, pool: &PoolKind, config: &ConnectionConfig) {
|
||||
let interval_secs = config.keepalive_interval_secs;
|
||||
let idle_timeout_secs = config.idle_timeout_secs;
|
||||
@@ -537,11 +581,30 @@ impl AppState {
|
||||
self.get_or_create_pool_for_session(connection_id, database, None).await
|
||||
}
|
||||
|
||||
pub async fn get_or_create_pool_for_connection_attempt(
|
||||
&self,
|
||||
connection_id: &str,
|
||||
database: Option<&str>,
|
||||
attempt: u64,
|
||||
) -> Result<String, String> {
|
||||
self.get_or_create_pool_for_session_inner(connection_id, database, None, Some(attempt)).await
|
||||
}
|
||||
|
||||
pub async fn get_or_create_pool_for_session(
|
||||
&self,
|
||||
connection_id: &str,
|
||||
database: Option<&str>,
|
||||
client_session_id: Option<&str>,
|
||||
) -> Result<String, String> {
|
||||
self.get_or_create_pool_for_session_inner(connection_id, database, client_session_id, None).await
|
||||
}
|
||||
|
||||
async fn get_or_create_pool_for_session_inner(
|
||||
&self,
|
||||
connection_id: &str,
|
||||
database: Option<&str>,
|
||||
client_session_id: Option<&str>,
|
||||
connection_attempt: Option<u64>,
|
||||
) -> Result<String, String> {
|
||||
let db_type = {
|
||||
let configs = self.configs.read().await;
|
||||
@@ -693,6 +756,7 @@ impl AppState {
|
||||
if self.connections.read().await.contains_key(&pool_key) {
|
||||
return Ok(pool_key);
|
||||
}
|
||||
self.ensure_current_connection_attempt(connection_id, connection_attempt).await?;
|
||||
self.insert_connection_pool(pool_key.clone(), PoolKind::MongoDb(client), &db_config)
|
||||
.await;
|
||||
return Ok(pool_key);
|
||||
@@ -924,6 +988,10 @@ impl AppState {
|
||||
}
|
||||
};
|
||||
|
||||
if let Err(err) = self.ensure_current_connection_attempt(connection_id, connection_attempt).await {
|
||||
close_pool_kind(pool).await;
|
||||
return Err(err);
|
||||
}
|
||||
self.insert_connection_pool(pool_key.clone(), pool, &db_config).await;
|
||||
Ok(pool_key)
|
||||
}
|
||||
@@ -2679,6 +2747,48 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn stale_connection_attempt_cannot_replace_newer_pool() {
|
||||
let (state, dir) = test_app_state().await;
|
||||
let mut config = mysql_config(None);
|
||||
config.name = "SQLite".to_string();
|
||||
config.db_type = DatabaseType::Sqlite;
|
||||
config.host = dir.join("current.db").to_string_lossy().to_string();
|
||||
let old_attempt = state.begin_connection_attempt("conn").await;
|
||||
let new_attempt = state.begin_connection_attempt("conn").await;
|
||||
let current_pool =
|
||||
db::sqlite::connect_path_create_if_missing(&dir.join("current.db").to_string_lossy()).await.unwrap();
|
||||
let stale_pool =
|
||||
db::sqlite::connect_path_create_if_missing(&dir.join("stale.db").to_string_lossy()).await.unwrap();
|
||||
|
||||
state
|
||||
.insert_connection_pool_for_attempt(
|
||||
"conn",
|
||||
new_attempt,
|
||||
"conn".to_string(),
|
||||
PoolKind::Sqlite(current_pool),
|
||||
&config,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let result = state
|
||||
.insert_connection_pool_for_attempt(
|
||||
"conn",
|
||||
old_attempt,
|
||||
"conn".to_string(),
|
||||
PoolKind::Sqlite(stale_pool),
|
||||
&config,
|
||||
)
|
||||
.await;
|
||||
|
||||
assert!(result.is_err());
|
||||
let conns = state.connections.read().await;
|
||||
assert!(matches!(conns.get("conn"), Some(PoolKind::Sqlite(_))));
|
||||
assert_eq!(conns.len(), 1);
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn jdbc_plugin_env_uses_managed_jre_when_installed() {
|
||||
let dir = std::env::temp_dir().join(format!("dbx-core-jdbc-managed-jre-{}", uuid::Uuid::new_v4()));
|
||||
|
||||
Reference in new issue
Block a user