fix(connection): size metadata gate by real session pool capacity

This commit is contained in:
Ghyper9023 authored and GitHub committed 2026-09-03 21:07:18 +08:00
1 parent 5e4f756a63
commit 56f85a2c77
1 file changed
+37 -2
+37 -2
View File
@@ -396,6 +396,29 @@ pub(crate) fn uses_metadata_gate(db_type: DatabaseType) -> bool {
matches!(db_type, DatabaseType::Mysql | DatabaseType::Postgres | DatabaseType::SqlServer) 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( fn metadata_gate_key(
connection_id: &str, connection_id: &str,
database: Option<&str>, database: Option<&str>,
@@ -1369,8 +1392,8 @@ impl AppState {
client_session_id: Option<&str>, client_session_id: Option<&str>,
) -> Result<OwnedSemaphorePermit, String> { ) -> Result<OwnedSemaphorePermit, String> {
let key = metadata_gate_key(connection_id, database, db_type, client_session_id); let key = metadata_gate_key(connection_id, database, db_type, client_session_id);
let max_connections = let has_session = client_session_id.map(str::trim).is_some_and(|session| !session.is_empty());
if client_session_id.map(str::trim).is_some_and(|session| !session.is_empty()) { 1 } else { 10 }; let max_connections = if has_session { metadata_gate_session_allowance(db_type) } else { 10 };
let limit = metadata_concurrency_limit(db_type, max_connections); let limit = metadata_concurrency_limit(db_type, max_connections);
let gate = { let gate = {
let mut gates = self.metadata_gates.lock().await; let mut gates = self.metadata_gates.lock().await;
@@ -7844,6 +7867,18 @@ mod tests {
assert_eq!(super::metadata_concurrency_limit(DatabaseType::SqlServer, 10), 1); 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] #[test]
fn metadata_gate_is_limited_to_supported_pool_drivers() { fn metadata_gate_is_limited_to_supported_pool_drivers() {
assert!(super::uses_metadata_gate(DatabaseType::Mysql)); assert!(super::uses_metadata_gate(DatabaseType::Mysql));