feat(connection): add connection attempt cancellation
This commit is contained in:
16 files changed
+681
-51
No files matched your search
@@ -161,7 +161,7 @@ pub struct AppState {
|
||||
pub connections: Arc<RwLock<HashMap<String, PoolKind>>>,
|
||||
keepalive_tasks: Arc<RwLock<HashMap<String, JoinHandle<()>>>>,
|
||||
pool_activity: Arc<RwLock<HashMap<String, PoolActivity>>>,
|
||||
connection_attempts: RwLock<HashMap<String, u64>>,
|
||||
connection_attempts: RwLock<HashMap<String, ConnectionAttemptState>>,
|
||||
pub configs: RwLock<HashMap<String, ConnectionConfig>>,
|
||||
pub running_queries: RunningQueries,
|
||||
pub tunnels: TunnelManager,
|
||||
@@ -185,6 +185,12 @@ struct PoolActivity {
|
||||
last_used_at: Instant,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct ConnectionAttemptState {
|
||||
server_attempt: u64,
|
||||
client_attempt: Option<u64>,
|
||||
}
|
||||
|
||||
impl PoolActivity {
|
||||
fn now() -> Self {
|
||||
Self { last_used_at: Instant::now() }
|
||||
@@ -553,9 +559,17 @@ impl AppState {
|
||||
}
|
||||
|
||||
pub async fn begin_connection_attempt(&self, connection_id: &str) -> u64 {
|
||||
self.begin_connection_attempt_with_client_attempt(connection_id, None).await
|
||||
}
|
||||
|
||||
pub async fn begin_connection_attempt_with_client_attempt(
|
||||
&self,
|
||||
connection_id: &str,
|
||||
client_attempt: Option<u64>,
|
||||
) -> u64 {
|
||||
let mut attempts = self.connection_attempts.write().await;
|
||||
let next = attempts.get(connection_id).copied().unwrap_or(0).wrapping_add(1);
|
||||
attempts.insert(connection_id.to_string(), next);
|
||||
let next = attempts.get(connection_id).map(|state| state.server_attempt).unwrap_or(0).wrapping_add(1);
|
||||
attempts.insert(connection_id.to_string(), ConnectionAttemptState { server_attempt: next, client_attempt });
|
||||
next
|
||||
}
|
||||
|
||||
@@ -563,11 +577,34 @@ impl AppState {
|
||||
self.begin_connection_attempt(connection_id).await;
|
||||
}
|
||||
|
||||
async fn connection_attempt_is_current(&self, connection_id: &str, attempt: u64) -> bool {
|
||||
self.connection_attempts.read().await.get(connection_id).copied() == Some(attempt)
|
||||
pub async fn supersede_connection_attempt_if_client_attempt(
|
||||
&self,
|
||||
connection_id: &str,
|
||||
client_attempt: u64,
|
||||
) -> bool {
|
||||
let mut attempts = self.connection_attempts.write().await;
|
||||
let Some(current) = attempts.get(connection_id).copied() else {
|
||||
return false;
|
||||
};
|
||||
if current.client_attempt != Some(client_attempt) {
|
||||
return false;
|
||||
}
|
||||
attempts.insert(
|
||||
connection_id.to_string(),
|
||||
ConnectionAttemptState { server_attempt: current.server_attempt.wrapping_add(1), client_attempt: None },
|
||||
);
|
||||
true
|
||||
}
|
||||
|
||||
async fn ensure_current_connection_attempt(&self, connection_id: &str, attempt: Option<u64>) -> Result<(), String> {
|
||||
async fn connection_attempt_is_current(&self, connection_id: &str, attempt: u64) -> bool {
|
||||
self.connection_attempts.read().await.get(connection_id).map(|state| state.server_attempt) == Some(attempt)
|
||||
}
|
||||
|
||||
pub async fn ensure_current_connection_attempt(
|
||||
&self,
|
||||
connection_id: &str,
|
||||
attempt: Option<u64>,
|
||||
) -> Result<(), String> {
|
||||
let Some(attempt) = attempt else {
|
||||
return Ok(());
|
||||
};
|
||||
@@ -594,6 +631,20 @@ impl AppState {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn discard_stale_connection_attempt_pool(
|
||||
&self,
|
||||
connection_id: &str,
|
||||
pool_key: String,
|
||||
pool: PoolKind,
|
||||
config: &ConnectionConfig,
|
||||
) {
|
||||
if matches!(pool, PoolKind::MessageQueue) {
|
||||
self.mq_registry.drop_connection(connection_id).await;
|
||||
}
|
||||
self.reset_connection_transport_for_config(connection_id, config).await;
|
||||
close_pool_kind_with_timeout(pool_key, pool).await;
|
||||
}
|
||||
|
||||
async fn start_keepalive_task(&self, pool_key: &str, pool: &PoolKind, config: &ConnectionConfig) {
|
||||
let interval_secs = config.keepalive_interval_secs;
|
||||
let idle_timeout_secs = config.idle_timeout_secs;
|
||||
@@ -782,8 +833,17 @@ impl AppState {
|
||||
let db_config = database_connection_config(&config, database);
|
||||
|
||||
validate_h2_file_connection(&db_config)?;
|
||||
self.ensure_current_connection_attempt(connection_id, connection_attempt).await?;
|
||||
let (host, port) = self.connection_host_port(connection_id, &db_config).await?;
|
||||
if let Err(err) = self.ensure_current_connection_attempt(connection_id, connection_attempt).await {
|
||||
self.reset_connection_transport_for_config(connection_id, &db_config).await;
|
||||
return Err(err);
|
||||
}
|
||||
probe_connection_endpoint(&db_config, &host, port).await?;
|
||||
if let Err(err) = self.ensure_current_connection_attempt(connection_id, connection_attempt).await {
|
||||
self.reset_connection_transport_for_config(connection_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);
|
||||
@@ -935,9 +995,21 @@ impl AppState {
|
||||
Ok(()) => {
|
||||
// Re-check: another task may have created the pool while we were connecting.
|
||||
if self.connections.read().await.contains_key(&pool_key) {
|
||||
close_pool_kind_with_timeout(pool_key.clone(), PoolKind::MongoDb(client)).await;
|
||||
return Ok(pool_key);
|
||||
}
|
||||
self.ensure_current_connection_attempt(connection_id, connection_attempt).await?;
|
||||
if let Err(err) =
|
||||
self.ensure_current_connection_attempt(connection_id, connection_attempt).await
|
||||
{
|
||||
self.discard_stale_connection_attempt_pool(
|
||||
connection_id,
|
||||
pool_key.clone(),
|
||||
PoolKind::MongoDb(client),
|
||||
&db_config,
|
||||
)
|
||||
.await;
|
||||
return Err(err);
|
||||
}
|
||||
self.insert_connection_pool(pool_key.clone(), PoolKind::MongoDb(client), &db_config)
|
||||
.await;
|
||||
return Ok(pool_key);
|
||||
@@ -1133,6 +1205,11 @@ impl AppState {
|
||||
self.mq_registry.drop_connection(connection_id).await;
|
||||
return Err(err);
|
||||
}
|
||||
if let Err(err) = self.ensure_current_connection_attempt(connection_id, connection_attempt).await {
|
||||
self.mq_registry.drop_connection(connection_id).await;
|
||||
self.reset_connection_transport_for_config(connection_id, &db_config).await;
|
||||
return Err(err);
|
||||
}
|
||||
PoolKind::MessageQueue
|
||||
}
|
||||
#[cfg(not(feature = "mq-admin"))]
|
||||
@@ -1145,7 +1222,7 @@ impl AppState {
|
||||
};
|
||||
|
||||
if let Err(err) = self.ensure_current_connection_attempt(connection_id, connection_attempt).await {
|
||||
close_pool_kind_with_timeout(pool_key.clone(), pool).await;
|
||||
self.discard_stale_connection_attempt_pool(connection_id, pool_key.clone(), pool, &db_config).await;
|
||||
return Err(err);
|
||||
}
|
||||
self.insert_connection_pool(pool_key.clone(), pool, &db_config).await;
|
||||
|
||||
Reference in new issue
Block a user