interface: tune RNode TCP sockets

This commit is contained in:
DeFiDude committed 2026-05-17 15:23:12 -06:00
1 parent 8ac7f9a625
commit 83c63b463e
2 files changed
+116

No files matched your search

+19
View File
@@ -113,6 +113,16 @@ pub const DEFAULT_TCP_PORT: u16 = 7633;
const RNODE_READ_TIMEOUT_MS: u64 = 100;
#[cfg(feature = "serial")]
const RNODE_TCP_CONNECT_TIMEOUT_SECS: u64 = 5;
#[cfg(feature = "serial")]
const RNODE_TCP_KEEPIDLE_SECS: u64 = 5;
#[cfg(feature = "serial")]
const RNODE_TCP_KEEPINTVL_SECS: u64 = 2;
#[cfg(feature = "serial")]
const RNODE_TCP_KEEPCNT: u32 = 12;
#[cfg(feature = "serial")]
const RNODE_TCP_USER_TIMEOUT_SECS: u64 = 24;
#[cfg(feature = "serial")]
const RNODE_TCP_BUFFER_BYTES: usize = 131_072;
// Transport abstraction
@@ -253,6 +263,15 @@ impl RNodeStream {
fn from_tcp_stream(stream: std::net::TcpStream) -> std::io::Result<Self> {
// Mirror the serial timeout so the read loop doesn't block forever.
stream.set_read_timeout(Some(Duration::from_millis(RNODE_READ_TIMEOUT_MS)))?;
stream.set_nodelay(true)?;
crate::socket_tuning::set_keepalive_tuned_std(
&stream,
Duration::from_secs(RNODE_TCP_KEEPIDLE_SECS),
Duration::from_secs(RNODE_TCP_KEEPINTVL_SECS),
RNODE_TCP_KEEPCNT,
Duration::from_secs(RNODE_TCP_USER_TIMEOUT_SECS),
);
crate::socket_tuning::set_socket_buffers_std(&stream, RNODE_TCP_BUFFER_BYTES);
Ok(Self::Tcp(stream))
}
+97
View File
@@ -29,6 +29,25 @@ pub fn set_keepalive(stream: &TcpStream) -> std::io::Result<()> {
Ok(())
}
/// Enable portable `SO_KEEPALIVE` on a blocking std TCP stream.
#[cfg(unix)]
pub fn set_keepalive_std(stream: &std::net::TcpStream) -> std::io::Result<()> {
use std::os::fd::{AsRawFd, FromRawFd};
let raw_fd = stream.as_raw_fd();
let sock = std::mem::ManuallyDrop::new(unsafe { socket2::Socket::from_raw_fd(raw_fd) });
sock.set_keepalive(true)?;
Ok(())
}
#[cfg(windows)]
pub fn set_keepalive_std(stream: &std::net::TcpStream) -> std::io::Result<()> {
use std::os::windows::io::{AsRawSocket, FromRawSocket};
let raw = stream.as_raw_socket();
let sock = std::mem::ManuallyDrop::new(unsafe { socket2::Socket::from_raw_socket(raw) });
sock.set_keepalive(true)?;
Ok(())
}
/// Tuned keepalive: idle/interval/retries + Linux TCP_USER_TIMEOUT. Best-effort.
#[cfg(unix)]
pub fn set_keepalive_tuned(
@@ -58,6 +77,35 @@ pub fn set_keepalive_tuned(
apply_tuned_keepalive(&sock, idle, intvl, retries, user_timeout);
}
/// Tuned keepalive for blocking std TCP streams. Best-effort.
#[cfg(unix)]
pub fn set_keepalive_tuned_std(
stream: &std::net::TcpStream,
idle: Duration,
intvl: Duration,
retries: u32,
user_timeout: Duration,
) {
use std::os::fd::{AsRawFd, FromRawFd};
let raw_fd = stream.as_raw_fd();
let sock = std::mem::ManuallyDrop::new(unsafe { socket2::Socket::from_raw_fd(raw_fd) });
apply_tuned_keepalive(&sock, idle, intvl, retries, user_timeout);
}
#[cfg(windows)]
pub fn set_keepalive_tuned_std(
stream: &std::net::TcpStream,
idle: Duration,
intvl: Duration,
retries: u32,
user_timeout: Duration,
) {
use std::os::windows::io::{AsRawSocket, FromRawSocket};
let raw = stream.as_raw_socket();
let sock = std::mem::ManuallyDrop::new(unsafe { socket2::Socket::from_raw_socket(raw) });
apply_tuned_keepalive(&sock, idle, intvl, retries, user_timeout);
}
fn apply_tuned_keepalive(
sock: &socket2::Socket,
idle: Duration,
@@ -107,6 +155,25 @@ pub fn set_socket_buffers(stream: &TcpStream, size: usize) {
let _ = sock.set_send_buffer_size(size);
}
/// Raise TCP send/recv buffers on a blocking std TCP stream; best-effort.
#[cfg(unix)]
pub fn set_socket_buffers_std(stream: &std::net::TcpStream, size: usize) {
use std::os::fd::{AsRawFd, FromRawFd};
let raw_fd = stream.as_raw_fd();
let sock = std::mem::ManuallyDrop::new(unsafe { socket2::Socket::from_raw_fd(raw_fd) });
let _ = sock.set_recv_buffer_size(size);
let _ = sock.set_send_buffer_size(size);
}
#[cfg(windows)]
pub fn set_socket_buffers_std(stream: &std::net::TcpStream, size: usize) {
use std::os::windows::io::{AsRawSocket, FromRawSocket};
let raw = stream.as_raw_socket();
let sock = std::mem::ManuallyDrop::new(unsafe { socket2::Socket::from_raw_socket(raw) });
let _ = sock.set_recv_buffer_size(size);
let _ = sock.set_send_buffer_size(size);
}
/// Resolve interface name to `IpAddr`; `None` if missing. Caller falls back
/// to wildcard bind.
pub fn iface_addr_for(name: &str, prefer_ipv6: bool) -> Option<IpAddr> {
@@ -177,6 +244,36 @@ mod tests {
set_socket_buffers(&client, 131_072);
}
#[test]
fn std_keepalive_and_buffers_apply_without_panic() {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = listener.local_addr().unwrap();
let accept = std::thread::spawn(move || listener.accept().unwrap().0);
let client = std::net::TcpStream::connect(addr).unwrap();
let server = accept.join().unwrap();
set_keepalive_std(&server).expect("server set_keepalive_std");
set_keepalive_std(&client).expect("client set_keepalive_std");
set_keepalive_tuned_std(
&server,
Duration::from_secs(5),
Duration::from_secs(2),
12,
Duration::from_secs(24),
);
set_keepalive_tuned_std(
&client,
Duration::from_secs(5),
Duration::from_secs(2),
12,
Duration::from_secs(24),
);
set_socket_buffers_std(&server, 131_072);
set_socket_buffers_std(&client, 131_072);
}
#[test]
fn iface_addr_for_loopback_resolves() {
// Loopback name varies by OS; iterate candidates.