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
#[macro_use] | |
extern crate crossbeam_channel; | |
use crossbeam_channel::unbounded; | |
use std::collections::hash_map::Entry; | |
use std::collections::{HashMap, HashSet}; | |
use std::thread; | |
use std::time::Duration; | |
use uuid::Uuid; |
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 the "payment" service. | |
let _ = thread::spawn(move || loop { | |
match payment_request_receiver.recv() { | |
Ok(PaymentRequest::NewOrder(order_id)) => { | |
// Process the payment for a new order. | |
let _ = payment_result_sender.send(PaymentResult(order_id, true)); | |
} | |
Ok(PaymentRequest::ShutDown) => { | |
break; | |
} |
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
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] | |
struct CustomerId(Uuid); | |
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] | |
struct OrderId(Uuid); | |
/// Messages sent from the "basket" service, | |
/// to the "order" service. | |
enum OrderRequest { | |
/// A customer is attempting to make a new order. |
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() { |