simplify loops, less work done in dispatcher thread
This commit is contained in:
parent
56ca5ae767
commit
055a323d64
21
src/lib.rs
21
src/lib.rs
@ -75,9 +75,9 @@ impl Tenquestionmarks {
|
||||
let mut dispatchers: BTreeMap<&str, Receiver<Envelope>> = BTreeMap::new();
|
||||
|
||||
// Event loop threads.
|
||||
// Event loop threads consume events passed in by dispatcher threads, and
|
||||
// generate events through dispatcher threads.
|
||||
let module_senders: BTreeMap<&str, Sender<Arc<Envelope>>> = self.modules.iter().map(|(key, module)| {
|
||||
// Event loop threads consume events passed in by other modules' dispatcher threads,
|
||||
// and produce events through their own dispatcher threads.
|
||||
let senders: BTreeMap<&str, Sender<Arc<Envelope>>> = self.modules.iter().map(|(key, module)| {
|
||||
let from = key.clone();
|
||||
let (dispatcher_sender, dispatcher_receiver) = transformable_channels::mpsc::channel();
|
||||
dispatchers.insert(key, dispatcher_receiver);
|
||||
@ -102,10 +102,9 @@ impl Tenquestionmarks {
|
||||
// Dispatcher threads transmit events produced by parent modules to child modules.
|
||||
for (from, receiver) in dispatchers.into_iter() {
|
||||
if let Some(subscriptions) = self.subscriptions.get_vec(from) {
|
||||
let senders: BTreeMap<&str, Sender<Arc<Envelope>>> = module_senders.iter().filter(|&(key, _)| {
|
||||
subscriptions.iter().any(|subscription| subscription.name == *key)
|
||||
}).map(|(key, value)| {
|
||||
(*key, value.clone())
|
||||
let dispatcher_senders: BTreeMap<&str, (&Subscription, Sender<Arc<Envelope>>)> = senders.iter().filter_map(|(key, value)| {
|
||||
subscriptions.iter().find(|subscription| subscription.name == *key)
|
||||
.map(|subscription| (*key, (subscription, value.clone())))
|
||||
}).collect();
|
||||
|
||||
info!("Spawning dispatcher thread for \"{}\"", from);
|
||||
@ -114,12 +113,10 @@ impl Tenquestionmarks {
|
||||
match receiver.recv() {
|
||||
Ok(envelope) => {
|
||||
let arc_envelope = Arc::new(envelope);
|
||||
for subscription in subscriptions {
|
||||
for (child_name, &(subscription, ref sender)) in dispatcher_senders.iter() {
|
||||
if subscription.can_handle_event(&arc_envelope) {
|
||||
if let Some(sender) = senders.get(&*subscription.name) {
|
||||
if let Err(err) = sender.send(arc_envelope.clone()) {
|
||||
debug!("Failed to dispatch event to module \"{}\": {:?}", subscription.name, err);
|
||||
}
|
||||
if let Err(err) = sender.send(arc_envelope.clone()) {
|
||||
debug!("Failed to dispatch event to module \"{}\": {:?}", child_name, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user