tokio_postgres/
keepalive.rs

1use socket2::TcpKeepalive;
2use std::time::Duration;
3
4#[derive(Clone, PartialEq, Eq)]
5pub(crate) struct KeepaliveConfig {
6    pub idle: Duration,
7    pub interval: Option<Duration>,
8    pub retries: Option<u32>,
9}
10
11impl From<&KeepaliveConfig> for TcpKeepalive {
12    fn from(keepalive_config: &KeepaliveConfig) -> Self {
13        let mut tcp_keepalive = Self::new().with_time(keepalive_config.idle);
14
15        #[cfg(not(any(
16            target_os = "aix",
17            target_os = "redox",
18            target_os = "solaris",
19            target_os = "openbsd"
20        )))]
21        if let Some(interval) = keepalive_config.interval {
22            tcp_keepalive = tcp_keepalive.with_interval(interval);
23        }
24
25        #[cfg(not(any(
26            target_os = "aix",
27            target_os = "redox",
28            target_os = "solaris",
29            target_os = "windows",
30            target_os = "openbsd"
31        )))]
32        if let Some(retries) = keepalive_config.retries {
33            tcp_keepalive = tcp_keepalive.with_retries(retries);
34        }
35
36        tcp_keepalive
37    }
38}