use std::net::TcpStream;
use std::time::Duration;
pub(crate) const IDLE: Duration = Duration::from_secs(2);
pub(crate) const INTERVAL: Duration = Duration::from_secs(1);
pub(crate) const RETRIES: u32 = 3;
#[cfg(any(target_os = "linux", target_os = "android"))]
pub(crate) const USER_TIMEOUT: Duration = Duration::from_secs(6);
pub(crate) fn enable(stream: &TcpStream) {
set(stream, IDLE, INTERVAL, RETRIES, USER_TIMEOUT_SHORT);
}
pub(crate) const UDP_IDLE: Duration = Duration::from_secs(30);
pub(crate) const UDP_INTERVAL: Duration = Duration::from_secs(5);
pub(crate) const UDP_RETRIES: u32 = 6;
#[cfg_attr(not(any(target_os = "linux", target_os = "android")), allow(dead_code))]
pub(crate) const UDP_USER_TIMEOUT: Duration = Duration::from_secs(60);
#[cfg(any(target_os = "linux", target_os = "android"))]
const USER_TIMEOUT_SHORT: Duration = USER_TIMEOUT;
#[cfg(not(any(target_os = "linux", target_os = "android")))]
const USER_TIMEOUT_SHORT: Duration = Duration::ZERO;
pub(crate) fn relax(stream: &TcpStream) {
set(
stream,
UDP_IDLE,
UDP_INTERVAL,
UDP_RETRIES,
UDP_USER_TIMEOUT,
);
}
#[allow(unused_variables)]
fn set(stream: &TcpStream, idle: Duration, interval: Duration, retries: u32, user: Duration) {
let sock = socket2::SockRef::from(stream);
let params = socket2::TcpKeepalive::new()
.with_time(idle)
.with_interval(interval);
#[cfg(any(
target_os = "linux",
target_os = "android",
target_os = "macos",
target_os = "ios",
target_os = "freebsd",
target_os = "windows",
))]
let params = params.with_retries(retries);
let _ = sock.set_tcp_keepalive(¶ms);
#[cfg(any(target_os = "linux", target_os = "android"))]
let _ = sock.set_tcp_user_timeout(Some(user));
}
#[cfg(test)]
mod tests {
use super::*;
use std::net::TcpListener;
#[test]
fn a_connected_stream_takes_the_short_timers() {
let listener = match TcpListener::bind("127.0.0.1:0") {
Ok(l) => l,
Err(_) => {
eprintln!("no local network; skipping");
return;
}
};
let stream = TcpStream::connect(listener.local_addr().unwrap()).unwrap();
enable(&stream);
let sock = socket2::SockRef::from(&stream);
assert!(sock.keepalive().unwrap());
#[cfg(any(target_os = "linux", target_os = "android"))]
{
assert_eq!(sock.tcp_keepalive_time().unwrap(), IDLE);
assert_eq!(sock.tcp_keepalive_interval().unwrap(), INTERVAL);
assert_eq!(sock.tcp_keepalive_retries().unwrap(), RETRIES);
}
#[cfg(any(target_os = "linux", target_os = "android"))]
assert_eq!(sock.tcp_user_timeout().unwrap(), Some(USER_TIMEOUT));
}
#[test]
fn relax_takes_the_long_timers_and_enable_the_short_ones() {
let listener = match TcpListener::bind("127.0.0.1:0") {
Ok(l) => l,
Err(_) => {
eprintln!("no local network; skipping");
return;
}
};
let stream = TcpStream::connect(listener.local_addr().unwrap()).unwrap();
relax(&stream);
#[cfg(any(target_os = "linux", target_os = "android"))]
{
let sock = socket2::SockRef::from(&stream);
assert_eq!(sock.tcp_keepalive_time().unwrap(), UDP_IDLE);
assert_eq!(sock.tcp_keepalive_interval().unwrap(), UDP_INTERVAL);
assert_eq!(sock.tcp_keepalive_retries().unwrap(), UDP_RETRIES);
assert_eq!(sock.tcp_user_timeout().unwrap(), Some(UDP_USER_TIMEOUT));
enable(&stream);
assert_eq!(sock.tcp_keepalive_time().unwrap(), IDLE);
assert_eq!(sock.tcp_user_timeout().unwrap(), Some(USER_TIMEOUT));
}
}
}