pub struct ConnectionPool { /* private fields */ }Expand description
Per-peer connection pool with idle eviction and capacity enforcement.
The pool stores connections grouped by peer_id. On acquire
it first looks for an existing idle connection; if none is available it
creates a new Connecting slot, subject to both per-peer and global limits.
Implementations§
Source§impl ConnectionPool
impl ConnectionPool
Sourcepub fn new(config: PoolConfig) -> Self
pub fn new(config: PoolConfig) -> Self
Create a new pool with the given configuration.
Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
Create a new pool with default configuration.
Sourcepub fn acquire(&self, peer_id: &str) -> Result<u64, PoolError>
pub fn acquire(&self, peer_id: &str) -> Result<u64, PoolError>
Acquire a connection to peer_id.
- If an idle connection exists for the peer it is transitioned to
Connectingand its ID is returned. - Otherwise a new slot in
Connectingstate is created, provided neither the per-peer limit nor the global limit would be exceeded.
§Errors
PoolError::PeerCapacityExceededwhen the per-peer limit is reached.PoolError::GlobalCapacityExceededwhen the global limit is reached.
Sourcepub fn mark_active(&self, connection_id: u64) -> Result<(), PoolError>
pub fn mark_active(&self, connection_id: u64) -> Result<(), PoolError>
Transition connection_id to Active.
§Errors
Returns PoolError::ConnectionNotFound if no connection with the given
ID exists.
Sourcepub fn mark_idle(&self, connection_id: u64) -> Result<(), PoolError>
pub fn mark_idle(&self, connection_id: u64) -> Result<(), PoolError>
Transition connection_id to Idle.
§Errors
Returns PoolError::ConnectionNotFound if no connection with the given
ID exists.
Sourcepub fn mark_failed(
&self,
connection_id: u64,
reason: &str,
) -> Result<(), PoolError>
pub fn mark_failed( &self, connection_id: u64, reason: &str, ) -> Result<(), PoolError>
Transition connection_id to Failed with
the supplied human-readable reason.
§Errors
Returns PoolError::ConnectionNotFound if no connection with the given
ID exists.
Sourcepub fn release(&self, connection_id: u64)
pub fn release(&self, connection_id: u64)
Remove connection_id from the pool entirely.
This is a no-op when the connection does not exist. Empty peer buckets are cleaned up automatically.
Sourcepub fn evict_idle(&self, now: Instant) -> usize
pub fn evict_idle(&self, now: Instant) -> usize
Remove all connections that have been idle longer than
PoolConfig::idle_timeout, measured against now.
Returns the number of connections evicted.
Sourcepub fn connection_count(&self) -> usize
pub fn connection_count(&self) -> usize
Total number of connections currently in the pool (all states).
Sourcepub fn idle_count(&self) -> usize
pub fn idle_count(&self) -> usize
Number of connections in the Idle state.
Sourcepub fn active_count(&self) -> usize
pub fn active_count(&self) -> usize
Number of connections in the Active state.
Sourcepub fn stats(&self) -> PoolStatsSnapshot
pub fn stats(&self) -> PoolStatsSnapshot
Return an immutable snapshot of the pool-wide statistics.
Auto Trait Implementations§
impl !Freeze for ConnectionPool
impl RefUnwindSafe for ConnectionPool
impl Send for ConnectionPool
impl Sync for ConnectionPool
impl Unpin for ConnectionPool
impl UnsafeUnpin for ConnectionPool
impl UnwindSafe for ConnectionPool
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