move to a model of one thread per dispatcher, instead of one main thread

This commit is contained in:
Adrian Malacoda 2018-02-22 02:04:09 -06:00
parent fcc86a671e
commit 56ca5ae767

View File

@ -28,6 +28,7 @@ use std::sync::mpsc;
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
use transformable_channels::mpsc::TransformableSender; use transformable_channels::mpsc::TransformableSender;
use transformable_channels::mpsc::Receiver;
use multimap::MultiMap; use multimap::MultiMap;
mod helpers; mod helpers;
@ -71,18 +72,17 @@ impl Tenquestionmarks {
pub fn run (&self) { pub fn run (&self) {
crossbeam::scope(|scope| { crossbeam::scope(|scope| {
// Our main event channel. let mut dispatchers: BTreeMap<&str, Receiver<Envelope>> = BTreeMap::new();
// Modules push events to tenquestionmarks using this channel.
let (ref main_sender, ref main_receiver) = transformable_channels::mpsc::channel();
// Module threads. // Event loop threads.
// Each Module will produce events which tenquestionmarks will push // Event loop threads consume events passed in by dispatcher threads, and
// into all other Modules. // generate events through dispatcher threads.
// tenquestionmarks propagates all events to each Module through these
// channels.
let module_senders: BTreeMap<&str, Sender<Arc<Envelope>>> = self.modules.iter().map(|(key, module)| { let module_senders: BTreeMap<&str, Sender<Arc<Envelope>>> = self.modules.iter().map(|(key, module)| {
let from = key.clone(); let from = key.clone();
let main_sender_mapped = main_sender.map(move |envelope: Envelope| { let (dispatcher_sender, dispatcher_receiver) = transformable_channels::mpsc::channel();
dispatchers.insert(key, dispatcher_receiver);
let mapped_sender = dispatcher_sender.map(move |envelope: Envelope| {
Envelope { Envelope {
from: Some(from.clone()), from: Some(from.clone()),
event: envelope.event event: envelope.event
@ -90,43 +90,49 @@ impl Tenquestionmarks {
}); });
let (module_sender, module_receiver) = mpsc::channel(); let (module_sender, module_receiver) = mpsc::channel();
info!("Spawning thread for \"{}\"", key); info!("Spawning event loop thread for \"{}\"", key);
scope.spawn(move || { scope.spawn(move || {
module.run(Box::new(main_sender_mapped), module_receiver); module.run(Box::new(mapped_sender), module_receiver);
info!("Thread for \"{}\" is exiting", key); info!("Event loop thread for \"{}\" is exiting", key);
}); });
(&key[..], module_sender) (&key[..], module_sender)
}).collect(); }).collect();
// tenquestionmarks main event loop. // Dispatcher threads.
// tenquestionmarks receives events produced by Modules and pushes them // Dispatcher threads transmit events produced by parent modules to child modules.
// into all other Modules for (from, receiver) in dispatchers.into_iter() {
loop { if let Some(subscriptions) = self.subscriptions.get_vec(from) {
match main_receiver.recv() { let senders: BTreeMap<&str, Sender<Arc<Envelope>>> = module_senders.iter().filter(|&(key, _)| {
Ok(envelope) => { subscriptions.iter().any(|subscription| subscription.name == *key)
/* }).map(|(key, value)| {
* Check if the target module is a valid destination for the envelope. (*key, value.clone())
* We want to deliver this envelope if all of the following are true: }).collect();
* 1. The target module is not the originator of the envelope
* 2. The envelope's list of allowed recipients is empty, or specifically info!("Spawning dispatcher thread for \"{}\"", from);
* names the target module. scope.spawn(move || {
*/ loop {
let arc_envelope = Arc::new(envelope); match receiver.recv() {
if let Some(ref from) = arc_envelope.from { Ok(envelope) => {
if let Some(subscriptions) = self.subscriptions.get_vec(&**from) { let arc_envelope = Arc::new(envelope);
for subscription in subscriptions { for subscription in subscriptions {
if subscription.can_handle_event(&arc_envelope) { if subscription.can_handle_event(&arc_envelope) {
if let Some(sender) = module_senders.get(&*subscription.name) { if let Some(sender) = senders.get(&*subscription.name) {
if let Err(err) = sender.send(arc_envelope.clone()) { if let Err(err) = sender.send(arc_envelope.clone()) {
debug!("Failed to dispatch event to module \"{}\": {:?}", subscription.name, err); debug!("Failed to dispatch event to module \"{}\": {:?}", subscription.name, err);
}
} }
} }
} }
},
Err(err) => {
error!("Failed to receive event from module: \"{}\": {:?}", from, err);
break;
} }
} }
} }
},
Err(err) => { error!("Failed to receive event in main event loop: {:?}", err); } info!("Dispatcher thread for \"{}\" is exiting", from);
});
} }
} }
}); });