@@ -4,6 +4,7 @@ use crate::error::Error;
4
4
use crate :: pool:: inner:: PoolInner ;
5
5
use crate :: pool:: Pool ;
6
6
use futures_core:: future:: BoxFuture ;
7
+ use log:: LevelFilter ;
7
8
use std:: fmt:: { self , Debug , Formatter } ;
8
9
use std:: sync:: Arc ;
9
10
use std:: time:: { Duration , Instant } ;
@@ -74,6 +75,9 @@ pub struct PoolOptions<DB: Database> {
74
75
> ,
75
76
> ,
76
77
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 ,
77
81
pub ( crate ) acquire_timeout : Duration ,
78
82
pub ( crate ) min_connections : u32 ,
79
83
pub ( crate ) max_lifetime : Option < Duration > ,
@@ -94,6 +98,9 @@ impl<DB: Database> Clone for PoolOptions<DB> {
94
98
before_acquire : self . before_acquire . clone ( ) ,
95
99
after_release : self . after_release . clone ( ) ,
96
100
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 ,
97
104
acquire_timeout : self . acquire_timeout ,
98
105
min_connections : self . min_connections ,
99
106
max_lifetime : self . max_lifetime ,
@@ -143,6 +150,13 @@ impl<DB: Database> PoolOptions<DB> {
143
150
// A production application will want to set a higher limit than this.
144
151
max_connections : 10 ,
145
152
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 ) ,
146
160
acquire_timeout : Duration :: from_secs ( 30 ) ,
147
161
idle_timeout : Some ( Duration :: from_secs ( 10 * 60 ) ) ,
148
162
max_lifetime : Some ( Duration :: from_secs ( 30 * 60 ) ) ,
@@ -198,6 +212,39 @@ impl<DB: Database> PoolOptions<DB> {
198
212
self . min_connections
199
213
}
200
214
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
+
201
248
/// Set the maximum amount of time to spend waiting for a connection in [`Pool::acquire()`].
202
249
///
203
250
/// Caps the total amount of time `Pool::acquire()` can spend waiting across multiple phases:
@@ -269,7 +316,7 @@ impl<DB: Database> PoolOptions<DB> {
269
316
self
270
317
}
271
318
272
- /// Get's whether `test_before_acquire` is currently set.
319
+ /// Get whether `test_before_acquire` is currently set.
273
320
pub fn get_test_before_acquire ( & self ) -> bool {
274
321
self . test_before_acquire
275
322
}
0 commit comments