This file contains hidden or 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
| 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, |
This file contains hidden or 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(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>, |
This file contains hidden or 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
| 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 | |
| } |
This file contains hidden or 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
| 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, |
This file contains hidden or 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(Debug, Deserialize)] | |
| pub struct Transaction { | |
| pub id: String, | |
| pub customer_id: String, | |
| pub load_amount: String, | |
| pub time: String | |
| } |
This file contains hidden or 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 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; |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| 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){ |
This file contains hidden or 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
| 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; | |
| } |
This file contains hidden or 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
| /// 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(), | |
| } | |
| } |