Skip to content

Instantly share code, notes, and snippets.

View jayhuang75's full-sized avatar

jayhuang75 jayhuang75

View GitHub Profile
@jayhuang75
jayhuang75 / rust-velocity-limit-main.rs
Last active April 29, 2022 02:36
rust-velocity-limit-main
fn main() {
let start = Instant::now();
let read_path = "./path/input.txt";
let write_path = "./path/output.txt";
let limit = Limit{
daily_max_amount: 5000.00,
weekly_max_amount: 20000.00,
daily_max_load: 5,
@jayhuang75
jayhuang75 / rust-velocity-limit-worker.rs
Last active April 28, 2022 16:28
rust-velocity-limit-worker
#[derive(Debug)]
pub struct Limit {
pub daily_max_amount: f64,
pub weekly_max_amount: f64,
pub daily_max_load: i32,
}
pub struct Worker {
velocity_limit: Limit,
transactions: Vec<Transaction>,
@jayhuang75
jayhuang75 / rust-velocity-limit-impl.rs
Created April 27, 2022 16:37
rust-velocity-limit-impl
impl Transaction {
pub fn is_duplicate(&self) -> bool {
let unique_key : &str = &format!("{}{}", self.id, self.customer_id);
let store = TRANSACTION_ID_COUNT.lock().unwrap();
match store.get_mut(unique_key) {
Some(_) => {
return true
}
@jayhuang75
jayhuang75 / rust-velocity-limit-tracking-struct.rs
Last active April 27, 2022 16:11
rust-velocity-limit-tracking-struct
lazy_static! {
pub static ref DAILY_TRANSACTIONS: Mutex<DashMap<String, i32>> = Mutex::new(DashMap::new());
pub static ref WEEKLY_TRANSACTIONS: Mutex<DashMap<String, f64>> = Mutex::new(DashMap::new());
pub static ref TRANSACTION_ID_COUNT: Mutex<DashMap<String, i32>> = Mutex::new(DashMap::new());
}
#[derive(Debug, Serialize)]
pub struct Output {
pub id: String,
pub customer_id: String,
@jayhuang75
jayhuang75 / rust-velocity-limit-transaction-struct.rs
Created April 27, 2022 15:01
rust-velocity-limit-transaction-struct
#[derive(Debug, Deserialize)]
pub struct Transaction {
pub id: String,
pub customer_id: String,
pub load_amount: String,
pub time: String
}
@jayhuang75
jayhuang75 / token_bucket_unit_test.rs
Last active March 30, 2022 04:17
token_bucket_unit_test
#[test]
fn test_token_bucket() {
let mut cinema = TokenBucket::new(2, 10);
let mut number_of_showtime:i64 = 0;
let mut random_volume_of_people = rand::thread_rng();
while number_of_showtime < 10 {
thread::sleep(time::Duration::from_secs_f64(2.0));
cinema.handle(random_volume_of_people.gen_range(1..10));
number_of_showtime += 1;
@jayhuang75
jayhuang75 / token_bucket_handle.rs
Last active March 30, 2022 03:48
token_bucket_handle
pub fn handle(&mut self, tokens: i64) {
self.update();
if self.current_tokens >= tokens {
self.current_tokens = self.current_tokens - tokens;
self.forward(tokens);
} else {
self.queue(tokens);
// for demo
// after some time period
@jayhuang75
jayhuang75 / token_bucket_trait.rs
Last active March 30, 2022 15:12
token_bucket_trait
pub trait Actions {
fn forward(&self, people: i64);
fn queue(&self, people: i64);
}
impl Actions for TokenBucket {
fn forward(&self, people: i64) {
println!("-> forward : {:?} people", people);
}
fn queue(&self, people: i64){
@jayhuang75
jayhuang75 / token_bucket_update.rs
Last active March 30, 2022 03:37
token_bucket_update
fn update(&mut self) {
let current = Utc::now();
let diff = current.time() - self.last_refill_timestamp.time();
let tokens_added = diff.num_seconds() * self.rate / 1000000000;
self.current_tokens = cmp::min(self.current_tokens + tokens_added, self.max_tokens);
self.last_refill_timestamp = current;
}
@jayhuang75
jayhuang75 / new_token_bucket.rs
Created March 29, 2022 23:54
new_token_bucket
/// TokeBucket implementation
impl TokenBucket {
pub fn new(rate: i64, max_token: i64) -> Self {
TokenBucket {
rate: rate,
max_tokens: max_token,
current_tokens: max_token,
last_refill_timestamp: Utc::now(),
}
}