Expand description
Live per-connection TCP statistics, read from the operating system.
TCP connections are done via OS primitives, and reqwest’s pool doesn’t expose statistics and tracking information directly. But we can read the kernel tables to obtain these. Supports Linux, macOS, Windows. Other platforms return empty stats.
Statistics refresh once a second, and a connection idle for longer than the tracker’s timeout expires out of it.
use std::{net::SocketAddr, time::Duration};
use web_faith_conn_tracker::ConnectionTracker;
// Spawns a refresh task, so build it inside a tokio runtime.
let tracker = ConnectionTracker::new(Duration::from_secs(90));
let local: SocketAddr = "127.0.0.1:54321".parse().expect("a valid address");
let remote: SocketAddr = "93.184.216.34:443".parse().expect("a valid address");
// Returns whether this connection had been seen before, so a repeat means it was reused.
println!("reused an existing connection: {}", tracker.track(local, remote));
for connection in tracker.snapshot() {
let Some(stats) = connection.stats else { continue };
println!(
"{}: rtt {}us, cwnd {}, {} retransmits",
connection.remote_addr, stats.rtt_us, stats.cwnd, stats.total_retrans,
);
}Structs§
- Connection
Key - The address pair identifying one connection.
- Connection
Snapshot - One tracked connection.
- Connection
Tracker - A set of tracked TCP connections.
- TcpStats
- The kernel’s view of one TCP connection.