This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A map of orders pending payment, owned by the "order" service. | |
let mut pending_payment = HashMap::new(); | |
// Spawn the "order" service. | |
let _ = thread::spawn(move || loop { | |
select! { | |
recv(order_request_receiver) -> msg => { | |
match msg { | |
Ok(OrderRequest::NewOrder(customer_id)) => { | |
let order_id = OrderId(Uuid::new_v4()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A map of keeping count of pending order per customer, | |
// owned by the "basket" service. | |
let mut pending_orders = HashMap::new(); | |
for _ in 0..4 { | |
let customer_id = CustomerId(Uuid::new_v4()); | |
match pending_orders.entry(customer_id) { | |
Entry::Vacant(entry) => { | |
entry.insert(1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Check the current size of the buffer, and modulate the source accordingly. | |
fn check_buffer_size( | |
buffer: &mut VecDeque<u8>, | |
sender: &Sender<RegulateSourceMsg>, | |
tick_adjusted: &mut bool, | |
) { | |
if !*tick_adjusted { | |
return; | |
} | |
match buffer.len() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
recv(from_processor_receiver) -> msg => { | |
let _ = work_sender.send(SourceMsg::TickAdjusted); | |
match msg { | |
Ok(RegulateSourceMsg::SlowDown) => { | |
current_ticker_duration = match current_ticker_duration { | |
Some(tick) => { | |
if tick > 100 { | |
Some(100) | |
} else { | |
Some(tick * 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Check the current size of the buffer, and modulate the source accordingly. | |
fn check_buffer_size( | |
buffer: &mut VecDeque<u8>, | |
sender: &Sender<RegulateSourceMsg>, | |
tick_adjusted: &mut bool, | |
) { | |
if !*tick_adjusted { | |
return; | |
} | |
match buffer.len() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The main test thread, doubling as the "consumer" component. | |
// A counter of work received. | |
let mut counter = 0; | |
loop { | |
match result_receiver.recv() { | |
Ok(ProcessorMsg::Result(num)) => { | |
counter += 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Spawn a "processor" component in parallel. | |
let _ = thread::spawn(move || { | |
// The processor has two worker threads at it's disposal. | |
let pool = rayon::ThreadPoolBuilder::new() | |
.num_threads(2) | |
.build() | |
.unwrap(); | |
// Workers in the pool communicate that they've finished a unit of work, | |
// back to the main-thread of the "processor", via this channel. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Spawn a "source" component in parallel. | |
let _ = thread::spawn(move || { | |
// A counter of work produced. | |
let mut counter: u8 = 0; | |
let ticker = tick(Duration::from_millis(1)); | |
loop { | |
// Block on a tick. | |
ticker.recv().unwrap(); | |
match counter.checked_add(1) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[test] | |
fn first() { | |
/// The messages sent from the "source", | |
/// to the "proccessor". | |
enum SourceMsg { | |
/// Work to be processed. | |
Work(u8), | |
/// The source has defenitively stopped producing. | |
Stopped, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[test] | |
fn fifth() { | |
enum WorkMsg { | |
Work(u8), | |
Exit, | |
} | |
#[derive(Debug, Eq, PartialEq)] | |
enum WorkPerformed { | |
FromCache, |