use std::time::Duration; use tokio::time::sleep; use tractor::prelude::*; pub struct Accumulator { sum: usize, } #[actor(derive_hooks)] impl Accumulator { fn add(&mut self, a: usize) { self.sum += a; } fn sub(&mut self, a: usize) { self.sum -= a; } } trait AccumulatorRefExt: Address { fn overloaded(&self) -> bool { self.len() > 1000 } } impl AccumulatorRefExt for Addr {} pub struct Feeder; #[async_actor(derive_hooks)] impl Feeder { async fn feed(&mut self, num: usize, addr: Addr) { for _i in 0..num { while addr.overloaded() { sleep(Duration::from_millis(1)).await; // println!("Overload"); } addr.add(1); } } } fn main() { ActorSystem::run_to_completion(|| { let accumulator = Accumulator { sum: 0 }.start(); let feeder = Feeder.start(); feeder.feed(1_000_000, accumulator); }); }