Skip to content

Instantly share code, notes, and snippets.

View gterzian's full-sized avatar

Gregory Terzian gterzian

View GitHub Profile
// 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());
// 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);
}
/// 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() {
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)
/// 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() {
// 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;
}
// 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.
// 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) {
#[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,
}
#[test]
fn fifth() {
enum WorkMsg {
Work(u8),
Exit,
}
#[derive(Debug, Eq, PartialEq)]
enum WorkPerformed {
FromCache,