pub struct PeerConnectionPool {
pub connections: HashMap<u64, PeerPooledConnection>,
pub next_conn_id: u64,
pub config: PeerPoolConfig,
pub total_acquired: u64,
pub total_released: u64,
pub total_evicted: u64,
}Expand description
Tick-based, single-threaded peer connection pool.
Manages a set of reusable PeerPooledConnections keyed by a monotonically
increasing conn_id. Callers drive time by passing an explicit tick
counter; the pool does not use wall-clock time.
§Example
use ipfrs_network::connection_pool::{PeerConnectionPool, PeerPoolConfig};
let mut pool = PeerConnectionPool::new(PeerPoolConfig::default());
let tick = 0_u64;
// Acquire (or create) a connection for "peer-1".
let conn_id = pool.acquire("peer-1", tick).expect("should succeed");
// Return the connection to the idle pool.
assert!(pool.release(conn_id, tick + 1));
// Evict connections that have been idle for too long.
pool.evict_idle(tick + 100);Fields§
§connections: HashMap<u64, PeerPooledConnection>All connections, keyed by their unique conn_id.
next_conn_id: u64Monotonically increasing counter for assigning connection IDs.
config: PeerPoolConfigPool configuration.
total_acquired: u64Cumulative successful acquires.
total_released: u64Cumulative successful releases.
total_evicted: u64Cumulative evictions.
Implementations§
Source§impl PeerConnectionPool
impl PeerConnectionPool
Sourcepub fn new(config: PeerPoolConfig) -> Self
pub fn new(config: PeerPoolConfig) -> Self
Create a new, empty pool with the given configuration.
Sourcepub fn release(&mut self, conn_id: u64, tick: u64) -> bool
pub fn release(&mut self, conn_id: u64, tick: u64) -> bool
Return a connection to the idle pool.
Returns true on success, false if the connection was not found or
was not in the InUse state.
Sourcepub fn close(&mut self, conn_id: u64) -> bool
pub fn close(&mut self, conn_id: u64) -> bool
Mark a connection as Closing.
Returns false if no connection with the given ID exists.
Sourcepub fn evict_idle(&mut self, tick: u64)
pub fn evict_idle(&mut self, tick: u64)
Evict connections that are stale or marked for teardown.
Removes:
Idleconnections whoselast_used_tickis at leastidle_timeout_ticksticks in the past.- All
Closingconnections.
total_evicted is incremented for each removed
connection.
Sourcepub fn connections_for_peer<'a>(
&'a self,
peer_id: &str,
) -> Vec<&'a PeerPooledConnection>
pub fn connections_for_peer<'a>( &'a self, peer_id: &str, ) -> Vec<&'a PeerPooledConnection>
Return all connections associated with peer_id, sorted ascending by
conn_id.
Sourcepub fn stats(&self) -> PeerPoolStats
pub fn stats(&self) -> PeerPoolStats
Return a point-in-time snapshot of pool statistics.
Auto Trait Implementations§
impl Freeze for PeerConnectionPool
impl RefUnwindSafe for PeerConnectionPool
impl Send for PeerConnectionPool
impl Sync for PeerConnectionPool
impl Unpin for PeerConnectionPool
impl UnsafeUnpin for PeerConnectionPool
impl UnwindSafe for PeerConnectionPool
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more