From 1d2d348dd84a0098410eda78161654471638d793 Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Fri, 12 Jun 2026 12:29:35 +0800 Subject: [PATCH] fix(db): refresh stale native connection pools --- crates/dbx-core/src/connection.rs | 48 +++++++++++++++++++++++++- crates/dbx-core/src/db/redis_driver.rs | 24 +++++++++++++ crates/dbx-core/src/db/sqlserver.rs | 9 +++++ crates/dbx-core/src/redis_ops.rs | 27 +++++++++++++++ 4 files changed, 107 insertions(+), 1 deletion(-) diff --git a/crates/dbx-core/src/connection.rs b/crates/dbx-core/src/connection.rs index fdb0707ca4..f602553097 100644 --- a/crates/dbx-core/src/connection.rs +++ b/crates/dbx-core/src/connection.rs @@ -324,7 +324,10 @@ impl AppState { let conns = self.connections.read().await; if conns.contains_key(&pool_key) { - return Ok(pool_key); + drop(conns); + if !self.remove_stale_connection_pool(&pool_key).await { + return Ok(pool_key); + } } else { drop(conns); } @@ -675,6 +678,49 @@ impl AppState { Ok(("127.0.0.1".to_string(), local_port)) } + async fn remove_stale_connection_pool(&self, pool_key: &str) -> bool { + let stale = { + let connections = self.connections.read().await; + let Some(pool) = connections.get(pool_key) else { + return false; + }; + match pool { + PoolKind::SqlServer(client) => { + let client = client.clone(); + drop(connections); + let mut client = client.lock().await; + match db::sqlserver::test_connection(&mut client).await { + Ok(()) => false, + Err(err) => { + log::warn!("SQL Server connection pool '{pool_key}' is stale: {err}"); + true + } + } + } + PoolKind::Redis(redis) => match db::redis_driver::test_connection(redis).await { + Ok(()) => false, + Err(err) => { + log::warn!("Redis connection pool '{pool_key}' is stale: {err}"); + true + } + }, + _ => false, + } + }; + + if !stale { + return false; + } + + let removed = self.connections.write().await.remove(pool_key); + if let Some(pool) = removed { + close_pool_kind(pool).await; + true + } else { + false + } + } + pub async fn reconnect_pool(&self, connection_id: &str, database: Option<&str>) -> Result { self.reconnect_pool_for_session(connection_id, database, None).await } diff --git a/crates/dbx-core/src/db/redis_driver.rs b/crates/dbx-core/src/db/redis_driver.rs index 92493c8150..9df309ebe7 100644 --- a/crates/dbx-core/src/db/redis_driver.rs +++ b/crates/dbx-core/src/db/redis_driver.rs @@ -237,6 +237,30 @@ pub async fn connect_cluster(config: &ConnectionConfig) -> Result Result<(), String> { + match connection { + RedisConnection::Direct(con) => { + let mut con = con.lock().await; + redis_ping(&mut *con, "Redis").await + } + RedisConnection::Cluster(cluster) => { + let mut con = cluster.connection.lock().await; + redis_ping(&mut *con, "Redis cluster").await + } + } +} + +async fn redis_ping(con: &mut C, label: &str) -> Result<(), String> +where + C: ConnectionLike + Send + Sync + Unpin, +{ + tokio::time::timeout(super::connection_timeout(), redis::cmd("PING").query_async::(con)) + .await + .map_err(|_| format!("{label} ping timed out ({}s)", super::CONNECTION_TIMEOUT_SECS))? + .map(|_| ()) + .map_err(|e| format!("{label} ping failed: {e}")) +} + fn redis_sentinel_nodes(config: &ConnectionConfig) -> Result, String> { let raw_nodes = config.redis_sentinel_nodes.trim(); let endpoints: Vec = if raw_nodes.is_empty() { diff --git a/crates/dbx-core/src/db/sqlserver.rs b/crates/dbx-core/src/db/sqlserver.rs index 3baa67c67e..268d72ddf1 100644 --- a/crates/dbx-core/src/db/sqlserver.rs +++ b/crates/dbx-core/src/db/sqlserver.rs @@ -601,6 +601,12 @@ pub async fn list_databases(client: &mut SqlServerClient) -> Result(0).unwrap_or("").to_string() }).collect()) } +pub async fn test_connection(client: &mut SqlServerClient) -> Result<(), String> { + let stream = client.simple_query("SELECT 1").await.map_err(|e| e.to_string())?; + let _ = stream.into_first_result().await.map_err(|e| e.to_string())?; + Ok(()) +} + pub async fn list_schemas(client: &mut SqlServerClient) -> Result, String> { let stream = client .query( @@ -891,6 +897,8 @@ pub async fn list_foreign_keys( ref_schema: Some(row.get::<&str, _>(2).unwrap_or("").to_string()), ref_table: row.get::<&str, _>(3).unwrap_or("").to_string(), ref_column: row.get::<&str, _>(4).unwrap_or("").to_string(), + on_update: None, + on_delete: None, }) .collect()) } @@ -940,6 +948,7 @@ pub async fn list_triggers( name: row.get::<&str, _>(0).unwrap_or("").to_string(), event: row.get::<&str, _>(1).unwrap_or("").to_string(), timing: row.get::<&str, _>(2).unwrap_or("AFTER").to_string(), + statement: None, }) .collect()) } diff --git a/crates/dbx-core/src/redis_ops.rs b/crates/dbx-core/src/redis_ops.rs index f083804c2c..712e53e524 100644 --- a/crates/dbx-core/src/redis_ops.rs +++ b/crates/dbx-core/src/redis_ops.rs @@ -3,10 +3,15 @@ use crate::db::redis_driver::{ self, RedisCommandResult, RedisConnection, RedisDatabaseInfo, RedisScanResult, RedisValue, }; +async fn ensure_redis_pool(state: &AppState, connection_id: &str) -> Result<(), String> { + state.get_or_create_pool(connection_id, None).await.map(|_| ()) +} + pub async fn redis_list_databases_core( state: &AppState, connection_id: &str, ) -> Result, String> { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; let pool = connections.get(connection_id).ok_or("Connection not found")?; match pool { @@ -29,6 +34,7 @@ pub async fn redis_scan_keys_core( pattern: &str, count: usize, ) -> Result { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; let pool = connections.get(connection_id).ok_or("Connection not found")?; match pool { @@ -57,6 +63,7 @@ pub async fn redis_scan_values_core( include_key_matches: bool, count: usize, ) -> Result { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; let pool = connections.get(connection_id).ok_or("Connection not found")?; match pool { @@ -86,6 +93,7 @@ pub async fn redis_get_value_in_db_core( db: u32, key_raw: &str, ) -> Result { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; let pool = connections.get(connection_id).ok_or("Connection not found")?; match pool { @@ -126,6 +134,7 @@ pub async fn redis_set_string_in_db_core( value: &str, ttl: Option, ) -> Result<(), String> { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; let pool = connections.get(connection_id).ok_or("Connection not found")?; match pool { @@ -158,6 +167,7 @@ pub async fn redis_delete_key_in_db_core( db: u32, key_raw: &str, ) -> Result<(), String> { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; let pool = connections.get(connection_id).ok_or("Connection not found")?; match pool { @@ -200,6 +210,7 @@ pub async fn redis_hash_set_in_db_core( value: &str, ttl: Option, ) -> Result<(), String> { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; match connections.get(connection_id).ok_or("Not found")? { PoolKind::Redis(redis) => { @@ -232,6 +243,7 @@ pub async fn redis_hash_del_in_db_core( key_raw: &str, field: &str, ) -> Result<(), String> { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; match connections.get(connection_id).ok_or("Not found")? { PoolKind::Redis(redis) => { @@ -271,6 +283,7 @@ pub async fn redis_list_push_in_db_core( value: &str, ttl: Option, ) -> Result<(), String> { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; match connections.get(connection_id).ok_or("Not found")? { PoolKind::Redis(redis) => { @@ -300,6 +313,7 @@ pub async fn redis_list_set_in_db_core( index: i64, value: &str, ) -> Result<(), String> { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; match connections.get(connection_id).ok_or("Not found")? { PoolKind::Redis(redis) => { @@ -337,6 +351,7 @@ pub async fn redis_list_remove_in_db_core( key_raw: &str, index: i64, ) -> Result<(), String> { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; match connections.get(connection_id).ok_or("Not found")? { PoolKind::Redis(redis) => { @@ -376,6 +391,7 @@ pub async fn redis_set_add_in_db_core( member: &str, ttl: Option, ) -> Result<(), String> { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; match connections.get(connection_id).ok_or("Not found")? { PoolKind::Redis(redis) => { @@ -413,6 +429,7 @@ pub async fn redis_set_remove_in_db_core( key_raw: &str, member: &str, ) -> Result<(), String> { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; match connections.get(connection_id).ok_or("Not found")? { PoolKind::Redis(redis) => { @@ -443,6 +460,7 @@ pub async fn redis_zadd_in_db_core( score: f64, ttl: Option, ) -> Result<(), String> { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; match connections.get(connection_id).ok_or("Not found")? { PoolKind::Redis(redis) => { @@ -471,6 +489,7 @@ pub async fn redis_zrem_in_db_core( key_raw: &str, member: &str, ) -> Result<(), String> { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; match connections.get(connection_id).ok_or("Not found")? { PoolKind::Redis(redis) => { @@ -501,6 +520,7 @@ pub async fn redis_stream_add_in_db_core( fields: Vec<(String, String)>, ttl: Option, ) -> Result<(), String> { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; match connections.get(connection_id).ok_or("Not found")? { PoolKind::Redis(redis) => { @@ -530,6 +550,7 @@ pub async fn redis_json_set_in_db_core( value: &str, ttl: Option, ) -> Result<(), String> { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; match connections.get(connection_id).ok_or("Not found")? { PoolKind::Redis(redis) => { @@ -556,6 +577,7 @@ pub async fn redis_check_json_module_in_db_core( connection_id: &str, db: u32, ) -> Result { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; match connections.get(connection_id).ok_or("Not found")? { PoolKind::Redis(redis) => match redis { @@ -581,6 +603,7 @@ pub async fn redis_set_ttl_in_db_core( key_raw: &str, ttl: i64, ) -> Result<(), String> { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; match connections.get(connection_id).ok_or("Not found")? { PoolKind::Redis(redis) => { @@ -608,6 +631,7 @@ pub async fn redis_delete_keys_in_db_core( db: u32, key_raws: &[String], ) -> Result { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; match connections.get(connection_id).ok_or("Not found")? { PoolKind::Redis(redis) => { @@ -636,6 +660,7 @@ pub async fn redis_delete_keys_in_db_core( } pub async fn redis_flush_db_core(state: &AppState, connection_id: &str, db: u32) -> Result<(), String> { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; match connections.get(connection_id).ok_or("Not found")? { PoolKind::Redis(redis) => match redis { @@ -660,6 +685,7 @@ pub async fn redis_execute_command_core( command: &str, skip_safety_check: bool, ) -> Result { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; match connections.get(connection_id).ok_or("Not found")? { PoolKind::Redis(redis) => match redis { @@ -692,6 +718,7 @@ pub async fn redis_load_more_in_db_core( cursor: u64, count: usize, ) -> Result { + ensure_redis_pool(state, connection_id).await?; let connections = state.connections.read().await; match connections.get(connection_id).ok_or("Not found")? { PoolKind::Redis(redis) => {