pub struct Bulkhead { /* private fields */ }Expand description
Bulkhead pattern: limits concurrent operations to prevent resource exhaustion.
Uses a semaphore to limit how many operations can run simultaneously. When the bulkhead is “full”, new operations either wait or fail fast.
§Use Cases
- Limit concurrent peer connections
- Limit concurrent sync-engine calls
- Prevent thread/connection pool exhaustion
§Example
use replication_engine::resilience::Bulkhead;
let bulkhead = Bulkhead::new(10); // Max 10 concurrent
// Blocking acquire
let permit = bulkhead.acquire().await?;
// do_work().await;
drop(permit); // Release slot
// Non-blocking (fail fast)
if let Some(permit) = bulkhead.try_acquire() {
// do_work().await;
drop(permit);
} else {
// return Err("service overloaded");
}Implementations§
Source§impl Bulkhead
impl Bulkhead
Sourcepub fn new(max_concurrent: usize) -> Self
pub fn new(max_concurrent: usize) -> Self
Create a new bulkhead with the given concurrency limit.
Sourcepub fn for_peers() -> Self
pub fn for_peers() -> Self
Create a bulkhead for limiting peer connections.
Default: 50 concurrent connections.
Sourcepub fn for_sync_engine() -> Self
pub fn for_sync_engine() -> Self
Create a bulkhead for limiting sync-engine operations.
Default: 100 concurrent operations.
Sourcepub async fn acquire(&self) -> Result<OwnedSemaphorePermit, BulkheadFull>
pub async fn acquire(&self) -> Result<OwnedSemaphorePermit, BulkheadFull>
Acquire a permit, waiting if necessary.
Returns a permit that releases the slot when dropped.
Sourcepub fn try_acquire(&self) -> Option<OwnedSemaphorePermit>
pub fn try_acquire(&self) -> Option<OwnedSemaphorePermit>
Try to acquire a permit without waiting.
Returns None if the bulkhead is full.
Sourcepub fn max_concurrent(&self) -> usize
pub fn max_concurrent(&self) -> usize
Get the maximum concurrent operations allowed.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Bulkhead
impl !RefUnwindSafe for Bulkhead
impl Send for Bulkhead
impl Sync for Bulkhead
impl Unpin for Bulkhead
impl !UnwindSafe for Bulkhead
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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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