pub struct BackpressureGuard { /* private fields */ }Expand description
Tracks in-flight work count and enforces a capacity limit.
§Guarantees
- Thread-safe via
Arc<Mutex<_>> try_acquireis non-blockingreleasedecrements the counter; no-op if counter is already 0- Optional soft limit emits a warning when depth reaches the threshold
Implementations§
Source§impl BackpressureGuard
impl BackpressureGuard
Sourcepub fn new(capacity: usize) -> Result<Self, AgentRuntimeError>
pub fn new(capacity: usize) -> Result<Self, AgentRuntimeError>
Create a new guard with the given capacity.
§Returns
Ok(BackpressureGuard)— on successErr(AgentRuntimeError::Orchestration)— ifcapacity == 0
Sourcepub fn with_soft_limit(self, soft: usize) -> Result<Self, AgentRuntimeError>
pub fn with_soft_limit(self, soft: usize) -> Result<Self, AgentRuntimeError>
Set a soft capacity threshold. When depth reaches this level, a warning is logged but the request is still accepted (up to hard capacity).
Sourcepub fn try_acquire(&self) -> Result<(), AgentRuntimeError>
pub fn try_acquire(&self) -> Result<(), AgentRuntimeError>
Try to acquire a slot.
Emits a warning when the soft limit is reached (if configured), but still accepts the request until hard capacity is exceeded.
§Returns
Ok(())— slot acquiredErr(AgentRuntimeError::BackpressureShed)— hard capacity exceeded
Sourcepub fn release(&self) -> Result<(), AgentRuntimeError>
pub fn release(&self) -> Result<(), AgentRuntimeError>
Release a previously acquired slot.
Sourcepub fn reset(&self)
pub fn reset(&self)
Reset the current depth to zero.
Useful in tests or after a controlled shutdown when all in-flight requests have been cancelled and the guard should start fresh.
Sourcepub fn is_full(&self) -> Result<bool, AgentRuntimeError>
pub fn is_full(&self) -> Result<bool, AgentRuntimeError>
Return true if the guard is at or over its hard capacity.
Sourcepub fn is_empty(&self) -> Result<bool, AgentRuntimeError>
pub fn is_empty(&self) -> Result<bool, AgentRuntimeError>
Return true if no slots are currently in use.
Sourcepub fn available_capacity(&self) -> Result<usize, AgentRuntimeError>
pub fn available_capacity(&self) -> Result<usize, AgentRuntimeError>
Return the number of additional request slots available before the hard cap.
Sourcepub fn hard_capacity(&self) -> usize
pub fn hard_capacity(&self) -> usize
Return the hard capacity (maximum concurrent slots) configured for this guard.
Sourcepub fn soft_limit(&self) -> Option<usize>
pub fn soft_limit(&self) -> Option<usize>
Return the soft capacity limit if one was configured, or None.
Sourcepub fn is_soft_limited(&self) -> bool
pub fn is_soft_limited(&self) -> bool
Return true if a soft capacity limit has been configured.
Equivalent to self.soft_limit().is_some() but more readable at call
sites that only need a boolean check.
Sourcepub fn depth(&self) -> Result<usize, AgentRuntimeError>
pub fn depth(&self) -> Result<usize, AgentRuntimeError>
Return the current depth.
Sourcepub fn percent_full(&self) -> Result<f64, AgentRuntimeError>
pub fn percent_full(&self) -> Result<f64, AgentRuntimeError>
Return the current depth as a percentage of the hard capacity.
Returns a value in [0.0, 100.0]. When depth > capacity (which
cannot happen in normal operation) the result is clamped to 100.0.
Sourcepub fn soft_depth_ratio(&self) -> f32
pub fn soft_depth_ratio(&self) -> f32
Return the ratio of current depth to soft capacity as a value in [0.0, ∞).
Returns 0.0 if no soft limit has been configured.
Values above 1.0 mean the soft limit has been exceeded.
Sourcepub fn utilization_ratio(&self) -> Result<f32, AgentRuntimeError>
pub fn utilization_ratio(&self) -> Result<f32, AgentRuntimeError>
Return the fraction of the hard capacity currently in use: depth / capacity.
Returns 0.0 when no slots are in use, 1.0 when fully saturated.
Sourcepub fn remaining_capacity(&self) -> Result<usize, AgentRuntimeError>
pub fn remaining_capacity(&self) -> Result<usize, AgentRuntimeError>
Return the number of additional slots that can be acquired before hitting the hard capacity limit.
Returns 0 when the guard is full.
Sourcepub fn reset_depth(&self) -> Result<(), AgentRuntimeError>
pub fn reset_depth(&self) -> Result<(), AgentRuntimeError>
Force the in-flight depth counter to zero.
Useful for test teardown or hard resets where acquired slots will never
be released normally (e.g., after a test panics before calling release).
Sourcepub fn headroom_ratio(&self) -> Result<f64, AgentRuntimeError>
pub fn headroom_ratio(&self) -> Result<f64, AgentRuntimeError>
Return the fraction of capacity that is still available, in [0.0, 1.0].
1.0 means completely empty; 0.0 means at full capacity.
Sourcepub fn acquired_count(&self) -> Result<usize, AgentRuntimeError>
pub fn acquired_count(&self) -> Result<usize, AgentRuntimeError>
Return the number of currently held (acquired) slots.
Equivalent to capacity - available_capacity().
Sourcepub fn over_soft_limit(&self) -> Result<bool, AgentRuntimeError>
pub fn over_soft_limit(&self) -> Result<bool, AgentRuntimeError>
Return true if the current depth exceeds the configured soft limit.
Returns false if no soft limit is set.
Trait Implementations§
Source§impl Clone for BackpressureGuard
impl Clone for BackpressureGuard
Source§fn clone(&self) -> BackpressureGuard
fn clone(&self) -> BackpressureGuard
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more