|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +/// TCP keepalive parameters for a connection's socket. |
| 4 | +/// |
| 5 | +/// Keepalive is what lets a client notice that its server is gone when the |
| 6 | +/// server disappeared without closing the socket: a failover, a killed |
| 7 | +/// container, a NAT table that dropped the mapping. Without it, a connection |
| 8 | +/// blocked reading a response it will never receive waits forever, because |
| 9 | +/// there is nothing left to retransmit and so nothing to time out. |
| 10 | +/// |
| 11 | +/// The parameters mirror libpq's `keepalives_idle`, `keepalives_interval` and |
| 12 | +/// `keepalives_count`, and are applied with `setsockopt` after connecting. |
| 13 | +/// |
| 14 | +/// Support varies by platform: `interval` is ignored on OpenBSD and Solaris, |
| 15 | +/// and `retries` is ignored on OpenBSD, Solaris, watchOS and tvOS. `idle` is |
| 16 | +/// supported everywhere keepalive itself is. |
| 17 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 18 | +pub struct TcpKeepalive { |
| 19 | + /// Idle time after which the first keepalive probe is sent. |
| 20 | + pub idle: Option<Duration>, |
| 21 | + /// Time between probes once the first one has been sent. |
| 22 | + pub interval: Option<Duration>, |
| 23 | + /// Number of unacknowledged probes before the connection is dropped. |
| 24 | + pub retries: Option<u32>, |
| 25 | +} |
| 26 | + |
| 27 | +impl TcpKeepalive { |
| 28 | + /// Keepalive with no parameters overridden, leaving the system defaults in |
| 29 | + /// place. On Linux those are 7200s idle, 75s interval, 9 retries. |
| 30 | + pub fn new() -> Self { |
| 31 | + Self::default() |
| 32 | + } |
| 33 | + |
| 34 | + /// Sets the idle time after which the first keepalive probe is sent. |
| 35 | + pub fn with_idle(mut self, idle: Duration) -> Self { |
| 36 | + self.idle = Some(idle); |
| 37 | + self |
| 38 | + } |
| 39 | + |
| 40 | + /// Sets the time between keepalive probes. |
| 41 | + pub fn with_interval(mut self, interval: Duration) -> Self { |
| 42 | + self.interval = Some(interval); |
| 43 | + self |
| 44 | + } |
| 45 | + |
| 46 | + /// Sets the number of unacknowledged probes before the connection is dropped. |
| 47 | + pub fn with_retries(mut self, retries: u32) -> Self { |
| 48 | + self.retries = Some(retries); |
| 49 | + self |
| 50 | + } |
| 51 | + |
| 52 | + #[cfg(any(feature = "_rt-tokio", feature = "_rt-async-io"))] |
| 53 | + pub(crate) fn to_socket2(self) -> socket2::TcpKeepalive { |
| 54 | + let mut keepalive = socket2::TcpKeepalive::new(); |
| 55 | + |
| 56 | + if let Some(idle) = self.idle { |
| 57 | + keepalive = keepalive.with_time(idle); |
| 58 | + } |
| 59 | + |
| 60 | + #[cfg(not(any(target_os = "openbsd", target_os = "solaris")))] |
| 61 | + if let Some(interval) = self.interval { |
| 62 | + keepalive = keepalive.with_interval(interval); |
| 63 | + } |
| 64 | + |
| 65 | + #[cfg(not(any( |
| 66 | + target_os = "openbsd", |
| 67 | + target_os = "solaris", |
| 68 | + target_os = "watchos", |
| 69 | + target_os = "tvos", |
| 70 | + )))] |
| 71 | + if let Some(retries) = self.retries { |
| 72 | + keepalive = keepalive.with_retries(retries); |
| 73 | + } |
| 74 | + |
| 75 | + keepalive |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(test)] |
| 80 | +mod tests { |
| 81 | + use super::*; |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn keepalive_is_disabled_by_default() { |
| 85 | + assert_eq!(TcpKeepalive::new(), TcpKeepalive::default()); |
| 86 | + assert!(TcpKeepalive::new().idle.is_none()); |
| 87 | + } |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn builders_set_each_parameter() { |
| 91 | + let keepalive = TcpKeepalive::new() |
| 92 | + .with_idle(Duration::from_secs(30)) |
| 93 | + .with_interval(Duration::from_secs(10)) |
| 94 | + .with_retries(3); |
| 95 | + |
| 96 | + assert_eq!(keepalive.idle, Some(Duration::from_secs(30))); |
| 97 | + assert_eq!(keepalive.interval, Some(Duration::from_secs(10))); |
| 98 | + assert_eq!(keepalive.retries, Some(3)); |
| 99 | + } |
| 100 | +} |
0 commit comments