LCOV - code coverage report
Current view: top level - proxy/src/serverless - conn_pool_lib.rs (source / functions) Coverage Total Hit
Test: 62212f4d57a7ad0f69dc82a04629a0bbd5f7c824.info Lines: 53.8 % 463 249
Test Date: 2025-03-17 10:41:39 Functions: 24.7 % 93 23

            Line data    Source code
       1              : use std::collections::HashMap;
       2              : use std::marker::PhantomData;
       3              : use std::ops::Deref;
       4              : use std::sync::atomic::{self, AtomicUsize};
       5              : use std::sync::{Arc, Weak};
       6              : use std::time::Duration;
       7              : 
       8              : use clashmap::ClashMap;
       9              : use parking_lot::RwLock;
      10              : use postgres_client::ReadyForQueryStatus;
      11              : use rand::Rng;
      12              : use smol_str::ToSmolStr;
      13              : use tracing::{Span, debug, info};
      14              : 
      15              : use super::backend::HttpConnError;
      16              : use super::conn_pool::ClientDataRemote;
      17              : use super::http_conn_pool::ClientDataHttp;
      18              : use super::local_conn_pool::ClientDataLocal;
      19              : use crate::auth::backend::ComputeUserInfo;
      20              : use crate::context::RequestContext;
      21              : use crate::control_plane::messages::{ColdStartInfo, MetricsAuxInfo};
      22              : use crate::metrics::{HttpEndpointPoolsGuard, Metrics};
      23              : use crate::protocol2::ConnectionInfoExtra;
      24              : use crate::types::{DbName, EndpointCacheKey, RoleName};
      25              : use crate::usage_metrics::{Ids, MetricCounter, USAGE_METRICS};
      26              : 
      27              : #[derive(Debug, Clone)]
      28              : pub(crate) struct ConnInfo {
      29              :     pub(crate) user_info: ComputeUserInfo,
      30              :     pub(crate) dbname: DbName,
      31              : }
      32              : 
      33              : impl ConnInfo {
      34              :     // hm, change to hasher to avoid cloning?
      35            3 :     pub(crate) fn db_and_user(&self) -> (DbName, RoleName) {
      36            3 :         (self.dbname.clone(), self.user_info.user.clone())
      37            3 :     }
      38              : 
      39            2 :     pub(crate) fn endpoint_cache_key(&self) -> Option<EndpointCacheKey> {
      40            2 :         // We don't want to cache http connections for ephemeral endpoints.
      41            2 :         if self.user_info.options.is_ephemeral() {
      42            0 :             None
      43              :         } else {
      44            2 :             Some(self.user_info.endpoint_cache_key())
      45              :         }
      46            2 :     }
      47              : }
      48              : 
      49              : #[derive(Clone)]
      50              : #[allow(clippy::large_enum_variant, reason = "TODO")]
      51              : pub(crate) enum ClientDataEnum {
      52              :     Remote(ClientDataRemote),
      53              :     Local(ClientDataLocal),
      54              :     Http(ClientDataHttp),
      55              : }
      56              : 
      57              : #[derive(Clone)]
      58              : pub(crate) struct ClientInnerCommon<C: ClientInnerExt> {
      59              :     pub(crate) inner: C,
      60              :     pub(crate) aux: MetricsAuxInfo,
      61              :     pub(crate) conn_id: uuid::Uuid,
      62              :     pub(crate) data: ClientDataEnum, // custom client data like session, key, jti
      63              : }
      64              : 
      65              : impl<C: ClientInnerExt> Drop for ClientInnerCommon<C> {
      66            7 :     fn drop(&mut self) {
      67            7 :         match &mut self.data {
      68            7 :             ClientDataEnum::Remote(remote_data) => {
      69            7 :                 remote_data.cancel();
      70            7 :             }
      71            0 :             ClientDataEnum::Local(local_data) => {
      72            0 :                 local_data.cancel();
      73            0 :             }
      74            0 :             ClientDataEnum::Http(_http_data) => (),
      75              :         }
      76            7 :     }
      77              : }
      78              : 
      79              : impl<C: ClientInnerExt> ClientInnerCommon<C> {
      80            6 :     pub(crate) fn get_conn_id(&self) -> uuid::Uuid {
      81            6 :         self.conn_id
      82            6 :     }
      83              : 
      84            0 :     pub(crate) fn get_data(&mut self) -> &mut ClientDataEnum {
      85            0 :         &mut self.data
      86            0 :     }
      87              : }
      88              : 
      89              : pub(crate) struct ConnPoolEntry<C: ClientInnerExt> {
      90              :     pub(crate) conn: ClientInnerCommon<C>,
      91              :     pub(crate) _last_access: std::time::Instant,
      92              : }
      93              : 
      94              : // Per-endpoint connection pool, (dbname, username) -> DbUserConnPool
      95              : // Number of open connections is limited by the `max_conns_per_endpoint`.
      96              : pub(crate) struct EndpointConnPool<C: ClientInnerExt> {
      97              :     pools: HashMap<(DbName, RoleName), DbUserConnPool<C>>,
      98              :     total_conns: usize,
      99              :     /// max # connections per endpoint
     100              :     max_conns: usize,
     101              :     _guard: HttpEndpointPoolsGuard<'static>,
     102              :     global_connections_count: Arc<AtomicUsize>,
     103              :     global_pool_size_max_conns: usize,
     104              :     pool_name: String,
     105              : }
     106              : 
     107              : impl<C: ClientInnerExt> EndpointConnPool<C> {
     108            0 :     pub(crate) fn new(
     109            0 :         hmap: HashMap<(DbName, RoleName), DbUserConnPool<C>>,
     110            0 :         tconns: usize,
     111            0 :         max_conns_per_endpoint: usize,
     112            0 :         global_connections_count: Arc<AtomicUsize>,
     113            0 :         max_total_conns: usize,
     114            0 :         pname: String,
     115            0 :     ) -> Self {
     116            0 :         Self {
     117            0 :             pools: hmap,
     118            0 :             total_conns: tconns,
     119            0 :             max_conns: max_conns_per_endpoint,
     120            0 :             _guard: Metrics::get().proxy.http_endpoint_pools.guard(),
     121            0 :             global_connections_count,
     122            0 :             global_pool_size_max_conns: max_total_conns,
     123            0 :             pool_name: pname,
     124            0 :         }
     125            0 :     }
     126              : 
     127            0 :     pub(crate) fn get_conn_entry(
     128            0 :         &mut self,
     129            0 :         db_user: (DbName, RoleName),
     130            0 :     ) -> Option<ConnPoolEntry<C>> {
     131            0 :         let Self {
     132            0 :             pools,
     133            0 :             total_conns,
     134            0 :             global_connections_count,
     135            0 :             ..
     136            0 :         } = self;
     137            0 :         pools.get_mut(&db_user).and_then(|pool_entries| {
     138            0 :             let (entry, removed) = pool_entries.get_conn_entry(total_conns);
     139            0 :             global_connections_count.fetch_sub(removed, atomic::Ordering::Relaxed);
     140            0 :             entry
     141            0 :         })
     142            0 :     }
     143              : 
     144            0 :     pub(crate) fn remove_client(
     145            0 :         &mut self,
     146            0 :         db_user: (DbName, RoleName),
     147            0 :         conn_id: uuid::Uuid,
     148            0 :     ) -> bool {
     149            0 :         let Self {
     150            0 :             pools,
     151            0 :             total_conns,
     152            0 :             global_connections_count,
     153            0 :             ..
     154            0 :         } = self;
     155            0 :         if let Some(pool) = pools.get_mut(&db_user) {
     156            0 :             let old_len = pool.get_conns().len();
     157            0 :             pool.get_conns()
     158            0 :                 .retain(|conn| conn.conn.get_conn_id() != conn_id);
     159            0 :             let new_len = pool.get_conns().len();
     160            0 :             let removed = old_len - new_len;
     161            0 :             if removed > 0 {
     162            0 :                 global_connections_count.fetch_sub(removed, atomic::Ordering::Relaxed);
     163            0 :                 Metrics::get()
     164            0 :                     .proxy
     165            0 :                     .http_pool_opened_connections
     166            0 :                     .get_metric()
     167            0 :                     .dec_by(removed as i64);
     168            0 :             }
     169            0 :             *total_conns -= removed;
     170            0 :             removed > 0
     171              :         } else {
     172            0 :             false
     173              :         }
     174            0 :     }
     175              : 
     176            6 :     pub(crate) fn get_name(&self) -> &str {
     177            6 :         &self.pool_name
     178            6 :     }
     179              : 
     180            0 :     pub(crate) fn get_pool(&self, db_user: (DbName, RoleName)) -> Option<&DbUserConnPool<C>> {
     181            0 :         self.pools.get(&db_user)
     182            0 :     }
     183              : 
     184            0 :     pub(crate) fn get_pool_mut(
     185            0 :         &mut self,
     186            0 :         db_user: (DbName, RoleName),
     187            0 :     ) -> Option<&mut DbUserConnPool<C>> {
     188            0 :         self.pools.get_mut(&db_user)
     189            0 :     }
     190              : 
     191            6 :     pub(crate) fn put(pool: &RwLock<Self>, conn_info: &ConnInfo, client: ClientInnerCommon<C>) {
     192            6 :         let conn_id = client.get_conn_id();
     193            6 :         let (max_conn, conn_count, pool_name) = {
     194            6 :             let pool = pool.read();
     195            6 :             (
     196            6 :                 pool.global_pool_size_max_conns,
     197            6 :                 pool.global_connections_count
     198            6 :                     .load(atomic::Ordering::Relaxed),
     199            6 :                 pool.get_name().to_string(),
     200            6 :             )
     201            6 :         };
     202            6 : 
     203            6 :         if client.inner.is_closed() {
     204            1 :             info!(%conn_id, "{}: throwing away connection '{conn_info}' because connection is closed", pool_name);
     205            1 :             return;
     206            5 :         }
     207            5 : 
     208            5 :         if conn_count >= max_conn {
     209            1 :             info!(%conn_id, "{}: throwing away connection '{conn_info}' because pool is full", pool_name);
     210            1 :             return;
     211            4 :         }
     212            4 : 
     213            4 :         // return connection to the pool
     214            4 :         let mut returned = false;
     215            4 :         let mut per_db_size = 0;
     216            4 :         let total_conns = {
     217            4 :             let mut pool = pool.write();
     218            4 : 
     219            4 :             if pool.total_conns < pool.max_conns {
     220            3 :                 let pool_entries = pool.pools.entry(conn_info.db_and_user()).or_default();
     221            3 :                 pool_entries.get_conns().push(ConnPoolEntry {
     222            3 :                     conn: client,
     223            3 :                     _last_access: std::time::Instant::now(),
     224            3 :                 });
     225            3 : 
     226            3 :                 returned = true;
     227            3 :                 per_db_size = pool_entries.get_conns().len();
     228            3 : 
     229            3 :                 pool.total_conns += 1;
     230            3 :                 pool.global_connections_count
     231            3 :                     .fetch_add(1, atomic::Ordering::Relaxed);
     232            3 :                 Metrics::get()
     233            3 :                     .proxy
     234            3 :                     .http_pool_opened_connections
     235            3 :                     .get_metric()
     236            3 :                     .inc();
     237            3 :             }
     238              : 
     239            4 :             pool.total_conns
     240            4 :         };
     241            4 : 
     242            4 :         // do logging outside of the mutex
     243            4 :         if returned {
     244            3 :             debug!(%conn_id, "{pool_name}: returning connection '{conn_info}' back to the pool, total_conns={total_conns}, for this (db, user)={per_db_size}");
     245              :         } else {
     246            1 :             info!(%conn_id, "{pool_name}: throwing away connection '{conn_info}' because pool is full, total_conns={total_conns}");
     247              :         }
     248            6 :     }
     249              : }
     250              : 
     251              : impl<C: ClientInnerExt> Drop for EndpointConnPool<C> {
     252            2 :     fn drop(&mut self) {
     253            2 :         if self.total_conns > 0 {
     254            2 :             self.global_connections_count
     255            2 :                 .fetch_sub(self.total_conns, atomic::Ordering::Relaxed);
     256            2 :             Metrics::get()
     257            2 :                 .proxy
     258            2 :                 .http_pool_opened_connections
     259            2 :                 .get_metric()
     260            2 :                 .dec_by(self.total_conns as i64);
     261            2 :         }
     262            2 :     }
     263              : }
     264              : 
     265              : pub(crate) struct DbUserConnPool<C: ClientInnerExt> {
     266              :     pub(crate) conns: Vec<ConnPoolEntry<C>>,
     267              :     pub(crate) initialized: Option<bool>, // a bit ugly, exists only for local pools
     268              : }
     269              : 
     270              : impl<C: ClientInnerExt> Default for DbUserConnPool<C> {
     271            2 :     fn default() -> Self {
     272            2 :         Self {
     273            2 :             conns: Vec::new(),
     274            2 :             initialized: None,
     275            2 :         }
     276            2 :     }
     277              : }
     278              : 
     279              : pub(crate) trait DbUserConn<C: ClientInnerExt>: Default {
     280              :     fn set_initialized(&mut self);
     281              :     fn is_initialized(&self) -> bool;
     282              :     fn clear_closed_clients(&mut self, conns: &mut usize) -> usize;
     283              :     fn get_conn_entry(&mut self, conns: &mut usize) -> (Option<ConnPoolEntry<C>>, usize);
     284              :     fn get_conns(&mut self) -> &mut Vec<ConnPoolEntry<C>>;
     285              : }
     286              : 
     287              : impl<C: ClientInnerExt> DbUserConn<C> for DbUserConnPool<C> {
     288            0 :     fn set_initialized(&mut self) {
     289            0 :         self.initialized = Some(true);
     290            0 :     }
     291              : 
     292            0 :     fn is_initialized(&self) -> bool {
     293            0 :         self.initialized.unwrap_or(false)
     294            0 :     }
     295              : 
     296            1 :     fn clear_closed_clients(&mut self, conns: &mut usize) -> usize {
     297            1 :         let old_len = self.conns.len();
     298            1 : 
     299            2 :         self.conns.retain(|conn| !conn.conn.inner.is_closed());
     300            1 : 
     301            1 :         let new_len = self.conns.len();
     302            1 :         let removed = old_len - new_len;
     303            1 :         *conns -= removed;
     304            1 :         removed
     305            1 :     }
     306              : 
     307            0 :     fn get_conn_entry(&mut self, conns: &mut usize) -> (Option<ConnPoolEntry<C>>, usize) {
     308            0 :         let mut removed = self.clear_closed_clients(conns);
     309            0 :         let conn = self.conns.pop();
     310            0 :         if conn.is_some() {
     311            0 :             *conns -= 1;
     312            0 :             removed += 1;
     313            0 :         }
     314              : 
     315            0 :         Metrics::get()
     316            0 :             .proxy
     317            0 :             .http_pool_opened_connections
     318            0 :             .get_metric()
     319            0 :             .dec_by(removed as i64);
     320            0 : 
     321            0 :         (conn, removed)
     322            0 :     }
     323              : 
     324            6 :     fn get_conns(&mut self) -> &mut Vec<ConnPoolEntry<C>> {
     325            6 :         &mut self.conns
     326            6 :     }
     327              : }
     328              : 
     329              : pub(crate) trait EndpointConnPoolExt<C: ClientInnerExt> {
     330              :     fn clear_closed(&mut self) -> usize;
     331              :     fn total_conns(&self) -> usize;
     332              : }
     333              : 
     334              : impl<C: ClientInnerExt> EndpointConnPoolExt<C> for EndpointConnPool<C> {
     335            1 :     fn clear_closed(&mut self) -> usize {
     336            1 :         let mut clients_removed: usize = 0;
     337            1 :         for db_pool in self.pools.values_mut() {
     338            1 :             clients_removed += db_pool.clear_closed_clients(&mut self.total_conns);
     339            1 :         }
     340            1 :         clients_removed
     341            1 :     }
     342              : 
     343            1 :     fn total_conns(&self) -> usize {
     344            1 :         self.total_conns
     345            1 :     }
     346              : }
     347              : 
     348              : pub(crate) struct GlobalConnPool<C, P>
     349              : where
     350              :     C: ClientInnerExt,
     351              :     P: EndpointConnPoolExt<C>,
     352              : {
     353              :     // endpoint -> per-endpoint connection pool
     354              :     //
     355              :     // That should be a fairly conteded map, so return reference to the per-endpoint
     356              :     // pool as early as possible and release the lock.
     357              :     pub(crate) global_pool: ClashMap<EndpointCacheKey, Arc<RwLock<P>>>,
     358              : 
     359              :     /// Number of endpoint-connection pools
     360              :     ///
     361              :     /// [`ClashMap::len`] iterates over all inner pools and acquires a read lock on each.
     362              :     /// That seems like far too much effort, so we're using a relaxed increment counter instead.
     363              :     /// It's only used for diagnostics.
     364              :     pub(crate) global_pool_size: AtomicUsize,
     365              : 
     366              :     /// Total number of connections in the pool
     367              :     pub(crate) global_connections_count: Arc<AtomicUsize>,
     368              : 
     369              :     pub(crate) config: &'static crate::config::HttpConfig,
     370              : 
     371              :     _marker: PhantomData<C>,
     372              : }
     373              : 
     374              : #[derive(Debug, Clone, Copy)]
     375              : pub struct GlobalConnPoolOptions {
     376              :     // Maximum number of connections per one endpoint.
     377              :     // Can mix different (dbname, username) connections.
     378              :     // When running out of free slots for a particular endpoint,
     379              :     // falls back to opening a new connection for each request.
     380              :     pub max_conns_per_endpoint: usize,
     381              : 
     382              :     pub gc_epoch: Duration,
     383              : 
     384              :     pub pool_shards: usize,
     385              : 
     386              :     pub idle_timeout: Duration,
     387              : 
     388              :     pub opt_in: bool,
     389              : 
     390              :     // Total number of connections in the pool.
     391              :     pub max_total_conns: usize,
     392              : }
     393              : 
     394              : impl<C, P> GlobalConnPool<C, P>
     395              : where
     396              :     C: ClientInnerExt,
     397              :     P: EndpointConnPoolExt<C>,
     398              : {
     399            1 :     pub(crate) fn new(config: &'static crate::config::HttpConfig) -> Arc<Self> {
     400            1 :         let shards = config.pool_options.pool_shards;
     401            1 :         Arc::new(Self {
     402            1 :             global_pool: ClashMap::with_shard_amount(shards),
     403            1 :             global_pool_size: AtomicUsize::new(0),
     404            1 :             config,
     405            1 :             global_connections_count: Arc::new(AtomicUsize::new(0)),
     406            1 :             _marker: PhantomData,
     407            1 :         })
     408            1 :     }
     409              : 
     410              :     #[cfg(test)]
     411            9 :     pub(crate) fn get_global_connections_count(&self) -> usize {
     412            9 :         self.global_connections_count
     413            9 :             .load(atomic::Ordering::Relaxed)
     414            9 :     }
     415              : 
     416            0 :     pub(crate) fn get_idle_timeout(&self) -> Duration {
     417            0 :         self.config.pool_options.idle_timeout
     418            0 :     }
     419              : 
     420            0 :     pub(crate) fn shutdown(&self) {
     421            0 :         // drops all strong references to endpoint-pools
     422            0 :         self.global_pool.clear();
     423            0 :     }
     424              : 
     425            0 :     pub(crate) async fn gc_worker(&self, mut rng: impl Rng) {
     426            0 :         let epoch = self.config.pool_options.gc_epoch;
     427            0 :         let mut interval = tokio::time::interval(epoch / (self.global_pool.shards().len()) as u32);
     428              :         loop {
     429            0 :             interval.tick().await;
     430              : 
     431            0 :             let shard = rng.gen_range(0..self.global_pool.shards().len());
     432            0 :             self.gc(shard);
     433              :         }
     434              :     }
     435              : 
     436            2 :     pub(crate) fn gc(&self, shard: usize) {
     437            2 :         debug!(shard, "pool: performing epoch reclamation");
     438              : 
     439              :         // acquire a random shard lock
     440            2 :         let mut shard = self.global_pool.shards()[shard].write();
     441            2 : 
     442            2 :         let timer = Metrics::get()
     443            2 :             .proxy
     444            2 :             .http_pool_reclaimation_lag_seconds
     445            2 :             .start_timer();
     446            2 :         let current_len = shard.len();
     447            2 :         let mut clients_removed = 0;
     448            2 :         shard.retain(|(endpoint, x)| {
     449              :             // if the current endpoint pool is unique (no other strong or weak references)
     450              :             // then it is currently not in use by any connections.
     451            2 :             if let Some(pool) = Arc::get_mut(x) {
     452            1 :                 let endpoints = pool.get_mut();
     453            1 :                 clients_removed = endpoints.clear_closed();
     454            1 : 
     455            1 :                 if endpoints.total_conns() == 0 {
     456            0 :                     info!("pool: discarding pool for endpoint {endpoint}");
     457            0 :                     return false;
     458            1 :                 }
     459            1 :             }
     460              : 
     461            2 :             true
     462            2 :         });
     463            2 : 
     464            2 :         let new_len = shard.len();
     465            2 :         drop(shard);
     466            2 :         timer.observe();
     467            2 : 
     468            2 :         // Do logging outside of the lock.
     469            2 :         if clients_removed > 0 {
     470            1 :             let size = self
     471            1 :                 .global_connections_count
     472            1 :                 .fetch_sub(clients_removed, atomic::Ordering::Relaxed)
     473            1 :                 - clients_removed;
     474            1 :             Metrics::get()
     475            1 :                 .proxy
     476            1 :                 .http_pool_opened_connections
     477            1 :                 .get_metric()
     478            1 :                 .dec_by(clients_removed as i64);
     479            1 :             info!(
     480            0 :                 "pool: performed global pool gc. removed {clients_removed} clients, total number of clients in pool is {size}"
     481              :             );
     482            1 :         }
     483            2 :         let removed = current_len - new_len;
     484            2 : 
     485            2 :         if removed > 0 {
     486            0 :             let global_pool_size = self
     487            0 :                 .global_pool_size
     488            0 :                 .fetch_sub(removed, atomic::Ordering::Relaxed)
     489            0 :                 - removed;
     490            0 :             info!("pool: performed global pool gc. size now {global_pool_size}");
     491            2 :         }
     492            2 :     }
     493              : }
     494              : 
     495              : impl<C: ClientInnerExt> GlobalConnPool<C, EndpointConnPool<C>> {
     496            0 :     pub(crate) fn get(
     497            0 :         self: &Arc<Self>,
     498            0 :         ctx: &RequestContext,
     499            0 :         conn_info: &ConnInfo,
     500            0 :     ) -> Result<Option<Client<C>>, HttpConnError> {
     501            0 :         let mut client: Option<ClientInnerCommon<C>> = None;
     502            0 :         let Some(endpoint) = conn_info.endpoint_cache_key() else {
     503            0 :             return Ok(None);
     504              :         };
     505              : 
     506            0 :         let endpoint_pool = self.get_or_create_endpoint_pool(&endpoint);
     507            0 :         if let Some(entry) = endpoint_pool
     508            0 :             .write()
     509            0 :             .get_conn_entry(conn_info.db_and_user())
     510            0 :         {
     511            0 :             client = Some(entry.conn);
     512            0 :         }
     513            0 :         let endpoint_pool = Arc::downgrade(&endpoint_pool);
     514              : 
     515              :         // ok return cached connection if found and establish a new one otherwise
     516            0 :         if let Some(mut client) = client {
     517            0 :             if client.inner.is_closed() {
     518            0 :                 info!("pool: cached connection '{conn_info}' is closed, opening a new one");
     519            0 :                 return Ok(None);
     520            0 :             }
     521            0 :             tracing::Span::current()
     522            0 :                 .record("conn_id", tracing::field::display(client.get_conn_id()));
     523            0 :             tracing::Span::current().record(
     524            0 :                 "pid",
     525            0 :                 tracing::field::display(client.inner.get_process_id()),
     526            0 :             );
     527            0 :             debug!(
     528            0 :                 cold_start_info = ColdStartInfo::HttpPoolHit.as_str(),
     529            0 :                 "pool: reusing connection '{conn_info}'"
     530              :             );
     531              : 
     532            0 :             match client.get_data() {
     533            0 :                 ClientDataEnum::Local(data) => {
     534            0 :                     data.session().send(ctx.session_id())?;
     535              :                 }
     536              : 
     537            0 :                 ClientDataEnum::Remote(data) => {
     538            0 :                     data.session().send(ctx.session_id())?;
     539              :                 }
     540            0 :                 ClientDataEnum::Http(_) => (),
     541              :             }
     542              : 
     543            0 :             ctx.set_cold_start_info(ColdStartInfo::HttpPoolHit);
     544            0 :             ctx.success();
     545            0 :             return Ok(Some(Client::new(client, conn_info.clone(), endpoint_pool)));
     546            0 :         }
     547            0 :         Ok(None)
     548            0 :     }
     549              : 
     550            2 :     pub(crate) fn get_or_create_endpoint_pool(
     551            2 :         self: &Arc<Self>,
     552            2 :         endpoint: &EndpointCacheKey,
     553            2 :     ) -> Arc<RwLock<EndpointConnPool<C>>> {
     554              :         // fast path
     555            2 :         if let Some(pool) = self.global_pool.get(endpoint) {
     556            0 :             return pool.clone();
     557            2 :         }
     558            2 : 
     559            2 :         // slow path
     560            2 :         let new_pool = Arc::new(RwLock::new(EndpointConnPool {
     561            2 :             pools: HashMap::new(),
     562            2 :             total_conns: 0,
     563            2 :             max_conns: self.config.pool_options.max_conns_per_endpoint,
     564            2 :             _guard: Metrics::get().proxy.http_endpoint_pools.guard(),
     565            2 :             global_connections_count: self.global_connections_count.clone(),
     566            2 :             global_pool_size_max_conns: self.config.pool_options.max_total_conns,
     567            2 :             pool_name: String::from("remote"),
     568            2 :         }));
     569            2 : 
     570            2 :         // find or create a pool for this endpoint
     571            2 :         let mut created = false;
     572            2 :         let pool = self
     573            2 :             .global_pool
     574            2 :             .entry(endpoint.clone())
     575            2 :             .or_insert_with(|| {
     576            2 :                 created = true;
     577            2 :                 new_pool
     578            2 :             })
     579            2 :             .clone();
     580            2 : 
     581            2 :         // log new global pool size
     582            2 :         if created {
     583            2 :             let global_pool_size = self
     584            2 :                 .global_pool_size
     585            2 :                 .fetch_add(1, atomic::Ordering::Relaxed)
     586            2 :                 + 1;
     587            2 :             info!(
     588            0 :                 "pool: created new pool for '{endpoint}', global pool size now {global_pool_size}"
     589              :             );
     590            0 :         }
     591              : 
     592            2 :         pool
     593            2 :     }
     594              : }
     595              : pub(crate) struct Client<C: ClientInnerExt> {
     596              :     span: Span,
     597              :     inner: Option<ClientInnerCommon<C>>,
     598              :     conn_info: ConnInfo,
     599              :     pool: Weak<RwLock<EndpointConnPool<C>>>,
     600              : }
     601              : 
     602              : pub(crate) struct Discard<'a, C: ClientInnerExt> {
     603              :     conn_info: &'a ConnInfo,
     604              :     pool: &'a mut Weak<RwLock<EndpointConnPool<C>>>,
     605              : }
     606              : 
     607              : impl<C: ClientInnerExt> Client<C> {
     608            7 :     pub(crate) fn new(
     609            7 :         inner: ClientInnerCommon<C>,
     610            7 :         conn_info: ConnInfo,
     611            7 :         pool: Weak<RwLock<EndpointConnPool<C>>>,
     612            7 :     ) -> Self {
     613            7 :         Self {
     614            7 :             inner: Some(inner),
     615            7 :             span: Span::current(),
     616            7 :             conn_info,
     617            7 :             pool,
     618            7 :         }
     619            7 :     }
     620              : 
     621            0 :     pub(crate) fn client_inner(&mut self) -> (&mut ClientInnerCommon<C>, Discard<'_, C>) {
     622            0 :         let Self {
     623            0 :             inner,
     624            0 :             pool,
     625            0 :             conn_info,
     626            0 :             span: _,
     627            0 :         } = self;
     628            0 :         let inner_m = inner.as_mut().expect("client inner should not be removed");
     629            0 :         (inner_m, Discard { conn_info, pool })
     630            0 :     }
     631              : 
     632            1 :     pub(crate) fn inner(&mut self) -> (&mut C, Discard<'_, C>) {
     633            1 :         let Self {
     634            1 :             inner,
     635            1 :             pool,
     636            1 :             conn_info,
     637            1 :             span: _,
     638            1 :         } = self;
     639            1 :         let inner = inner.as_mut().expect("client inner should not be removed");
     640            1 :         (&mut inner.inner, Discard { conn_info, pool })
     641            1 :     }
     642              : 
     643            0 :     pub(crate) fn metrics(&self, ctx: &RequestContext) -> Arc<MetricCounter> {
     644            0 :         let aux = &self
     645            0 :             .inner
     646            0 :             .as_ref()
     647            0 :             .expect("client inner should not be removed")
     648            0 :             .aux;
     649              : 
     650            0 :         let private_link_id = match ctx.extra() {
     651            0 :             None => None,
     652            0 :             Some(ConnectionInfoExtra::Aws { vpce_id }) => Some(vpce_id.clone()),
     653            0 :             Some(ConnectionInfoExtra::Azure { link_id }) => Some(link_id.to_smolstr()),
     654              :         };
     655              : 
     656            0 :         USAGE_METRICS.register(Ids {
     657            0 :             endpoint_id: aux.endpoint_id,
     658            0 :             branch_id: aux.branch_id,
     659            0 :             private_link_id,
     660            0 :         })
     661            0 :     }
     662              : }
     663              : 
     664              : impl<C: ClientInnerExt> Drop for Client<C> {
     665            7 :     fn drop(&mut self) {
     666            7 :         let conn_info = self.conn_info.clone();
     667            7 :         let client = self
     668            7 :             .inner
     669            7 :             .take()
     670            7 :             .expect("client inner should not be removed");
     671            7 :         if let Some(conn_pool) = std::mem::take(&mut self.pool).upgrade() {
     672            6 :             let _current_span = self.span.enter();
     673            6 :             // return connection to the pool
     674            6 :             EndpointConnPool::put(&conn_pool, &conn_info, client);
     675            6 :         }
     676            7 :     }
     677              : }
     678              : 
     679              : impl<C: ClientInnerExt> Deref for Client<C> {
     680              :     type Target = C;
     681              : 
     682            0 :     fn deref(&self) -> &Self::Target {
     683            0 :         &self
     684            0 :             .inner
     685            0 :             .as_ref()
     686            0 :             .expect("client inner should not be removed")
     687            0 :             .inner
     688            0 :     }
     689              : }
     690              : 
     691              : pub(crate) trait ClientInnerExt: Sync + Send + 'static {
     692              :     fn is_closed(&self) -> bool;
     693              :     fn get_process_id(&self) -> i32;
     694              : }
     695              : 
     696              : impl ClientInnerExt for postgres_client::Client {
     697            0 :     fn is_closed(&self) -> bool {
     698            0 :         self.is_closed()
     699            0 :     }
     700              : 
     701            0 :     fn get_process_id(&self) -> i32 {
     702            0 :         self.get_process_id()
     703            0 :     }
     704              : }
     705              : 
     706              : impl<C: ClientInnerExt> Discard<'_, C> {
     707            0 :     pub(crate) fn check_idle(&mut self, status: ReadyForQueryStatus) {
     708            0 :         let conn_info = &self.conn_info;
     709            0 :         if status != ReadyForQueryStatus::Idle && std::mem::take(self.pool).strong_count() > 0 {
     710            0 :             info!("pool: throwing away connection '{conn_info}' because connection is not idle");
     711            0 :         }
     712            0 :     }
     713            1 :     pub(crate) fn discard(&mut self) {
     714            1 :         let conn_info = &self.conn_info;
     715            1 :         if std::mem::take(self.pool).strong_count() > 0 {
     716            1 :             info!(
     717            0 :                 "pool: throwing away connection '{conn_info}' because connection is potentially in a broken state"
     718              :             );
     719            0 :         }
     720            1 :     }
     721              : }
        

Generated by: LCOV version 2.1-beta