3
3
#[ cfg( feature = "runtime" ) ]
4
4
use crate :: connect:: connect;
5
5
use crate :: connect_raw:: connect_raw;
6
+ use crate :: keepalive:: KeepaliveConfig ;
6
7
#[ cfg( feature = "runtime" ) ]
7
8
use crate :: tls:: MakeTlsConnect ;
8
9
use crate :: tls:: TlsConnect ;
@@ -99,6 +100,10 @@ pub enum Host {
99
100
/// This option is ignored when connecting with Unix sockets. Defaults to on.
100
101
/// * `keepalives_idle` - The number of seconds of inactivity after which a keepalive message is sent to the server.
101
102
/// This option is ignored when connecting with Unix sockets. Defaults to 2 hours.
103
+ /// * `keepalives_interval` - The time interval between TCP keepalive probes.
104
+ /// This option is ignored when connecting with Unix sockets. Available on neither Redox nor Solaris.
105
+ /// * `keepalives_retries` - The maximum number of TCP keepalive probes that will be sent before dropping a connection.
106
+ /// This option is ignored when connecting with Unix sockets. Available on neither Redox, Solaris nor Windows.
102
107
/// * `target_session_attrs` - Specifies requirements of the session. If set to `read-write`, the client will check that
103
108
/// the `transaction_read_write` session parameter is set to `on`. This can be used to connect to the primary server
104
109
/// in a database cluster as opposed to the secondary read-only mirrors. Defaults to `all`.
@@ -156,7 +161,7 @@ pub struct Config {
156
161
pub ( crate ) port : Vec < u16 > ,
157
162
pub ( crate ) connect_timeout : Option < Duration > ,
158
163
pub ( crate ) keepalives : bool ,
159
- pub ( crate ) keepalives_idle : Duration ,
164
+ pub ( crate ) keepalive_config : KeepaliveConfig ,
160
165
pub ( crate ) target_session_attrs : TargetSessionAttrs ,
161
166
pub ( crate ) channel_binding : ChannelBinding ,
162
167
}
@@ -170,6 +175,13 @@ impl Default for Config {
170
175
impl Config {
171
176
/// Creates a new configuration.
172
177
pub fn new ( ) -> Config {
178
+ let keepalive_config = KeepaliveConfig {
179
+ idle : Duration :: from_secs ( 2 * 60 * 60 ) ,
180
+ #[ cfg( not( any( target_os = "redox" , target_os = "solaris" ) ) ) ]
181
+ interval : None ,
182
+ #[ cfg( not( any( target_os = "redox" , target_os = "solaris" , target_os = "windows" ) ) ) ]
183
+ retries : None ,
184
+ } ;
173
185
Config {
174
186
user : None ,
175
187
password : None ,
@@ -181,7 +193,7 @@ impl Config {
181
193
port : vec ! [ ] ,
182
194
connect_timeout : None ,
183
195
keepalives : true ,
184
- keepalives_idle : Duration :: from_secs ( 2 * 60 * 60 ) ,
196
+ keepalive_config ,
185
197
target_session_attrs : TargetSessionAttrs :: Any ,
186
198
channel_binding : ChannelBinding :: Prefer ,
187
199
}
@@ -347,14 +359,53 @@ impl Config {
347
359
///
348
360
/// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled. Defaults to 2 hours.
349
361
pub fn keepalives_idle ( & mut self , keepalives_idle : Duration ) -> & mut Config {
350
- self . keepalives_idle = keepalives_idle;
362
+ self . keepalive_config . idle = keepalives_idle;
351
363
self
352
364
}
353
365
354
366
/// Gets the configured amount of idle time before a keepalive packet will
355
367
/// be sent on the connection.
356
368
pub fn get_keepalives_idle ( & self ) -> Duration {
357
- self . keepalives_idle
369
+ self . keepalive_config . idle
370
+ }
371
+
372
+ /// Sets the time interval between TCP keepalive probes.
373
+ /// On Windows, this sets the value of the tcp_keepalive struct’s keepaliveinterval field.
374
+ ///
375
+ /// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled.
376
+ ///
377
+ /// Available on neither Redox nor Solaris.
378
+ #[ cfg( not( any( target_os = "redox" , target_os = "solaris" ) ) ) ]
379
+ pub fn keepalives_interval ( & mut self , keepalives_interval : Duration ) -> & mut Config {
380
+ self . keepalive_config . interval = Some ( keepalives_interval) ;
381
+ self
382
+ }
383
+
384
+ /// Gets the time interval between TCP keepalive probes.
385
+ ///
386
+ /// Available on neither Redox nor Solaris.
387
+ #[ cfg( not( any( target_os = "redox" , target_os = "solaris" ) ) ) ]
388
+ pub fn get_keepalives_interval ( & self ) -> Option < & Duration > {
389
+ self . keepalive_config . interval . as_ref ( )
390
+ }
391
+
392
+ /// Sets the maximum number of TCP keepalive probes that will be sent before dropping a connection.
393
+ ///
394
+ /// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled.
395
+ ///
396
+ /// Available on neither Redox, Solaris nor Windows.
397
+ #[ cfg( not( any( target_os = "redox" , target_os = "solaris" , target_os = "windows" ) ) ) ]
398
+ pub fn keepalives_retries ( & mut self , keepalives_retries : u32 ) -> & mut Config {
399
+ self . keepalive_config . retries = Some ( keepalives_retries) ;
400
+ self
401
+ }
402
+
403
+ /// Gets the maximum number of TCP keepalive probes that will be sent before dropping a connection.
404
+ ///
405
+ /// Available on neither Redox, Solaris nor Windows.
406
+ #[ cfg( not( any( target_os = "redox" , target_os = "solaris" , target_os = "windows" ) ) ) ]
407
+ pub fn get_keepalives_retries ( & self ) -> Option < & u32 > {
408
+ self . keepalive_config . retries . as_ref ( )
358
409
}
359
410
360
411
/// Sets the requirements of the session.
@@ -451,6 +502,22 @@ impl Config {
451
502
self . keepalives_idle ( Duration :: from_secs ( keepalives_idle as u64 ) ) ;
452
503
}
453
504
}
505
+ #[ cfg( not( any( target_os = "redox" , target_os = "solaris" ) ) ) ]
506
+ "keepalives_interval" => {
507
+ let keepalives_interval = value. parse :: < i64 > ( ) . map_err ( |_| {
508
+ Error :: config_parse ( Box :: new ( InvalidValue ( "keepalives_interval" ) ) )
509
+ } ) ?;
510
+ if keepalives_interval > 0 {
511
+ self . keepalives_interval ( Duration :: from_secs ( keepalives_interval as u64 ) ) ;
512
+ }
513
+ }
514
+ #[ cfg( not( any( target_os = "redox" , target_os = "solaris" , target_os = "windows" ) ) ) ]
515
+ "keepalives_retries" => {
516
+ let keepalives_retries = value. parse :: < u32 > ( ) . map_err ( |_| {
517
+ Error :: config_parse ( Box :: new ( InvalidValue ( "keepalives_retries" ) ) )
518
+ } ) ?;
519
+ self . keepalives_retries ( keepalives_retries) ;
520
+ }
454
521
"target_session_attrs" => {
455
522
let target_session_attrs = match value {
456
523
"any" => TargetSessionAttrs :: Any ,
@@ -534,8 +601,8 @@ impl fmt::Debug for Config {
534
601
}
535
602
}
536
603
537
- f. debug_struct ( "Config" )
538
- . field ( "user" , & self . user )
604
+ let mut ds = f. debug_struct ( "Config" ) ;
605
+ ds . field ( "user" , & self . user )
539
606
. field ( "password" , & self . password . as_ref ( ) . map ( |_| Redaction { } ) )
540
607
. field ( "dbname" , & self . dbname )
541
608
. field ( "options" , & self . options )
@@ -545,8 +612,19 @@ impl fmt::Debug for Config {
545
612
. field ( "port" , & self . port )
546
613
. field ( "connect_timeout" , & self . connect_timeout )
547
614
. field ( "keepalives" , & self . keepalives )
548
- . field ( "keepalives_idle" , & self . keepalives_idle )
549
- . field ( "target_session_attrs" , & self . target_session_attrs )
615
+ . field ( "keepalives_idle" , & self . keepalive_config . idle ) ;
616
+
617
+ #[ cfg( not( any( target_os = "redox" , target_os = "solaris" ) ) ) ]
618
+ {
619
+ ds. field ( "keepalives_interval" , & self . keepalive_config . interval ) ;
620
+ }
621
+
622
+ #[ cfg( not( any( target_os = "redox" , target_os = "solaris" , target_os = "windows" ) ) ) ]
623
+ {
624
+ ds. field ( "keepalives_retries" , & self . keepalive_config . retries ) ;
625
+ }
626
+
627
+ ds. field ( "target_session_attrs" , & self . target_session_attrs )
550
628
. field ( "channel_binding" , & self . channel_binding )
551
629
. finish ( )
552
630
}
0 commit comments