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.
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.
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,11 @@ 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
+ interval : None ,
181
+ retries : None ,
182
+ } ;
173
183
Config {
174
184
user : None ,
175
185
password : None ,
@@ -181,7 +191,7 @@ impl Config {
181
191
port : vec ! [ ] ,
182
192
connect_timeout : None ,
183
193
keepalives : true ,
184
- keepalives_idle : Duration :: from_secs ( 2 * 60 * 60 ) ,
194
+ keepalive_config ,
185
195
target_session_attrs : TargetSessionAttrs :: Any ,
186
196
channel_binding : ChannelBinding :: Prefer ,
187
197
}
@@ -347,14 +357,41 @@ impl Config {
347
357
///
348
358
/// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled. Defaults to 2 hours.
349
359
pub fn keepalives_idle ( & mut self , keepalives_idle : Duration ) ->& mut Config {
350
- self . keepalives_idle = keepalives_idle;
360
+ self . keepalive_config . idle = keepalives_idle;
351
361
self
352
362
}
353
363
354
364
/// Gets the configured amount of idle time before a keepalive packet will
355
365
/// be sent on the connection.
356
366
pub fn get_keepalives_idle ( & self ) ->Duration {
357
- self . keepalives_idle
367
+ self . keepalive_config . idle
368
+ }
369
+
370
+ /// Sets the time interval between TCP keepalive probes.
371
+ /// On Windows, this sets the value of the tcp_keepalive struct’s keepaliveinterval field.
372
+ ///
373
+ /// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled.
374
+ pub fn keepalives_interval ( & mut self , keepalives_interval : Duration ) ->& mut Config {
375
+ self . keepalive_config . interval =Some ( keepalives_interval) ;
376
+ self
377
+ }
378
+
379
+ /// Gets the time interval between TCP keepalive probes.
380
+ pub fn get_keepalives_interval ( & self ) ->Option < Duration > {
381
+ self . keepalive_config . interval
382
+ }
383
+
384
+ /// Sets the maximum number of TCP keepalive probes that will be sent before dropping a connection.
385
+ ///
386
+ /// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled.
387
+ pub fn keepalives_retries ( & mut self , keepalives_retries : u32 ) ->& mut Config {
388
+ self . keepalive_config . retries =Some ( keepalives_retries) ;
389
+ self
390
+ }
391
+
392
+ /// Gets the maximum number of TCP keepalive probes that will be sent before dropping a connection.
393
+ pub fn get_keepalives_retries ( & self ) ->Option < u32 > {
394
+ self . keepalive_config . retries
358
395
}
359
396
360
397
/// Sets the requirements of the session.
@@ -451,6 +488,20 @@ impl Config {
451
488
self . keepalives_idle ( Duration :: from_secs ( keepalives_idleas u64 ) ) ;
452
489
}
453
490
}
491
+ "keepalives_interval" =>{
492
+ let keepalives_interval = value. parse :: < i64 > ( ) . map_err ( |_|{
493
+ Error :: config_parse ( Box :: new ( InvalidValue ( "keepalives_interval" ) ) )
494
+ } ) ?;
495
+ if keepalives_interval >0 {
496
+ self . keepalives_interval ( Duration :: from_secs ( keepalives_intervalas u64 ) ) ;
497
+ }
498
+ }
499
+ "keepalives_retries" =>{
500
+ let keepalives_retries = value. parse :: < u32 > ( ) . map_err ( |_|{
501
+ Error :: config_parse ( Box :: new ( InvalidValue ( "keepalives_retries" ) ) )
502
+ } ) ?;
503
+ self . keepalives_retries ( keepalives_retries) ;
504
+ }
454
505
"target_session_attrs" =>{
455
506
let target_session_attrs =match value{
456
507
"any" =>TargetSessionAttrs :: Any ,
@@ -545,7 +596,9 @@ impl fmt::Debug for Config {
545
596
. field ( "port" , & self . port )
546
597
. field ( "connect_timeout" , & self . connect_timeout )
547
598
. field ( "keepalives" , & self . keepalives )
548
- . field ( "keepalives_idle" , & self . keepalives_idle )
599
+ . field ( "keepalives_idle" , & self . keepalive_config . idle )
600
+ . field ( "keepalives_interval" , & self . keepalive_config . interval )
601
+ . field ( "keepalives_retries" , & self . keepalive_config . retries )
549
602
. field ( "target_session_attrs" , & self . target_session_attrs )
550
603
. field ( "channel_binding" , & self . channel_binding )
551
604
. finish ( )