pub struct WrrQueue<T: PartialEq> { /* private fields */ }
Expand description

weighted round robin queue struct

WRR queue, each time new instance is inserted, balance queue need to be recalculated. So minimizing the insert operation can improve performance.

select method requires only an atomic usize and a Read access to the RwLock. There should be of no runtime performance issue.

example:

use async_wrr_queue::{WrrQueue, Instance};
use std::num::NonZeroUsize;

let mut queue = WrrQueue::new().insert_many([("data1", 1), ("data2", 2), ("data3", 3)]).await;
queue.insert(Instance::new_with_weight("data4", NonZeroUsize::new(4).unwrap())).await;

let selected1 = queue.select();
let selected2 = queue.select();
let selected3 = queue.select();

Implementations§

source§

impl<T: PartialEq> WrrQueue<T>

source

pub fn new() -> Self

source§

impl<T: PartialEq> WrrQueue<T>

source

pub async fn insert(&mut self, instance: impl Into<Instance<T>>) -> bool

insert a new instance, and re-calculate request queue

source

pub async fn insert_many<U>(&mut self, instance_list: impl Into<Vec<U>>) -> bool
where T: PartialEq, U: Into<Instance<T>>,

insert a new instance vec, and re-calculate request queue recommended when have multiple instance to be inserted

source

pub async fn select(&mut self) -> Option<&Instance<T>>

return the selected instance, None if instance_list is empty NOTE: select operation used only atomic operation, and can be paralleled

Trait Implementations§

source§

impl<T: PartialEq> Default for WrrQueue<T>

source§

fn default() -> Self

create a default WRR Queue, with no data

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for WrrQueue<T>

§

impl<T> Send for WrrQueue<T>
where T: Send,

§

impl<T> Sync for WrrQueue<T>
where T: Sync,

§

impl<T> Unpin for WrrQueue<T>
where T: Unpin,

§

impl<T> UnwindSafe for WrrQueue<T>
where T: UnwindSafe,

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>,

§

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>,

§

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.