pub struct Window<T>{ /* private fields */ }Expand description
A load balancer that limits the number of allocations per entry over a specified time interval.
This implementation supports both async and sync allocation.
Implementations§
Source§impl<T> Window<T>
impl<T> Window<T>
Sourcepub fn new(entries: Vec<(u64, T)>) -> Self
pub fn new(entries: Vec<(u64, T)>) -> Self
Create a new Window with the default interval of 1 second.
§Arguments
entries- A vector of tuples(max_count, value).
Sourcepub fn new_interval(entries: Vec<(u64, T)>, interval: Duration) -> Self
pub fn new_interval(entries: Vec<(u64, T)>, interval: Duration) -> Self
Create a new Window with a custom interval duration.
§Arguments
entries- A vector of tuples(max_count, value).interval- Duration after which allocation counts are reset.
Sourcepub async fn update<F, R, N>(&self, handler: F) -> Result<N>
pub async fn update<F, R, N>(&self, handler: F) -> Result<N>
Update the load balancer using an async callback.
Sourcepub async fn update_timer<F, R>(
&self,
handler: F,
interval: Duration,
) -> Result<JoinHandle<()>>
pub async fn update_timer<F, R>( &self, handler: F, interval: Duration, ) -> Result<JoinHandle<()>>
Spawn a background task that calls handler every interval.
The handler is called once immediately; if that initial call fails
the error is returned and no background task is spawned.
Returns a JoinHandle that can be aborted to stop the timer.
Sourcepub async fn alloc_skip(&self, index: usize) -> (usize, T)
pub async fn alloc_skip(&self, index: usize) -> (usize, T)
Asynchronously allocate an entry, skipping the specified index. Loops until a valid entry is found.
Sourcepub fn try_alloc_skip(&self, index: usize) -> Option<(usize, T)>
pub fn try_alloc_skip(&self, index: usize) -> Option<(usize, T)>
Try to allocate an entry without awaiting.
Returns None immediately if no entry is available.
Trait Implementations§
Auto Trait Implementations§
impl<T> !RefUnwindSafe for Window<T>
impl<T> !UnwindSafe for Window<T>
impl<T> Freeze for Window<T>
impl<T> Send for Window<T>
impl<T> Sync for Window<T>
impl<T> Unpin for Window<T>
impl<T> UnsafeUnpin for Window<T>
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