fix(db): refresh stale native pools
This commit is contained in:
1 parent
b575ec2729
commit
66e6ca1211
6 files changed
+61
-1
No files matched your search
@@ -145,12 +145,18 @@ pub async fn kv_delete_core(state: &AppState, connection_id: &str, key: &str) ->
|
||||
call_agent_kv(state, connection_id, AgentKvMethod::Delete, kv_delete_params(key)).await
|
||||
}
|
||||
|
||||
async fn ensure_agent_kv_pool(state: &AppState, connection_id: &str) -> Result<(), String> {
|
||||
state.get_or_create_pool(connection_id, None).await.map(|_| ())
|
||||
}
|
||||
|
||||
async fn call_agent_kv<T: serde::de::DeserializeOwned + Send + 'static>(
|
||||
state: &AppState,
|
||||
connection_id: &str,
|
||||
method: AgentKvMethod,
|
||||
params: serde_json::Value,
|
||||
) -> Result<T, String> {
|
||||
ensure_agent_kv_pool(state, connection_id).await?;
|
||||
|
||||
let connections = state.connections.read().await;
|
||||
let pool = connections.get(connection_id).ok_or("Connection not found")?;
|
||||
match pool {
|
||||
|
||||
@@ -823,6 +823,17 @@ impl AppState {
|
||||
return false;
|
||||
};
|
||||
match pool {
|
||||
PoolKind::Mysql(pool, _) => {
|
||||
let pool = pool.clone();
|
||||
drop(connections);
|
||||
match db::mysql::get_conn_with_health_check(&pool).await {
|
||||
Ok(_) => false,
|
||||
Err(err) => {
|
||||
log::warn!("MySQL connection pool '{pool_key}' is stale: {err}");
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
PoolKind::SqlServer(client) => {
|
||||
let client = client.clone();
|
||||
drop(connections);
|
||||
@@ -842,6 +853,27 @@ impl AppState {
|
||||
true
|
||||
}
|
||||
},
|
||||
PoolKind::MongoDb(client) => {
|
||||
let client = client.clone();
|
||||
drop(connections);
|
||||
let (connect_timeout, database) = {
|
||||
let configs = self.configs.read().await;
|
||||
let config = config_for_pool_key(pool_key, &configs);
|
||||
(
|
||||
config
|
||||
.map(|config| Duration::from_secs(config.effective_connect_timeout_secs().max(1)))
|
||||
.unwrap_or_else(|| Duration::from_secs(1)),
|
||||
config.and_then(|config| config.effective_database().map(str::to_string)),
|
||||
)
|
||||
};
|
||||
match db::mongo_driver::test_connection(&client, connect_timeout, database.as_deref()).await {
|
||||
Ok(()) => false,
|
||||
Err(err) => {
|
||||
log::warn!("MongoDB connection pool '{pool_key}' is stale: {err}");
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
};
|
||||
|
||||
@@ -12,7 +12,12 @@ fn sort_names(mut names: Vec<String>) -> Vec<String> {
|
||||
names
|
||||
}
|
||||
|
||||
async fn ensure_document_pool(state: &AppState, connection_id: &str) -> Result<(), String> {
|
||||
state.get_or_create_pool(connection_id, None).await.map(|_| ())
|
||||
}
|
||||
|
||||
pub async fn mongo_list_databases_core(state: &AppState, connection_id: &str) -> Result<Vec<String>, String> {
|
||||
ensure_document_pool(state, connection_id).await?;
|
||||
let fallback_database = configured_mongo_database(state, connection_id).await;
|
||||
let connections = state.connections.read().await;
|
||||
match connections.get(connection_id).ok_or("Not found")? {
|
||||
@@ -59,6 +64,7 @@ pub async fn mongo_list_collections_core(
|
||||
connection_id: &str,
|
||||
database: &str,
|
||||
) -> Result<Vec<String>, String> {
|
||||
ensure_document_pool(state, connection_id).await?;
|
||||
let connections = state.connections.read().await;
|
||||
match connections.get(connection_id).ok_or("Not found")? {
|
||||
PoolKind::MongoDb(client) => mongo_driver::list_collections(client, database).await.map(sort_names),
|
||||
@@ -82,6 +88,7 @@ pub async fn document_find_documents_core(
|
||||
filter: Option<&str>,
|
||||
sort: Option<&str>,
|
||||
) -> Result<MongoDocumentResult, String> {
|
||||
ensure_document_pool(state, connection_id).await?;
|
||||
let connections = state.connections.read().await;
|
||||
match connections.get(connection_id).ok_or("Not found")? {
|
||||
PoolKind::MongoDb(client) => {
|
||||
@@ -131,6 +138,7 @@ pub async fn mongo_aggregate_documents_core(
|
||||
pipeline_json: &str,
|
||||
max_rows: Option<usize>,
|
||||
) -> Result<MongoDocumentResult, String> {
|
||||
ensure_document_pool(state, connection_id).await?;
|
||||
let connections = state.connections.read().await;
|
||||
match connections.get(connection_id).ok_or("Not found")? {
|
||||
PoolKind::MongoDb(client) => {
|
||||
@@ -148,6 +156,7 @@ pub async fn mongo_insert_document_core(
|
||||
collection: &str,
|
||||
doc_json: &str,
|
||||
) -> Result<String, String> {
|
||||
ensure_document_pool(state, connection_id).await?;
|
||||
let connections = state.connections.read().await;
|
||||
match connections.get(connection_id).ok_or("Not found")? {
|
||||
PoolKind::MongoDb(client) => mongo_driver::insert_document(client, database, collection, doc_json).await,
|
||||
@@ -178,6 +187,7 @@ pub async fn mongo_insert_documents_core(
|
||||
collection: &str,
|
||||
docs_json: &str,
|
||||
) -> Result<u64, String> {
|
||||
ensure_document_pool(state, connection_id).await?;
|
||||
let connections = state.connections.read().await;
|
||||
match connections.get(connection_id).ok_or("Not found")? {
|
||||
PoolKind::MongoDb(client) => mongo_driver::insert_documents(client, database, collection, docs_json).await,
|
||||
@@ -194,6 +204,7 @@ pub async fn mongo_update_document_core(
|
||||
id: &str,
|
||||
doc_json: &str,
|
||||
) -> Result<u64, String> {
|
||||
ensure_document_pool(state, connection_id).await?;
|
||||
let connections = state.connections.read().await;
|
||||
match connections.get(connection_id).ok_or("Not found")? {
|
||||
PoolKind::MongoDb(client) => mongo_driver::update_document(client, database, collection, id, doc_json).await,
|
||||
@@ -227,6 +238,7 @@ pub async fn mongo_update_documents_core(
|
||||
update_json: &str,
|
||||
many: bool,
|
||||
) -> Result<u64, String> {
|
||||
ensure_document_pool(state, connection_id).await?;
|
||||
let connections = state.connections.read().await;
|
||||
match connections.get(connection_id).ok_or("Not found")? {
|
||||
PoolKind::MongoDb(client) => {
|
||||
@@ -244,6 +256,7 @@ pub async fn mongo_delete_document_core(
|
||||
collection: &str,
|
||||
id: &str,
|
||||
) -> Result<u64, String> {
|
||||
ensure_document_pool(state, connection_id).await?;
|
||||
let connections = state.connections.read().await;
|
||||
match connections.get(connection_id).ok_or("Not found")? {
|
||||
PoolKind::MongoDb(client) => mongo_driver::delete_document(client, database, collection, id).await,
|
||||
@@ -270,6 +283,7 @@ pub async fn mongo_delete_documents_core(
|
||||
filter_json: &str,
|
||||
many: bool,
|
||||
) -> Result<u64, String> {
|
||||
ensure_document_pool(state, connection_id).await?;
|
||||
let connections = state.connections.read().await;
|
||||
match connections.get(connection_id).ok_or("Not found")? {
|
||||
PoolKind::MongoDb(client) => {
|
||||
|
||||
@@ -2084,6 +2084,7 @@ mod tests {
|
||||
driver_store_dir: Some("/tmp/dbx-drivers".to_string()),
|
||||
plugin_store_dir: Some("/tmp/dbx-plugins".to_string()),
|
||||
agent_store_dir: Some("/tmp/dbx-agents".to_string()),
|
||||
sidebar_table_page_size: DesktopSettings::default().sidebar_table_page_size,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -2098,7 +2099,8 @@ mod tests {
|
||||
saved_sql_sync_dir: None,
|
||||
driver_store_dir: Some("/tmp/dbx-drivers".to_string()),
|
||||
plugin_store_dir: Some("/tmp/dbx-plugins".to_string()),
|
||||
agent_store_dir: Some("/tmp/dbx-agents".to_string())
|
||||
agent_store_dir: Some("/tmp/dbx-agents".to_string()),
|
||||
sidebar_table_page_size: DesktopSettings::default().sidebar_table_page_size,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user