Expand description
Connection Pool
Provides per-peer connection pooling with idle eviction, state transitions, capacity enforcement, and detailed statistics.
§Overview
Maintaining a dedicated connection per request is expensive. This module reuses
existing connections from a per-peer pool and evicts idle ones that have
exceeded the configured idle_timeout.
§Example
use ipfrs_network::connection_pool::{ConnectionPool, PoolConfig};
use std::time::{Duration, Instant};
let config = PoolConfig {
max_connections_per_peer: 4,
max_total_connections: 100,
idle_timeout: Duration::from_secs(60),
connect_timeout: Duration::from_secs(10),
};
let pool = ConnectionPool::new(config);
// Acquire (or create) a connection slot for "peer-1".
let conn_id = pool.acquire("peer-1").expect("acquire failed");
// Transition to active once the handshake completes.
pool.mark_active(conn_id).expect("mark_active failed");
// Return to idle when the request finishes.
pool.mark_idle(conn_id).expect("mark_idle failed");
// Evict connections that have been idle too long.
let evicted = pool.evict_idle(Instant::now());
println!("Evicted {} idle connections", evicted);Structs§
- Connection
Pool - Per-peer connection pool with idle eviction and capacity enforcement.
- Peer
Connection Pool - Tick-based, single-threaded peer connection pool.
- Peer
Pool Config - Configuration for
PeerConnectionPool. - Peer
Pool Stats - A point-in-time snapshot of
PeerConnectionPoolstatistics. - Peer
Pooled Connection - A single connection entry managed by
PeerConnectionPool. - Pool
Config - Configuration knobs for
ConnectionPool. - Pool
Stats - Atomic counters tracking pool-wide activity.
- Pool
Stats Snapshot - Immutable snapshot of
PoolStatssuitable for logging and export. - Pooled
Connection - A single connection entry managed by the pool.
Enums§
- Connection
State - Lifecycle state of a single pooled connection.
- Pool
Connection State - Lifecycle state for a connection managed by
PeerConnectionPool. - Pool
Error - Errors returned by
ConnectionPooloperations.