MCPcopy Create free account
hub / github.com/erans/pgsqlite / acquire

Method acquire

src/session/pool.rs:279–318  ·  view source on GitHub ↗
(&self)

Source from the content-addressed store, hash-verified

277 }
278
279 pub async fn acquire(&self) -> Result<PooledConnection> {
280 let permit = self.semaphore.clone().acquire_owned().await.unwrap();
281
282 let pool_conn = {
283 let mut conns = self.connections.lock().unwrap();
284 let mut stats = self.stats.lock().unwrap();
285
286 match conns.pop() {
287 Some(mut pc) => {
288 pc.touch(); // Update last_used timestamp
289 stats.idle_connections -= 1;
290 stats.active_connections += 1;
291 Some(pc)
292 }
293 None => {
294 stats.active_connections += 1;
295 None
296 }
297 }
298 };
299
300 let conn = match pool_conn {
301 Some(pc) => pc.conn,
302 None => {
303 // Create new connection if pool is empty
304 let new_conn = self.create_connection()?;
305 let mut stats = self.stats.lock().unwrap();
306 stats.connections_created += 1;
307 stats.total_connections += 1;
308 new_conn
309 }
310 };
311
312 Ok(PooledConnection {
313 conn: Some(conn),
314 pool: self.connections.clone(),
315 stats: self.stats.clone(),
316 _permit: permit,
317 })
318 }
319}
320
321pub struct PooledConnection {

Callers 7

health_checkMethod · 0.80
test_pool_statsFunction · 0.80
queryMethod · 0.80
query_with_paramsMethod · 0.80

Calls 3

cloneMethod · 0.45
touchMethod · 0.45
create_connectionMethod · 0.45

Tested by 3

test_pool_statsFunction · 0.64