atomr_core/routing/
random.rs1use std::sync::atomic::{AtomicU64, Ordering};
4
5use crate::actor::ActorRef;
6
7pub struct RandomRouter<M: Send + Clone + 'static> {
8 routees: Vec<ActorRef<M>>,
9 seed: AtomicU64,
10}
11
12impl<M: Send + Clone + 'static> RandomRouter<M> {
13 pub fn new(routees: Vec<ActorRef<M>>) -> Self {
14 Self { routees, seed: AtomicU64::new(0xDEADBEEF) }
15 }
16
17 pub fn route(&self, msg: M) {
18 if self.routees.is_empty() {
19 return;
20 }
21 let s = self.seed.fetch_add(1, Ordering::Relaxed);
22 let idx = (splitmix64(s) as usize) % self.routees.len();
23 self.routees[idx].tell(msg);
24 }
25}
26
27fn splitmix64(mut x: u64) -> u64 {
28 x = x.wrapping_add(0x9E3779B97F4A7C15);
29 let mut z = x;
30 z = (z ^ (z >> 30)).wrapping_mul(0xBF58476D1CE4E5B9);
31 z = (z ^ (z >> 27)).wrapping_mul(0x94D049BB133111EB);
32 z ^ (z >> 31)
33}