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
| #[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
| #[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
| 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
| 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
| #[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
| 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
| let f = File::create(path).expect("Unable to create file"); | |
| +let mut buf = BufWriter::new(f); | |
| for line in &self.output{ | |
| let serialized = serde_json::to_string(&line).unwrap(); | |
| writeln!(&mut buf, "{}", serialized).unwrap(); | |
| } |
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
| {"id":"1234","customer_id":"528","load_amount":"$3318.47","time":"2000-01-01T00:00:00Z"} | |
| {"id":"5678","customer_id":"154","load_amount":"$1413.18","time":"2000-01-01T01:01:22Z"} |
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
| {"id":"31808","customer_id":"511","accept":true} | |
| {"id":"558","customer_id":"256","accept":false} |