-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d1718a
commit 9ac993e
Showing
5 changed files
with
165 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use std::collections::{HashMap, VecDeque}; | ||
use std::sync::Arc; | ||
use std::time::{Duration, Instant}; | ||
use tokio::sync::Mutex; | ||
|
||
const CACHE_DURATION: Duration = Duration::from_secs(86_400); // 1 day | ||
|
||
struct Payment { | ||
time: Instant, | ||
amount: u64, | ||
} | ||
|
||
struct PaymentTracker { | ||
payments: VecDeque<Payment>, | ||
} | ||
|
||
impl PaymentTracker { | ||
pub fn new() -> Self { | ||
PaymentTracker { | ||
payments: VecDeque::new(), | ||
} | ||
} | ||
|
||
pub fn add_payment(&mut self, amount: u64) { | ||
let now = Instant::now(); | ||
let payment = Payment { time: now, amount }; | ||
|
||
self.payments.push_back(payment); | ||
} | ||
|
||
fn clean_old_payments(&mut self) { | ||
let now = Instant::now(); | ||
while let Some(payment) = self.payments.front() { | ||
if now.duration_since(payment.time) < CACHE_DURATION { | ||
break; | ||
} | ||
|
||
self.payments.pop_front(); | ||
} | ||
} | ||
|
||
pub fn sum_payments(&mut self) -> u64 { | ||
self.clean_old_payments(); | ||
self.payments.iter().map(|p| p.amount).sum() | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct PaymentsByIp { | ||
trackers: Arc<Mutex<HashMap<String, PaymentTracker>>>, | ||
} | ||
|
||
impl PaymentsByIp { | ||
pub fn new() -> Self { | ||
PaymentsByIp { | ||
trackers: Arc::new(Mutex::new(HashMap::new())), | ||
} | ||
} | ||
|
||
// Add a payment to the tracker for the given ip | ||
pub async fn add_payment(&self, ip: &str, amount: u64) { | ||
let mut trackers = self.trackers.lock().await; | ||
let tracker = trackers | ||
.entry(ip.to_string()) | ||
.or_insert_with(PaymentTracker::new); | ||
tracker.add_payment(amount); | ||
} | ||
|
||
// Get the total amount of payments for the given ip | ||
pub async fn get_total_payments(&self, ip: &str) -> u64 { | ||
let mut trackers = self.trackers.lock().await; | ||
match trackers.get_mut(ip) { | ||
Some(tracker) => tracker.sum_payments(), | ||
None => 0, | ||
} | ||
} | ||
} |