pub struct ConnPool { /* private fields */ }Expand description
Per-backend HTTP/1.1 connection pool.
Holds idle TcpStream connections keyed by "host:port". When a
backend responds with Connection: keep-alive (or the HTTP/1.1 default),
the stream is returned here and reused for the next request to the same
backend, eliminating the TCP-handshake cost and reducing ephemeral-port
exhaustion under load.
§Thread safety
All methods take &self and are safe to call from multiple threads.
The inner map is protected by a Mutex.
§Example
use std::sync::Arc;
use std::time::Duration;
use rust_web_server::proxy::ConnPool;
use rust_web_server::proxy::ReverseProxy;
let pool = Arc::new(ConnPool::new(16, Duration::from_secs(30)));
let _proxy = ReverseProxy::new(["http://backend:8080"])
.with_pool(Arc::clone(&pool));Implementations§
Source§impl ConnPool
impl ConnPool
Sourcepub fn new(max_idle: usize, idle_timeout: Duration) -> Self
pub fn new(max_idle: usize, idle_timeout: Duration) -> Self
Create a pool with the given per-backend idle limit and idle timeout.
Sourcepub fn new_default() -> Self
pub fn new_default() -> Self
Create a pool with defaults: 8 idle connections per backend, 60-second timeout.
Sourcepub fn acquire(&self, key: &str) -> Option<TcpStream>
pub fn acquire(&self, key: &str) -> Option<TcpStream>
Try to acquire an idle connection for key = "host:port".
Stale entries (older than idle_timeout) are discarded automatically.
Returns None if no usable connection is available.
Sourcepub fn release(&self, key: &str, stream: TcpStream)
pub fn release(&self, key: &str, stream: TcpStream)
Return a keep-alive connection to the pool.
If the backend slot is already at max_idle, the stream is dropped
(the TCP connection closes) rather than exceeding the limit.
Sourcepub fn idle_count(&self) -> usize
pub fn idle_count(&self) -> usize
Total idle connections across all backends (useful for testing/metrics).