feat: add TCP keepalive options and connection pooling for improved performance
- Added `socket2` dependency for socket options. - Introduced `keep_alive`, `keep_alive_initial_delay_ms`, and `max_connections` fields in `ConnectionConfig`. - Implemented TCP keepalive settings in `TcpListenerManager` for both client and backend connections. - Created a new `ConnectionPool` for managing idle HTTP/1.1 and HTTP/2 connections to reduce overhead. - Enhanced TLS configuration to support ALPN for HTTP/2. - Added performance tests for connection pooling, stability, and concurrent connections.
This commit is contained in:
19
rust/crates/rustproxy-passthrough/src/socket_opts.rs
Normal file
19
rust/crates/rustproxy-passthrough/src/socket_opts.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
//! Socket-level options for TCP streams (keepalive, etc.).
|
||||
//!
|
||||
//! Uses `socket2::SockRef::from()` to borrow the raw fd without ownership transfer.
|
||||
|
||||
use std::io;
|
||||
use std::time::Duration;
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
/// Apply TCP keepalive to a connected socket.
|
||||
///
|
||||
/// Enables SO_KEEPALIVE and sets the initial probe delay.
|
||||
/// On Linux, also sets the interval between probes to the same value.
|
||||
pub fn apply_keepalive(stream: &TcpStream, delay: Duration) -> io::Result<()> {
|
||||
let sock_ref = socket2::SockRef::from(stream);
|
||||
let ka = socket2::TcpKeepalive::new().with_time(delay);
|
||||
#[cfg(target_os = "linux")]
|
||||
let ka = ka.with_interval(delay);
|
||||
sock_ref.set_tcp_keepalive(&ka)
|
||||
}
|
||||
Reference in New Issue
Block a user