Backpressure

Struct Backpressure 

Source
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

Source

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
Source

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.

Source

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.

Source

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.

Source

pub fn current_size(&self) -> usize

Get the current buffer usage.

Source

pub const fn max_size(&self) -> usize

Get the maximum buffer size.

Source

pub fn is_full(&self) -> bool

Check if the buffer is full.

Source

pub fn available(&self) -> usize

Get the available space.

Source

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();
Source

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);
Source

pub fn available_permits(&self) -> usize

Get the number of available permits.

Trait Implementations§

Source§

impl Debug for Backpressure

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Backpressure

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V