Skip to content

Instantly share code, notes, and snippets.

View gterzian's full-sized avatar

Gregory Terzian gterzian

View GitHub Profile
------------------------ MODULE FailingSharedStorage ------------------------
EXTENDS Naturals, Sequences
VARIABLES incoming, outgoing, statuses, shared_storage
CONSTANT QLen, Id
ASSUME (QLen \in Nat) /\ (QLen > 0)
--------------------------------------------------------------
StoredObject == {"Vacant", "Occupied"}
Statuses == {"Vacant", "Waiting", "Processed"}
Init == /\ shared_storage = [p \in Id |-> "Vacant" ]
--------------------------- MODULE SharedStorage ---------------------------
EXTENDS Naturals, Sequences
VARIABLES incoming, outgoing, statuses, shared_storage
CONSTANT QLen, Id
ASSUME (QLen \in Nat) /\ (QLen > 0)
--------------------------------------------------------------
StoredObject == {"Vacant", "Occupied"}
Statuses == {"Vacant", "Waiting", "Processed"}
Init == /\ shared_storage = [p \in Id |-> "Vacant" ]
#[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;
// 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;
}
#[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.
// 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() {