1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use crate::request_rate::{api::Api, duration_to_string::duration_to_string, RequestRate};
use futures::future;
use std::time::SystemTime;
impl RequestRate {
/// This method is not for public consumption. It is for internal use only.
///
/// ## Description
///
/// This method performs rate limiting, using the throttler under `rate_map`
/// specified by the list of apis, which was calculated using targeted
/// requests/duration rates during initialization. If the current rate
/// exceeds any of the targeted rate, this method will put the thread to
/// sleep until it is ready for the next request.
///
/// ## Arguments
///
/// * `apis` ‧ The APIs for which to observe the request rate limit.
pub async fn limit_apis(&self, apis: Vec<&Api>) {
let mut limit_futures = Vec::new();
for (key, val) in &self.rate_map {
if apis.contains(&key) {
limit_futures.push(val.limit());
}
}
let start = SystemTime::now();
future::join_all(limit_futures).await;
let wait_time = SystemTime::now().duration_since(start);
if let Ok(duration) = wait_time {
if duration.as_millis() > 10 {
tracing::debug!(
"Waited for {} under rate limiter.",
duration_to_string(&duration)
);
}
} else {
tracing::warn!("Clock went backwards!");
}
}
} // impl