pub struct ConnectionLimiter { /* private fields */ }Expand description
Track active connections per hostname to enforce max-connection-per-server limit.
Uses DashMap + AtomicUsize for interior mutability, so every method takes
&self and the limiter can be shared via Arc<ConnectionLimiter> without any
wrapping Mutex/RwLock. This is a soft limiter: the CAS-with-rollback
pattern in ConnectionLimiter::try_acquire keeps the global and per-host
counts bounded by their limits at the moment of each successful CAS, but
snapshot readers (e.g. ConnectionLimiter::host_count) may observe
briefly-stale values.
Implementations§
Source§impl ConnectionLimiter
impl ConnectionLimiter
Sourcepub fn new(global: usize, per_host: usize) -> Self
pub fn new(global: usize, per_host: usize) -> Self
Create a new ConnectionLimiter with the given global and per-host limits.
Sourcepub fn try_acquire(&self, host: &str) -> bool
pub fn try_acquire(&self, host: &str) -> bool
Try to acquire a connection slot for the given host.
Returns true if the slot was acquired, false if either the global or
the per-host limit has been reached.
§Algorithm (CAS-with-rollback)
- Atomically increment
global_countvia CAS; abort if at/above limit. - Atomically increment the per-host counter via CAS; if the per-host
limit is hit, roll back the global increment so
global_countstays accurate.
This guarantees global_count never exceeds global_limit at the
moment of a successful CAS, and each host’s counter never exceeds
per_host_limit. A brief shard-level write lock is held by the
DashMap Entry guard during the per-host CAS — acceptable for a
connection limiter which is not a hot path.
Sourcepub fn release(&self, host: &str)
pub fn release(&self, host: &str)
Release a previously-acquired connection slot for the given host.
The caller must call this exactly once per successful try_acquire for
the same host. Double-releases are detected via debug_assert! in debug
builds.
Sourcepub fn host_count(&self, host: &str) -> usize
pub fn host_count(&self, host: &str) -> usize
Current connection count for a host (snapshot — may be slightly stale).
Sourcepub fn global_count(&self) -> usize
pub fn global_count(&self) -> usize
Total connection count across all hosts (snapshot — may be slightly stale).
Sourcepub fn global_limit(&self) -> usize
pub fn global_limit(&self) -> usize
The configured global connection limit.
Sourcepub fn per_host_limit(&self) -> usize
pub fn per_host_limit(&self) -> usize
The configured per-host connection limit.
Sourcepub fn available_for(&self, host: &str) -> usize
pub fn available_for(&self, host: &str) -> usize
How many slots are still available for the given host (snapshot).
Returns 0 if the host is at or above its per-host limit.
Auto Trait Implementations§
impl !Freeze for ConnectionLimiter
impl !RefUnwindSafe for ConnectionLimiter
impl Send for ConnectionLimiter
impl Sync for ConnectionLimiter
impl Unpin for ConnectionLimiter
impl UnsafeUnpin for ConnectionLimiter
impl UnwindSafe for ConnectionLimiter
Blanket Implementations§
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
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