sqlserver_mcp_catalog/services/sql_pool.rs
1// SQL Server - master/msdb/sandbox combined catalog MCP server.
2//
3// A process-wide cache of `bb8` connection pools, one per distinct
4// `tiberius::Config` (in practice: one, since `Config` is loaded once at
5// startup and reused for the process's lifetime) — mirrors
6// `data::store::cached_store_connection`'s process-wide-cache-by-key shape,
7// the existing convention in this codebase for "expensive resource, built
8// once, shared across tool calls" rather than introducing a different
9// pattern for this one case.
10
11use std::collections::HashMap;
12use std::sync::{Mutex, OnceLock};
13
14use bb8::Pool;
15use bb8_tiberius::ConnectionManager;
16
17pub type SqlPool = Pool<ConnectionManager>;
18
19/// Returns the cached pool for `cache_key`, building one via `config` on
20/// first use. `cache_key` is caller-supplied rather than derived from
21/// `config` here, since two `Config`s that are meaningfully different for
22/// pooling purposes (e.g. different resolved AAD tokens) don't necessarily
23/// differ in a way that's cheap to hash/compare.
24pub async fn cached_pool(
25 cache_key: &str,
26 config: tiberius::Config,
27 max_size: u32,
28) -> anyhow::Result<SqlPool> {
29 static POOLS: OnceLock<Mutex<HashMap<String, SqlPool>>> = OnceLock::new();
30 let pools = POOLS.get_or_init(|| Mutex::new(HashMap::new()));
31
32 if let Some(pool) = pools.lock().unwrap().get(cache_key) {
33 return Ok(pool.clone());
34 }
35
36 let manager = ConnectionManager::new(config);
37 let pool = Pool::builder().max_size(max_size).build(manager).await?;
38
39 // `bb8::Pool` is a cheap `Arc`-backed handle, so the pool built above
40 // and the one returned to the caller after this insert are the same
41 // underlying pool either way — a lost race here (two callers both
42 // missing the cache and both building) just means one extra pool gets
43 // built and then discarded once its `Arc` refcount drops, not a
44 // correctness issue.
45 let mut pools = pools.lock().unwrap();
46 Ok(pools.entry(cache_key.to_string()).or_insert(pool).clone())
47}
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52
53 /// A `tiberius::Config` pointed at a reserved documentation-only
54 /// address (RFC 5737 TEST-NET-1) -- never routable, so nothing this
55 /// test does can ever reach a real server. Safe to use because `bb8`'s
56 /// `Pool::builder()` defaults `min_idle` to `None` (see bb8's own
57 /// `Builder::default`/`PoolInner::start_connections`/`wanted`), so
58 /// `build()` doesn't eagerly establish any connection when the
59 /// builder never calls `.min_idle(..)` -- exactly what `cached_pool`
60 /// does -- meaning `Pool::builder().max_size(n).build(manager).await`
61 /// itself never touches the network here.
62 fn unroutable_config() -> tiberius::Config {
63 let mut config = tiberius::Config::new();
64 config.host("192.0.2.1");
65 config.port(1433);
66 config
67 }
68
69 #[tokio::test]
70 async fn cached_pool_serves_a_cache_hit_on_the_same_key_without_erroring() {
71 // First call: cache miss, builds and inserts a new pool. Second
72 // call, same key: cache hit, clones the cached entry instead of
73 // building again -- exercises both branches of `cached_pool`
74 // without a live SQL Server (`build()` doesn't eagerly connect;
75 // see `unroutable_config`'s doc comment).
76 let key = "cached_pool_serves_a_cache_hit_on_the_same_key_without_erroring";
77 cached_pool(key, unroutable_config(), 5).await.unwrap();
78 cached_pool(key, unroutable_config(), 5).await.unwrap();
79 }
80
81 #[tokio::test]
82 async fn cached_pool_builds_a_separate_pool_for_a_distinct_key() {
83 cached_pool(
84 "cached_pool_builds_a_separate_pool_for_a_distinct_key_a",
85 unroutable_config(),
86 5,
87 )
88 .await
89 .unwrap();
90 cached_pool(
91 "cached_pool_builds_a_separate_pool_for_a_distinct_key_b",
92 unroutable_config(),
93 5,
94 )
95 .await
96 .unwrap();
97 }
98}