fix(core): avoid holding pool registry locks during db io

This commit is contained in:
Guoyu Su authored and GitHub committed 2026-09-03 01:53:42 +08:00
1 parent 616c0daf25
commit d6fc1fe52c
30 files changed
+1436 -1110

No files matched your search

+80 -41
View File
@@ -256,10 +256,7 @@ async fn sync_mongo_legacy_driver_fallback(state: &WebState, config: &Connection
if config.db_type != DatabaseType::MongoDb {
return Ok(());
}
let uses_legacy_agent = {
let connections = state.app.connections.read().await;
matches!(connections.get(&config.id), Some(PoolKind::Agent(_)))
};
let uses_legacy_agent = matches!(state.app.pool_handle(&config.id).await, Some(PoolKind::Agent(_)));
if !uses_legacy_agent {
return Ok(());
}
@@ -314,12 +311,9 @@ async fn run_temporary_connection_test(
// runs before either a successful result or an error is returned.
let result: Result<ConnectionTestResult, String> = async {
let success_message = if pool_result.is_ok() && config.db_type == DatabaseType::Consul {
let client = {
let connections = app.connections.read().await;
match connections.get(&temp_id) {
Some(PoolKind::Consul(client)) => Some(client.clone()),
_ => None,
}
let client = match app.pool_handle(&temp_id).await {
Some(PoolKind::Consul(client)) => Some(client),
_ => None,
};
if let Some(client) = client {
let configured_target =
@@ -1040,7 +1034,7 @@ mod tests {
assert_eq!(detailed.0.message, "Connection successful");
assert_eq!(detailed.0.database_info, None);
assert!(state.app.configs.read().await.keys().all(|key| !key.starts_with("__test_")));
assert!(state.app.connections.read().await.keys().all(|key| !key.starts_with("__test_")));
assert!(state.app.with_connection_pools(|pools| pools.keys().all(|key| !key.starts_with("__test_"))).await);
let _ = std::fs::remove_dir_all(dir);
}
@@ -1055,7 +1049,7 @@ mod tests {
assert!(error.contains("CONSUL_AGENT_TARGET_MISMATCH"), "unexpected error: {error}");
assert!(state.app.configs.read().await.keys().all(|key| !key.starts_with("__test_")));
assert!(state.app.connections.read().await.keys().all(|key| !key.starts_with("__test_")));
assert!(state.app.with_connection_pools(|pools| pools.keys().all(|key| !key.starts_with("__test_"))).await);
let _ = std::fs::remove_dir_all(dir);
}
@@ -1103,7 +1097,7 @@ mod tests {
assert_eq!(result.0, "Connection successful");
assert!(state.app.configs.read().await.keys().all(|key| !key.starts_with("__test_")));
assert!(state.app.connections.read().await.keys().all(|key| !key.starts_with("__test_")));
assert!(state.app.with_connection_pools(|pools| pools.keys().all(|key| !key.starts_with("__test_"))).await);
let cached = state.app.mq_registry.cached_connection_ids().await;
assert!(
cached.iter().all(|id| !id.starts_with("__test_")),
@@ -1126,7 +1120,12 @@ mod tests {
.await
.unwrap();
state.app.configs.write().await.insert(initial.id.clone(), initial.clone());
state.app.connections.write().await.insert(initial.id.clone(), PoolKind::Sqlite(pool.clone()));
state
.app
.update_connection_pools(|connections| {
connections.insert(initial.id.clone(), PoolKind::Sqlite(pool.clone()));
})
.await;
let mut invalid = initial.clone();
invalid.attached_databases.push(AttachedDatabaseConfig {
@@ -1163,7 +1162,7 @@ mod tests {
.unwrap_err();
assert!(proxy_error.message.contains("in-memory main database"), "{}", proxy_error.message);
assert!(state.app.connections.read().await.contains_key(&initial.id));
assert!(state.app.pool_handle(&initial.id).await.is_some());
assert_eq!(state.app.configs.read().await.get(&initial.id), Some(&initial));
let retained = dbx_core::db::sqlite::execute_query(&pool, "SELECT value FROM retained;").await.unwrap();
assert_eq!(retained.rows[0][0], serde_json::json!("yes"));
@@ -1359,7 +1358,12 @@ mod tests {
let config = mq_config("mq-info", "http://127.0.0.1:8080");
state.app.storage.save_connections(std::slice::from_ref(&config)).await.unwrap();
state.app.configs.write().await.insert(config.id.clone(), config.clone());
state.app.connections.write().await.insert(config.id.clone(), PoolKind::MessageQueue);
state
.app
.update_connection_pools(|connections| {
connections.insert(config.id.clone(), PoolKind::MessageQueue);
})
.await;
let database_info = DatabaseConnectionInfo {
product_name: Some("Apache Pulsar".to_string()),
product_version: Some("3.3.0".to_string()),
@@ -1376,7 +1380,7 @@ mod tests {
.await;
assert!(result.is_ok());
assert!(state.app.connections.read().await.contains_key(&config.id));
assert!(state.app.pool_handle(&config.id).await.is_some());
assert_eq!(state.app.configs.read().await[&config.id].database_info, Some(database_info.clone()));
assert_eq!(state.app.storage.load_connections().await.unwrap()[0].database_info, Some(database_info));
@@ -1389,7 +1393,12 @@ mod tests {
let (state, dir) = test_web_state().await;
let initial = mq_config("mq-conn", "http://127.0.0.1:8080");
state.app.configs.write().await.insert(initial.id.clone(), initial.clone());
state.app.connections.write().await.insert(initial.id.clone(), PoolKind::MessageQueue);
state
.app
.update_connection_pools(|connections| {
connections.insert(initial.id.clone(), PoolKind::MessageQueue);
})
.await;
let first = state.app.mq_registry.get_or_build(&initial).await.unwrap().adapter;
let updated = mq_config("mq-conn", "http://127.0.0.1:8081");
@@ -1415,7 +1424,7 @@ mod tests {
let second = state.app.mq_registry.get_or_build(&updated).await.unwrap().adapter;
assert!(!Arc::ptr_eq(&first, &second));
assert!(!state.app.connections.read().await.contains_key(&initial.id));
assert!(!state.app.pool_handle(&initial.id).await.is_some());
let _ = std::fs::remove_dir_all(dir);
}
@@ -1426,7 +1435,12 @@ mod tests {
let (state, dir) = test_web_state().await;
let initial = mq_config("mq-conn", "http://127.0.0.1:8080");
state.app.configs.write().await.insert(initial.id.clone(), initial.clone());
state.app.connections.write().await.insert(initial.id.clone(), PoolKind::MessageQueue);
state
.app
.update_connection_pools(|connections| {
connections.insert(initial.id.clone(), PoolKind::MessageQueue);
})
.await;
let first = state.app.mq_registry.get_or_build(&initial).await.unwrap().adapter;
let updated = mq_config("mq-conn", &spawn_pulsar_clusters_server().await);
@@ -1648,7 +1662,12 @@ mod tests {
let updated = mq_config("mq-conn", "http://127.0.0.1:8081");
state.app.storage.save_connections(std::slice::from_ref(&updated)).await.unwrap();
state.app.configs.write().await.insert(initial.id.clone(), initial.clone());
state.app.connections.write().await.insert(initial.id.clone(), PoolKind::MessageQueue);
state
.app
.update_connection_pools(|connections| {
connections.insert(initial.id.clone(), PoolKind::MessageQueue);
})
.await;
let result = load_connections(State(state.clone()), HeaderMap::new()).await;
assert!(result.is_ok());
@@ -1661,7 +1680,7 @@ mod tests {
.and_then(serde_json::Value::as_str);
assert_eq!(cached_admin_url, Some("http://127.0.0.1:8081"));
drop(configs);
assert!(!state.app.connections.read().await.contains_key(&initial.id));
assert!(!state.app.pool_handle(&initial.id).await.is_some());
let _ = std::fs::remove_dir_all(dir);
}
@@ -1838,7 +1857,12 @@ mod tests {
configs.insert(kept.id.clone(), kept.clone());
configs.insert(removed.id.clone(), removed.clone());
}
state.app.connections.write().await.insert(removed.id.clone(), PoolKind::MessageQueue);
state
.app
.update_connection_pools(|connections| {
connections.insert(removed.id.clone(), PoolKind::MessageQueue);
})
.await;
let result = save_connections(
State(state.clone()),
@@ -1848,7 +1872,7 @@ mod tests {
.await;
assert!(result.is_ok());
assert!(!state.app.connections.read().await.contains_key(&removed.id));
assert!(!state.app.pool_handle(&removed.id).await.is_some());
let _ = std::fs::remove_dir_all(dir);
}
@@ -1863,11 +1887,13 @@ mod tests {
let conn_pool = dbx_core::db::sqlite::connect_path(&conn_path.to_string_lossy()).await.unwrap();
let conn2_pool = dbx_core::db::sqlite::connect_path(&conn2_path.to_string_lossy()).await.unwrap();
{
let mut connections = state.app.connections.write().await;
connections.insert("conn".to_string(), PoolKind::Sqlite(conn_pool));
connections.insert("conn2".to_string(), PoolKind::Sqlite(conn2_pool));
}
state
.app
.update_connection_pools(|connections| {
connections.insert("conn".to_string(), PoolKind::Sqlite(conn_pool));
connections.insert("conn2".to_string(), PoolKind::Sqlite(conn2_pool));
})
.await;
let result = disconnect_db(
State(state.clone()),
@@ -1876,9 +1902,10 @@ mod tests {
.await;
assert!(result.is_ok());
let connections = state.app.connections.read().await;
assert!(!connections.contains_key("conn"));
assert!(connections.contains_key("conn2"));
let (has_conn, has_conn2) =
state.app.with_connection_pools(|pools| (pools.contains_key("conn"), pools.contains_key("conn2"))).await;
assert!(!has_conn);
assert!(has_conn2);
let _ = std::fs::remove_dir_all(dir);
}
@@ -1891,7 +1918,12 @@ mod tests {
let conn_pool = dbx_core::db::sqlite::connect_path(&conn_path.to_string_lossy()).await.unwrap();
state.app.begin_connection_attempt_with_client_attempt("conn", Some(1)).await;
let current_attempt = state.app.begin_connection_attempt_with_client_attempt("conn", Some(2)).await;
state.app.connections.write().await.insert("conn".to_string(), PoolKind::Sqlite(conn_pool));
state
.app
.update_connection_pools(|connections| {
connections.insert("conn".to_string(), PoolKind::Sqlite(conn_pool));
})
.await;
let result = disconnect_db(
State(state.clone()),
@@ -1900,7 +1932,7 @@ mod tests {
.await;
assert!(result.is_ok());
assert!(state.app.connections.read().await.contains_key("conn"));
assert!(state.app.pool_handle("conn").await.is_some());
assert!(state.app.ensure_current_connection_attempt("conn", Some(current_attempt)).await.is_ok());
let result = disconnect_db(
@@ -1910,7 +1942,7 @@ mod tests {
.await;
assert!(result.is_ok());
assert!(!state.app.connections.read().await.contains_key("conn"));
assert!(!state.app.pool_handle("conn").await.is_some());
assert!(state.app.ensure_current_connection_attempt("conn", Some(current_attempt)).await.is_err());
let _ = std::fs::remove_dir_all(dir);
@@ -1923,10 +1955,12 @@ mod tests {
std::fs::File::create(&conn_path).unwrap();
let conn_pool = dbx_core::db::sqlite::connect_path(&conn_path.to_string_lossy()).await.unwrap();
{
let mut connections = state.app.connections.write().await;
connections.insert("conn".to_string(), PoolKind::Sqlite(conn_pool));
}
state
.app
.update_connection_pools(|connections| {
connections.insert("conn".to_string(), PoolKind::Sqlite(conn_pool));
})
.await;
{
let mut configs = state.app.configs.write().await;
configs.insert("conn".to_string(), sqlite_config("conn", &conn_path.to_string_lossy()));
@@ -1951,7 +1985,12 @@ mod tests {
let (state, dir) = test_web_state().await;
let config = mq_config("mq-conn", "http://127.0.0.1:8080");
state.app.configs.write().await.insert(config.id.clone(), config.clone());
state.app.connections.write().await.insert(config.id.clone(), PoolKind::MessageQueue);
state
.app
.update_connection_pools(|connections| {
connections.insert(config.id.clone(), PoolKind::MessageQueue);
})
.await;
let first = state.app.mq_registry.get_or_build(&config).await.unwrap().adapter;
let result = disconnect_db(
@@ -1961,7 +2000,7 @@ mod tests {
.await;
assert!(result.is_ok());
assert!(!state.app.connections.read().await.contains_key(&config.id));
assert!(!state.app.pool_handle(&config.id).await.is_some());
let second = state.app.mq_registry.get_or_build(&config).await.unwrap().adapter;
assert!(!Arc::ptr_eq(&first, &second));