pub struct SurbRingBuffer<S> {
surbs: Arc<Mutex<VecDeque<(HoprSurbId, S)>>>,
capacity: usize,
pop_order: SurbPopOrder,
}Expand description
Ring buffer of SURBs and their IDs, all normally belonging to one pseudonym and therefore
identified only by [HoprSurbId].
Backed by a VecDeque pre-allocated to capacity and never allowed to exceed it, so it
never reallocates: a push into a full buffer evicts the oldest element first. SurbPopOrder
picks which end a pop consumes from; overflow always evicts the oldest, in either order.
Fields§
§surbs: Arc<Mutex<VecDeque<(HoprSurbId, S)>>>§capacity: usize§pop_order: SurbPopOrderImplementations§
Source§impl<S> SurbRingBuffer<S>
impl<S> SurbRingBuffer<S>
Sourcepub fn new(capacity: usize, pop_order: SurbPopOrder) -> Self
pub fn new(capacity: usize, pop_order: SurbPopOrder) -> Self
Creates a buffer holding at most capacity (min 1, so a push is never a no-op) SURBs,
popped in the given order.
Sourcepub fn push<I: IntoIterator<Item = (HoprSurbId, S)>>(
&self,
surbs: I,
) -> SurbInsertOutcome
pub fn push<I: IntoIterator<Item = (HoprSurbId, S)>>( &self, surbs: I, ) -> SurbInsertOutcome
Pushes all SURBs with their IDs, evicting the oldest ones past capacity.
Returns what the push did; the eviction count is what lets a caller notice the overflow at all, since dropping the oldest entry is otherwise indistinguishable from a clean insert.
Sourcepub fn pop_next_valid<F>(&self, is_valid: F) -> Option<PoppedSurb<S>>
pub fn pop_next_valid<F>(&self, is_valid: F) -> Option<PoppedSurb<S>>
Pops the next SURB that is_valid accepts, in the buffer’s SurbPopOrder.
Destructive: rejected entries are discarded, not skipped, so an unusable SURB neither is
handed out nor blocks those behind it. Pass only a validity test — a selective predicate
(say, a routing preference) would drain the buffer. None once it is exhausted without a
match.
is_valid runs outside the lock: it is caller-supplied and may take locks of its own, so
calling it inside the critical section would invite lock-order inversion.
Sourcepub fn pop_one_if_has_id(&self, id: &HoprSurbId) -> Option<PoppedSurb<S>>
pub fn pop_one_if_has_id(&self, id: &HoprSurbId) -> Option<PoppedSurb<S>>
Pops the next SURB (in the buffer’s SurbPopOrder) only if it has the given ID.
Trait Implementations§
Source§impl<S: Clone> Clone for SurbRingBuffer<S>
impl<S: Clone> Clone for SurbRingBuffer<S>
Source§fn clone(&self) -> SurbRingBuffer<S>
fn clone(&self) -> SurbRingBuffer<S>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl<S> !RefUnwindSafe for SurbRingBuffer<S>
impl<S> !UnwindSafe for SurbRingBuffer<S>
impl<S> Freeze for SurbRingBuffer<S>
impl<S> Send for SurbRingBuffer<S>where
S: Send,
impl<S> Sync for SurbRingBuffer<S>where
S: Send,
impl<S> Unpin for SurbRingBuffer<S>
impl<S> UnsafeUnpin for SurbRingBuffer<S>
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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