pub struct Backpressure { /* private fields */ }Expand description
A backpressure controller for limiting data flow and concurrent operations.
This controller provides two mechanisms:
- Buffer size tracking: For tracking how much data is in-flight or buffered.
- Permit-based concurrency: For limiting the number of concurrent operations.
Both mechanisms use the same max_size limit but operate independently,
allowing flexible backpressure strategies.
Implementations§
Source§impl Backpressure
impl Backpressure
Sourcepub fn new(max_size: usize) -> Self
pub fn new(max_size: usize) -> Self
Create a new backpressure controller.
The max_size parameter controls both:
- The maximum buffer size for size-based tracking
- The number of available permits for concurrency limiting
Sourcepub fn try_acquire(&self, amount: usize) -> bool
pub fn try_acquire(&self, amount: usize) -> bool
Try to acquire space for the given amount.
Returns true if space was acquired, false if the buffer is full.
This is part of the buffer size tracking mechanism. Use release
to return the space when done.
Sourcepub async fn acquire(&self, amount: usize)
pub async fn acquire(&self, amount: usize)
Acquire space for the given amount, waiting if necessary.
This is part of the buffer size tracking mechanism. Use release
to return the space when done.
Sourcepub fn release(&self, amount: usize)
pub fn release(&self, amount: usize)
Release the given amount of space.
This is part of the buffer size tracking mechanism. Call this after data has been processed/consumed to free space for new data.
Sourcepub fn current_size(&self) -> usize
pub fn current_size(&self) -> usize
Get the current buffer usage.
Sourcepub fn try_acquire_permit(
&self,
) -> Result<OwnedSemaphorePermit, TryAcquireError>
pub fn try_acquire_permit( &self, ) -> Result<OwnedSemaphorePermit, TryAcquireError>
Try to acquire a permit for a concurrent operation.
Returns Ok(permit) if a permit was acquired, or an error if no permits
are available. The permit is automatically released when dropped.
This is part of the permit-based concurrency mechanism. Use this when you want to limit the number of concurrent operations rather than tracking buffer sizes.
§Example
use rust_expect::util::backpressure::Backpressure;
let bp = Backpressure::new(2); // Allow 2 concurrent operations
let permit1 = bp.try_acquire_permit().unwrap();
let permit2 = bp.try_acquire_permit().unwrap();
// Third attempt fails - at capacity
assert!(bp.try_acquire_permit().is_err());
// Dropping a permit frees it
drop(permit1);
let permit3 = bp.try_acquire_permit().unwrap();Sourcepub async fn acquire_permit(&self) -> OwnedSemaphorePermit
pub async fn acquire_permit(&self) -> OwnedSemaphorePermit
Acquire a permit for a concurrent operation, waiting if necessary.
Returns a permit that is automatically released when dropped.
This is part of the permit-based concurrency mechanism. Use this when you want to limit the number of concurrent operations.
§Example
use rust_expect::util::backpressure::Backpressure;
let bp = Backpressure::new(10);
// Acquire a permit - will wait if none available
let permit = bp.acquire_permit().await;
// Do work while holding the permit
// ...
// Permit is released when dropped
drop(permit);Sourcepub fn available_permits(&self) -> usize
pub fn available_permits(&self) -> usize
Get the number of available permits.