feat(connection): add connection attempt cancellation

This commit is contained in:
Moe. authored and GitHub committed 2026-07-04 23:47:53 +08:00
1 parent 454457f191
commit 8a1a791927
16 files changed
+681 -51

No files matched your search

+54 -7
View File
@@ -807,11 +807,15 @@ pub async fn test_connection(state: State<'_, Arc<AppState>>, config: Connection
}
#[tauri::command]
pub async fn connect_db(state: State<'_, Arc<AppState>>, config: ConnectionConfig) -> Result<String, String> {
pub async fn connect_db(
state: State<'_, Arc<AppState>>,
config: ConnectionConfig,
client_attempt: Option<u64>,
) -> Result<String, String> {
let config = config.canonicalized();
let id = config.id.clone();
let db_config = metadata_connection_config(&config);
let attempt = state.begin_connection_attempt(&id).await;
let attempt = state.begin_connection_attempt_with_client_attempt(&id, client_attempt).await;
let mut connected_config = config.clone();
let mut connected_db_config = db_config.clone();
@@ -819,7 +823,15 @@ pub async fn connect_db(state: State<'_, Arc<AppState>>, config: ConnectionConfi
state.reset_connection_transport_for_config(&id, &db_config).await;
let (host, port) = state.connection_host_port(&id, &db_config).await?;
if let Err(err) = state.ensure_current_connection_attempt(&id, Some(attempt)).await {
state.reset_connection_transport_for_config(&id, &db_config).await;
return Err(err);
}
probe_connection_endpoint(&db_config, &host, port).await?;
if let Err(err) = state.ensure_current_connection_attempt(&id, Some(attempt)).await {
state.reset_connection_transport_for_config(&id, &db_config).await;
return Err(err);
}
let url = connection_url_for_endpoint(&db_config, &host, port);
let connect_timeout = std::time::Duration::from_secs(db_config.effective_connect_timeout_secs());
let idle_timeout = std::time::Duration::from_secs(db_config.idle_timeout_secs);
@@ -885,14 +897,17 @@ pub async fn connect_db(state: State<'_, Arc<AppState>>, config: ConnectionConfi
if mongo_uses_legacy_driver(&db_config) {
let mut client =
state.agent_manager.spawn(&db_config.db_type, Some(MONGO_LEGACY_DRIVER_PROFILE)).await?;
state.ensure_current_connection_attempt(&id, Some(attempt)).await?;
client
.connect(mongo_legacy_connect_params(&db_config, &host, port))
.await
.map_err(|err| mongo_legacy_error_with_auth_hint(&err))?;
state.ensure_current_connection_attempt(&id, Some(attempt)).await?;
PoolKind::Agent(std::sync::Arc::new(tokio::sync::Mutex::new(client)))
} else {
let native_err = match db::mongo_driver::connect(&url, connect_timeout, idle_timeout).await {
Ok(client) => {
state.ensure_current_connection_attempt(&id, Some(attempt)).await?;
match db::mongo_driver::test_connection(
&client,
connect_timeout,
@@ -901,7 +916,8 @@ pub async fn connect_db(state: State<'_, Arc<AppState>>, config: ConnectionConfi
.await
{
Ok(()) => {
state
state.ensure_current_connection_attempt(&id, Some(attempt)).await?;
if let Err(err) = state
.insert_connection_pool_for_attempt(
&id,
attempt,
@@ -909,7 +925,11 @@ pub async fn connect_db(state: State<'_, Arc<AppState>>, config: ConnectionConfi
PoolKind::MongoDb(client),
&db_config,
)
.await?;
.await
{
state.reset_connection_transport_for_config(&id, &db_config).await;
return Err(err);
}
state.configs.write().await.insert(id.clone(), config);
return Ok(id);
}
@@ -922,12 +942,14 @@ pub async fn connect_db(state: State<'_, Arc<AppState>>, config: ConnectionConfi
log::info!("Native MongoDB driver failed ({native_err}), falling back to agent driver");
let mut client =
state.agent_manager.spawn(&db_config.db_type, Some(MONGO_LEGACY_DRIVER_PROFILE)).await?;
state.ensure_current_connection_attempt(&id, Some(attempt)).await?;
client.connect(mongo_legacy_connect_params(&db_config, &host, port)).await.map_err(|err| {
format!(
"{native_err}\n\nFallback with MongoDB (Legacy) driver failed: {}",
mongo_legacy_error_with_auth_hint(&err)
)
})?;
state.ensure_current_connection_attempt(&id, Some(attempt)).await?;
mark_mongo_legacy_driver(&mut connected_config);
connected_db_config = metadata_connection_config(&connected_config);
persist_mongo_legacy_driver_profile(state.inner(), &connected_config).await?;
@@ -1062,10 +1084,18 @@ pub async fn connect_db(state: State<'_, Arc<AppState>>, config: ConnectionConfi
return Err(err);
}
};
if let Err(err) = state.ensure_current_connection_attempt(&id, Some(attempt)).await {
state.mq_registry.drop_connection(&id).await;
return Err(err);
}
if let Err(err) = adapter.test_connection().await {
state.mq_registry.drop_connection(&id).await;
return Err(err);
}
if let Err(err) = state.ensure_current_connection_attempt(&id, Some(attempt)).await {
state.mq_registry.drop_connection(&id).await;
return Err(err);
}
PoolKind::MessageQueue
}
#[cfg(not(feature = "mq-admin"))]
@@ -1086,7 +1116,12 @@ pub async fn connect_db(state: State<'_, Arc<AppState>>, config: ConnectionConfi
db_type => return Err(format!("Unsupported database type: {db_type:?}")),
};
state.insert_connection_pool_for_attempt(&id, attempt, id.clone(), pool, &connected_db_config).await?;
if let Err(err) =
state.insert_connection_pool_for_attempt(&id, attempt, id.clone(), pool, &connected_db_config).await
{
state.reset_connection_transport_for_config(&id, &connected_db_config).await;
return Err(err);
}
state.configs.write().await.insert(id.clone(), connected_config);
Ok(id)
@@ -1111,8 +1146,20 @@ pub async fn connection_final_proxy_port(
}
#[tauri::command]
pub async fn disconnect_db(state: State<'_, Arc<AppState>>, connection_id: String) -> Result<(), String> {
state.supersede_connection_attempt(&connection_id).await;
pub async fn disconnect_db(
state: State<'_, Arc<AppState>>,
connection_id: String,
client_attempt: Option<u64>,
) -> Result<(), String> {
let should_disconnect = if let Some(client_attempt) = client_attempt {
state.supersede_connection_attempt_if_client_attempt(&connection_id, client_attempt).await
} else {
state.supersede_connection_attempt(&connection_id).await;
true
};
if !should_disconnect {
return Ok(());
}
state.remove_connection_pools_detached(&connection_id).await;
drop_nacos_adapters_for_connection_ids(state.inner(), std::slice::from_ref(&connection_id)).await;
drop_mq_adapters_for_connection_ids(state.inner(), std::slice::from_ref(&connection_id)).await;