@@ -4,6 +4,7 @@ use crate::error::Error;
44use crate :: pool:: inner:: PoolInner ;
55use crate :: pool:: Pool ;
66use futures_core:: future:: BoxFuture ;
7+ use log:: LevelFilter ;
78use std:: fmt:: { self , Debug , Formatter } ;
89use std:: sync:: Arc ;
910use std:: time:: { Duration , Instant } ;
@@ -74,6 +75,9 @@ pub struct PoolOptions<DB: Database> {
7475 > ,
7576 > ,
7677 pub ( crate ) max_connections : u32 ,
78+ pub ( crate ) acquire_time_level : LevelFilter ,
79+ pub ( crate ) acquire_slow_level : LevelFilter ,
80+ pub ( crate ) acquire_slow_threshold : Duration ,
7781 pub ( crate ) acquire_timeout : Duration ,
7882 pub ( crate ) min_connections : u32 ,
7983 pub ( crate ) max_lifetime : Option < Duration > ,
@@ -94,6 +98,9 @@ impl<DB: Database> Clone for PoolOptions<DB> {
9498 before_acquire : self . before_acquire . clone ( ) ,
9599 after_release : self . after_release . clone ( ) ,
96100 max_connections : self . max_connections ,
101+ acquire_time_level : self . acquire_time_level ,
102+ acquire_slow_threshold : self . acquire_slow_threshold ,
103+ acquire_slow_level : self . acquire_slow_level ,
97104 acquire_timeout : self . acquire_timeout ,
98105 min_connections : self . min_connections ,
99106 max_lifetime : self . max_lifetime ,
@@ -143,6 +150,13 @@ impl<DB: Database> PoolOptions<DB> {
143150 // A production application will want to set a higher limit than this.
144151 max_connections : 10 ,
145152 min_connections : 0 ,
153+ // Logging all acquires is opt-in
154+ acquire_time_level : LevelFilter :: Off ,
155+ // Default to warning, because an acquire timeout will be an error
156+ acquire_slow_level : LevelFilter :: Warn ,
157+ // Fast enough to catch problems (e.g. a full pool); slow enough
158+ // to not flag typical time to add a new connection to a pool.
159+ acquire_slow_threshold : Duration :: from_secs ( 2 ) ,
146160 acquire_timeout : Duration :: from_secs ( 30 ) ,
147161 idle_timeout : Some ( Duration :: from_secs ( 10 * 60 ) ) ,
148162 max_lifetime : Some ( Duration :: from_secs ( 30 * 60 ) ) ,
@@ -198,6 +212,39 @@ impl<DB: Database> PoolOptions<DB> {
198212 self . min_connections
199213 }
200214
215+ /// Enable logging of time taken to acquire a connection from the connection pool via
216+ /// [`Pool::acquire()`].
217+ ///
218+ /// If slow acquire logging is also enabled, this level is used for acquires that are not
219+ /// considered slow.
220+ pub fn acquire_time_level ( mut self , level : LevelFilter ) -> Self {
221+ self . acquire_time_level = level;
222+ self
223+ }
224+
225+ /// Log excessive time taken to acquire a connection at a different log level than time taken
226+ /// for faster connection acquires via [`Pool::acquire()`].
227+ pub fn acquire_slow_level ( mut self , level : LevelFilter ) -> Self {
228+ self . acquire_slow_level = level;
229+ self
230+ }
231+
232+ /// Set a threshold for reporting excessive time taken to acquire a connection from
233+ /// the connection pool via [`Pool::acquire()`]. When the threshold is exceeded, a warning is logged.
234+ ///
235+ /// Defaults to a value that should not typically be exceeded by the pool enlarging
236+ /// itself with an additional new connection.
237+ pub fn acquire_slow_threshold ( mut self , threshold : Duration ) -> Self {
238+ self . acquire_slow_threshold = threshold;
239+ self
240+ }
241+
242+ /// Get the threshold for reporting excessive time taken to acquire a connection via
243+ /// [`Pool::acquire()`].
244+ pub fn get_acquire_slow_threshold ( & self ) -> Duration {
245+ self . acquire_slow_threshold
246+ }
247+
201248 /// Set the maximum amount of time to spend waiting for a connection in [`Pool::acquire()`].
202249 ///
203250 /// Caps the total amount of time `Pool::acquire()` can spend waiting across multiple phases:
@@ -269,7 +316,7 @@ impl<DB: Database> PoolOptions<DB> {
269316 self
270317 }
271318
272- /// Get's whether `test_before_acquire` is currently set.
319+ /// Get whether `test_before_acquire` is currently set.
273320 pub fn get_test_before_acquire ( & self ) -> bool {
274321 self . test_before_acquire
275322 }
0 commit comments