1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
use crate::child::Message; use crate::messages::PoisonPill; use crossbeam_channel::{Receiver, Sender}; use ratelimit::Limiter; use std::any::Any; use std::time::Duration; #[derive(Clone)] pub struct BastionContext { pub bcast_tx: Option<Sender<Box<dyn Message>>>, pub bcast_rx: Option<Receiver<Box<dyn Message>>>, } impl BastionContext { fn dispatch_clock() -> Limiter { ratelimit::Builder::new() .capacity(1) .quantum(1) .interval(Duration::new(0, 100)) .build() } pub fn hook(self) { let mut dc = BastionContext::dispatch_clock(); dc.wait(); let rx = self.bcast_rx.clone().unwrap(); if let Ok(message) = rx.try_recv() { let msg: &dyn Any = message.as_any(); if msg.is::<PoisonPill>() { dc.wait(); panic!("PoisonPill"); } } } pub fn blocking_hook(self) { let mut dc = BastionContext::dispatch_clock(); dc.wait(); let rx = self.bcast_rx.clone().unwrap(); loop { if let Ok(message) = rx.try_recv() { let msg: &dyn Any = message.as_any(); if msg.is::<PoisonPill>() { dc.wait(); panic!("PoisonPill"); } } } } }