Skip to main content

AsyncLoadBalancer

Struct AsyncLoadBalancer 

Source
pub struct AsyncLoadBalancer<I: Send + Clone + 'static, O: Send + Clone + 'static> {
    pub output_rx: Receiver<O>,
    pub input_tx: Sender<I>,
    pub handles: Vec<JoinHandle<()>>,
}
Expand description

An asynchronous load balancer for distributing tasks across multiple workers, using tokio! Please remember that outputs might not be 1/1 timewise with inputs. Inputs ordered A,B,C could come out ordered A,C,B!

§Example:

let mut lb: LoadBalancer<i32, i32> = LoadBalancer::new(100, 4, |x| {Box::pin(async move {x * 2})});
for i in 0..100 {
   lb.input_tx.send(i).await.unwrap();
}
for i in 0..100 {
   let result = lb.recv().await.unwrap();
   assert_eq!(result % 2, 0);
   println!("Received: {}", result);
}

Fields§

§output_rx: Receiver<O>§input_tx: Sender<I>§handles: Vec<JoinHandle<()>>

Implementations§

Source§

impl<I, O> AsyncLoadBalancer<I, O>
where I: Send + Clone + 'static, O: Send + Clone + 'static,

Source

pub fn new<F>(channel_limits: usize, worker_count: usize, task: F) -> Self
where F: Fn(I) -> Pin<Box<dyn Future<Output = O> + Send>> + Send + Sync + 'static,

Source

pub async fn recv(&mut self) -> Option<O>

Trait Implementations§

Source§

impl<I, O> Drop for AsyncLoadBalancer<I, O>
where I: Send + Clone + 'static, O: Send + Clone + 'static,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<I, O> Freeze for AsyncLoadBalancer<I, O>

§

impl<I, O> RefUnwindSafe for AsyncLoadBalancer<I, O>

§

impl<I, O> Send for AsyncLoadBalancer<I, O>

§

impl<I, O> Sync for AsyncLoadBalancer<I, O>

§

impl<I, O> Unpin for AsyncLoadBalancer<I, O>

§

impl<I, O> UnsafeUnpin for AsyncLoadBalancer<I, O>

§

impl<I, O> UnwindSafe for AsyncLoadBalancer<I, O>

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.