Skip to content

Instantly share code, notes, and snippets.

View jayhuang75's full-sized avatar

jayhuang75 jayhuang75

View GitHub Profile
@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_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 / 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 / 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-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-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-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-bufwriter.rs
Created April 28, 2022 21:05
rust-velocity-limit-bufwriter
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();
}
@jayhuang75
jayhuang75 / rust-velocity-limit-file-example.rs
Created April 30, 2022 13:16
rust-velocity-limit-file-example
{"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"}
@jayhuang75
jayhuang75 / rust-velocity-limit-file-output.rs
Created April 30, 2022 13:17
rust-velocity-limit-file-output
{"id":"31808","customer_id":"511","accept":true}
{"id":"558","customer_id":"256","accept":false}