Skip to main content

Module connection_pool

Module connection_pool 

Source
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§

ConnectionPool
Per-peer connection pool with idle eviction and capacity enforcement.
PeerConnectionPool
Tick-based, single-threaded peer connection pool.
PeerPoolConfig
Configuration for PeerConnectionPool.
PeerPoolStats
A point-in-time snapshot of PeerConnectionPool statistics.
PeerPooledConnection
A single connection entry managed by PeerConnectionPool.
PoolConfig
Configuration knobs for ConnectionPool.
PoolStats
Atomic counters tracking pool-wide activity.
PoolStatsSnapshot
Immutable snapshot of PoolStats suitable for logging and export.
PooledConnection
A single connection entry managed by the pool.

Enums§

ConnectionState
Lifecycle state of a single pooled connection.
PoolConnectionState
Lifecycle state for a connection managed by PeerConnectionPool.
PoolError
Errors returned by ConnectionPool operations.