fix(connection): respect agent connect timeout
This commit is contained in:
1 parent
59fa888149
commit
9c478d1437
8 files changed
+193
-18
No files matched your search
@@ -751,6 +751,18 @@ impl AgentManager {
|
||||
crate::agent_runtime::call_daemon(self, db_type, driver_profile, method, params).await
|
||||
}
|
||||
|
||||
pub async fn call_daemon_with_timeout<T: serde::de::DeserializeOwned + Send + 'static>(
|
||||
&self,
|
||||
db_type: &DatabaseType,
|
||||
driver_profile: Option<&str>,
|
||||
method: &str,
|
||||
params: serde_json::Value,
|
||||
timeout_duration: Option<std::time::Duration>,
|
||||
) -> Result<T, String> {
|
||||
crate::agent_runtime::call_daemon_with_timeout(self, db_type, driver_profile, method, params, timeout_duration)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn call_daemon_method<T: serde::de::DeserializeOwned + Send + 'static>(
|
||||
&self,
|
||||
db_type: &DatabaseType,
|
||||
@@ -761,6 +773,25 @@ impl AgentManager {
|
||||
crate::agent_runtime::call_daemon_method(self, db_type, driver_profile, method, params).await
|
||||
}
|
||||
|
||||
pub async fn call_daemon_method_with_timeout<T: serde::de::DeserializeOwned + Send + 'static>(
|
||||
&self,
|
||||
db_type: &DatabaseType,
|
||||
driver_profile: Option<&str>,
|
||||
method: AgentMethod,
|
||||
params: serde_json::Value,
|
||||
timeout_duration: Option<std::time::Duration>,
|
||||
) -> Result<T, String> {
|
||||
crate::agent_runtime::call_daemon_method_with_timeout(
|
||||
self,
|
||||
db_type,
|
||||
driver_profile,
|
||||
method,
|
||||
params,
|
||||
timeout_duration,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn download_file(url: &str, dest: &Path) -> Result<(), String> {
|
||||
if let Some(parent) = dest.parent() {
|
||||
std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::agent_manager::{AgentManager, DEFAULT_JRE_KEY};
|
||||
use crate::database_capabilities;
|
||||
@@ -70,6 +71,39 @@ pub async fn call_daemon<T: DeserializeOwned + Send + 'static>(
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn call_daemon_with_timeout<T: DeserializeOwned + Send + 'static>(
|
||||
manager: &AgentManager,
|
||||
db_type: &DatabaseType,
|
||||
driver_profile: Option<&str>,
|
||||
method: &str,
|
||||
params: serde_json::Value,
|
||||
timeout_duration: Option<Duration>,
|
||||
) -> Result<T, String> {
|
||||
let keys = runtime_agent_key_candidates(db_type, driver_profile)
|
||||
.ok_or_else(|| format!("{:?} is not an agent-driven database type", db_type))?;
|
||||
let key = first_installed_agent_key(manager, &keys).unwrap_or(keys[0]).to_string();
|
||||
|
||||
let mut daemons = manager.daemons.lock().await;
|
||||
|
||||
if !daemons.contains_key(&key) {
|
||||
let client = spawn_client_for_key(manager, &key).await?;
|
||||
daemons.insert(key.clone(), client);
|
||||
}
|
||||
|
||||
let client = daemons.get_mut(&key).unwrap();
|
||||
match client.call_with_timeout::<T>(method, params.clone(), timeout_duration).await {
|
||||
Ok(result) => Ok(result),
|
||||
Err(err) => {
|
||||
log::warn!("[agent] daemon call failed, respawning: {err}");
|
||||
daemons.remove(&key);
|
||||
let mut new_client = spawn_client_for_key(manager, &key).await?;
|
||||
let result = new_client.call_with_timeout::<T>(method, params, timeout_duration).await?;
|
||||
daemons.insert(key, new_client);
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn call_daemon_method<T: DeserializeOwned + Send + 'static>(
|
||||
manager: &AgentManager,
|
||||
db_type: &DatabaseType,
|
||||
@@ -80,6 +114,17 @@ pub async fn call_daemon_method<T: DeserializeOwned + Send + 'static>(
|
||||
call_daemon(manager, db_type, driver_profile, method.as_str(), params).await
|
||||
}
|
||||
|
||||
pub async fn call_daemon_method_with_timeout<T: DeserializeOwned + Send + 'static>(
|
||||
manager: &AgentManager,
|
||||
db_type: &DatabaseType,
|
||||
driver_profile: Option<&str>,
|
||||
method: AgentMethod,
|
||||
params: serde_json::Value,
|
||||
timeout_duration: Option<Duration>,
|
||||
) -> Result<T, String> {
|
||||
call_daemon_with_timeout(manager, db_type, driver_profile, method.as_str(), params, timeout_duration).await
|
||||
}
|
||||
|
||||
fn runtime_agent_key_candidates(db_type: &DatabaseType, driver_profile: Option<&str>) -> Option<Vec<&'static str>> {
|
||||
let primary = db_type_to_agent_key(db_type, driver_profile)?;
|
||||
if *db_type == DatabaseType::Oracle {
|
||||
|
||||
@@ -28,6 +28,8 @@ use crate::storage::Storage;
|
||||
|
||||
pub const JDBC_PLUGIN_NOT_INSTALLED: &str =
|
||||
"JDBC plugin is not installed. Install the optional JDBC plugin to use this connection.";
|
||||
const DEFAULT_AGENT_CONNECT_TIMEOUT_SECS: u64 = 30;
|
||||
const ACCESS_AGENT_CONNECT_TIMEOUT_SECS: u64 = 30;
|
||||
|
||||
#[cfg(feature = "duckdb-bundled")]
|
||||
mod duckdb_types {
|
||||
@@ -570,8 +572,13 @@ impl AppState {
|
||||
agent_connect_params(&db_config, &host, port, db_config.effective_database().unwrap_or(""));
|
||||
let mut client =
|
||||
self.agent_manager.spawn(&db_config.db_type, db_config.driver_profile.as_deref()).await?;
|
||||
let connect_result =
|
||||
client.call_method::<serde_json::Value>(AgentMethod::Connect, connect_params.clone()).await;
|
||||
let connect_result = client
|
||||
.call_method_with_timeout::<serde_json::Value>(
|
||||
AgentMethod::Connect,
|
||||
connect_params.clone(),
|
||||
Some(agent_connect_timeout(&db_config)),
|
||||
)
|
||||
.await;
|
||||
if let Err(err) = connect_result {
|
||||
let alternate_configs = oracle_alternate_connect_configs(&db_config, &err);
|
||||
if !alternate_configs.is_empty() {
|
||||
@@ -589,7 +596,7 @@ impl AppState {
|
||||
.next()
|
||||
.unwrap_or_else(|| "alternate".to_string());
|
||||
match client
|
||||
.call_method::<serde_json::Value>(
|
||||
.call_method_with_timeout::<serde_json::Value>(
|
||||
AgentMethod::Connect,
|
||||
agent_connect_params(
|
||||
&alternate_config,
|
||||
@@ -597,6 +604,7 @@ impl AppState {
|
||||
port,
|
||||
alternate_config.effective_database().unwrap_or(""),
|
||||
),
|
||||
Some(agent_connect_timeout(&alternate_config)),
|
||||
)
|
||||
.await
|
||||
{
|
||||
@@ -627,7 +635,11 @@ impl AppState {
|
||||
match self.agent_manager.spawn(&db_config.db_type, Some(profile)).await {
|
||||
Ok(mut fallback_client) => {
|
||||
match fallback_client
|
||||
.call_method::<serde_json::Value>(AgentMethod::Connect, connect_params.clone())
|
||||
.call_method_with_timeout::<serde_json::Value>(
|
||||
AgentMethod::Connect,
|
||||
connect_params.clone(),
|
||||
Some(agent_connect_timeout(&db_config)),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {
|
||||
@@ -1170,8 +1182,17 @@ pub fn redacted_connection_url_for_endpoint(config: &ConnectionConfig, host: &st
|
||||
}
|
||||
}
|
||||
|
||||
pub fn agent_connect_timeout(config: &ConnectionConfig) -> std::time::Duration {
|
||||
let min_timeout = if config.db_type == DatabaseType::Access {
|
||||
ACCESS_AGENT_CONNECT_TIMEOUT_SECS
|
||||
} else {
|
||||
DEFAULT_AGENT_CONNECT_TIMEOUT_SECS
|
||||
};
|
||||
std::time::Duration::from_secs(config.effective_connect_timeout_secs().max(min_timeout))
|
||||
}
|
||||
|
||||
fn external_driver_connect_timeout(config: &ConnectionConfig) -> std::time::Duration {
|
||||
std::time::Duration::from_secs(config.effective_connect_timeout_secs().max(30))
|
||||
agent_connect_timeout(config)
|
||||
}
|
||||
|
||||
fn native_postgres_url_config(config: &ConnectionConfig) -> Option<ConnectionConfig> {
|
||||
@@ -1313,7 +1334,7 @@ async fn detect_ob_oracle_mode(config: &ConnectionConfig, pool: &db::mysql::MySq
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{
|
||||
connection_url_for_endpoint, database_connection_config, metadata_connection_config,
|
||||
agent_connect_timeout, connection_url_for_endpoint, database_connection_config, metadata_connection_config,
|
||||
mysql_metadata_fallback_url, redacted_connection_url_for_endpoint, uses_bare_mysql_pool, uses_tcp_probe,
|
||||
validate_h2_database_path, AppState, PoolKind,
|
||||
};
|
||||
@@ -1377,6 +1398,28 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn access_agent_connect_timeout_has_longer_default_floor() {
|
||||
let mut config = mysql_config(None);
|
||||
config.db_type = DatabaseType::Access;
|
||||
config.connect_timeout_secs = 5;
|
||||
assert_eq!(agent_connect_timeout(&config).as_secs(), 30);
|
||||
|
||||
config.connect_timeout_secs = 45;
|
||||
assert_eq!(agent_connect_timeout(&config).as_secs(), 45);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_access_agent_connect_timeout_uses_standard_floor() {
|
||||
let mut config = mysql_config(None);
|
||||
config.db_type = DatabaseType::Oracle;
|
||||
config.connect_timeout_secs = 5;
|
||||
assert_eq!(agent_connect_timeout(&config).as_secs(), 30);
|
||||
|
||||
config.connect_timeout_secs = 45;
|
||||
assert_eq!(agent_connect_timeout(&config).as_secs(), 45);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn agent_connect_params_include_url_params() {
|
||||
let mut config = mysql_config(Some("testdb"));
|
||||
|
||||
Reference in new issue
Block a user