File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -84,6 +84,11 @@ impl<DB: Database> PoolInner<DB> {
8484 self . num_idle . load ( Ordering :: Acquire )
8585 }
8686
87+ pub ( super ) fn num_acquired ( & self ) -> u32 {
88+ self . size ( )
89+ . saturating_sub ( u32:: try_from ( self . num_idle ( ) ) . unwrap_or ( u32:: MAX ) )
90+ }
91+
8792 pub ( super ) fn is_closed ( & self ) -> bool {
8893 self . is_closed . load ( Ordering :: Acquire )
8994 }
Original file line number Diff line number Diff line change @@ -541,6 +541,14 @@ impl<DB: Database> Pool<DB> {
541541 self . 0 . num_idle ( )
542542 }
543543
544+ /// Returns the number of connections currently checked out from the pool.
545+ ///
546+ /// This is an instantaneous snapshot. The value may change immediately as
547+ /// tasks acquire or release connections.
548+ pub fn num_acquired ( & self ) -> u32 {
549+ self . 0 . num_acquired ( )
550+ }
551+
544552 /// Gets a clone of the connection options for this pool
545553 pub fn connect_options ( & self ) -> Arc < <DB :: Connection as Connection >:: Options > {
546554 self . 0
@@ -581,6 +589,7 @@ impl<DB: Database> fmt::Debug for Pool<DB> {
581589 fmt. debug_struct ( "Pool" )
582590 . field ( "size" , & self . 0 . size ( ) )
583591 . field ( "num_idle" , & self . 0 . num_idle ( ) )
592+ . field ( "num_acquired" , & self . 0 . num_acquired ( ) )
584593 . field ( "is_closed" , & self . 0 . is_closed ( ) )
585594 . field ( "options" , & self . 0 . options )
586595 . finish ( )
Original file line number Diff line number Diff line change @@ -71,6 +71,39 @@ async fn pool_should_be_returned_failed_transactions() -> anyhow::Result<()> {
7171 Ok ( ( ) )
7272}
7373
74+ #[ sqlx_macros:: test]
75+ async fn pool_should_report_acquired_connections ( ) -> anyhow:: Result < ( ) > {
76+ sqlx:: any:: install_default_drivers ( ) ;
77+
78+ let pool = AnyPoolOptions :: new ( )
79+ . max_connections ( 2 )
80+ . connect ( & dotenvy:: var ( "DATABASE_URL" ) ?)
81+ . await ?;
82+
83+ assert_eq ! ( pool. size( ) , 1 ) ;
84+ assert_eq ! ( pool. num_idle( ) , 1 ) ;
85+ assert_eq ! ( pool. num_acquired( ) , 0 ) ;
86+
87+ let conn1 = pool. acquire ( ) . await ?;
88+
89+ assert_eq ! ( pool. size( ) , 1 ) ;
90+ assert_eq ! ( pool. num_idle( ) , 0 ) ;
91+ assert_eq ! ( pool. num_acquired( ) , 1 ) ;
92+
93+ let conn2 = pool. acquire ( ) . await ?;
94+
95+ assert_eq ! ( pool. size( ) , 2 ) ;
96+ assert_eq ! ( pool. num_idle( ) , 0 ) ;
97+ assert_eq ! ( pool. num_acquired( ) , 2 ) ;
98+
99+ drop ( conn2) ;
100+ drop ( conn1) ;
101+
102+ pool. close ( ) . await ;
103+
104+ Ok ( ( ) )
105+ }
106+
74107#[ sqlx_macros:: test]
75108async fn test_pool_callbacks ( ) -> anyhow:: Result < ( ) > {
76109 sqlx:: any:: install_default_drivers ( ) ;
You can’t perform that action at this time.
0 commit comments