From 56f85a2c776e9a6134becd9c32abd5d6eec63550 Mon Sep 17 00:00:00 2001 From: Ghyper9023 <125760530+cherish-ltt@users.noreply.github.com> Date: Thu, 3 Sep 2026 21:07:18 +0800 Subject: [PATCH] fix(connection): size metadata gate by real session pool capacity --- crates/dbx-core/src/connection.rs | 39 +++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/crates/dbx-core/src/connection.rs b/crates/dbx-core/src/connection.rs index ff45092f67..1368a8265c 100644 --- a/crates/dbx-core/src/connection.rs +++ b/crates/dbx-core/src/connection.rs @@ -396,6 +396,29 @@ pub(crate) fn uses_metadata_gate(db_type: DatabaseType) -> bool { matches!(db_type, DatabaseType::Mysql | DatabaseType::Postgres | DatabaseType::SqlServer) } +/// Session-scoped metadata gate allowance for a database type. +/// +/// A non-empty `client_session_id` normally means the caller opened a +/// dedicated single-connection session pool (for example MySQL tab sessions), +/// so the gate allows only one in-flight metadata operation. PostgreSQL is the +/// exception: `get_or_create_pool_for_session` builds a fresh full-size +/// (10-connection) pool for each export session, so an allowance of 1 would +/// serialize the database-export metadata prefetch (4 concurrent DDL/column +/// lookups) and make three of them time out after METADATA_POOL_ACQUIRE_TIMEOUT +/// with "DBX metadata pool is busy". Returning the pool's real capacity here +/// flows into metadata_concurrency_limit, which caps the effective gate at +/// METADATA_POOL_DEFAULT_LIMIT (6) -- still enough for the 4-wide prefetch and +/// still isolated from the UI/base pool. +fn metadata_gate_session_allowance(db_type: DatabaseType) -> usize { + if db_type == DatabaseType::Postgres { + // Matches db::postgres::connect's Pool::builder().max_size(10). + 10 + } else { + // MySQL and other session-scoped pools are single-connection. + 1 + } +} + fn metadata_gate_key( connection_id: &str, database: Option<&str>, @@ -1369,8 +1392,8 @@ impl AppState { client_session_id: Option<&str>, ) -> Result { let key = metadata_gate_key(connection_id, database, db_type, client_session_id); - let max_connections = - if client_session_id.map(str::trim).is_some_and(|session| !session.is_empty()) { 1 } else { 10 }; + let has_session = client_session_id.map(str::trim).is_some_and(|session| !session.is_empty()); + let max_connections = if has_session { metadata_gate_session_allowance(db_type) } else { 10 }; let limit = metadata_concurrency_limit(db_type, max_connections); let gate = { let mut gates = self.metadata_gates.lock().await; @@ -7844,6 +7867,18 @@ mod tests { assert_eq!(super::metadata_concurrency_limit(DatabaseType::SqlServer, 10), 1); } + #[test] + fn session_metadata_gate_allowance_matches_session_pool_capacity() { + // PostgreSQL session pools are full-size (10 connections, see + // db/postgres.rs Pool::builder().max_size(10)), so the gate must not + // serialize the database-export prefetch down to one permit. + assert_eq!(super::metadata_gate_session_allowance(DatabaseType::Postgres), 10); + // MySQL and other session-scoped pools are single-connection. + assert_eq!(super::metadata_gate_session_allowance(DatabaseType::Mysql), 1); + // SQL Server gate is intentionally shared/serialized regardless. + assert_eq!(super::metadata_gate_session_allowance(DatabaseType::SqlServer), 1); + } + #[test] fn metadata_gate_is_limited_to_supported_pool_drivers() { assert!(super::uses_metadata_gate(DatabaseType::Mysql));