google_maps/request_rate/api_rate_limit.rs
1use crate::request_rate::api_rate::ApiRate; // use crate::request_rate
2
3impl ApiRate {
4 /// This method is not for public consumption. It is for internal use only.
5 ///
6 /// ## Description
7 ///
8 /// This method does the actual rate limiting for an API.
9 /// If the current rate exceeds the targeted rate,
10 /// this method will put the thread to sleep until it is ready for the next
11 /// request.
12 pub async fn limit(&self) {
13 if let Some(wait_pool) = &self.throttle_pool { wait_pool.queue().await }
14 /*
15 match self.current_rate.first_request {
16 // If this is the first request to the API, initialize the
17 // timer.
18 None => {
19 // For some reason this trace! macro can cause a stack
20 // overflow, so it has been commented out for now:
21 /* trace!("Rate limiting is enabled for the `{}` API. First request.", api.to_string()); */
22 self.current_rate.first_request = Some(Instant::now());
23 self.current_rate.request_count = 1;
24
25 //let throttle_rate = ThrottleRate::new(self.target_rate.requests as usize, self.target_rate.duration);
26 //self.throttle_pool = Some(ThrottlePool::new(throttle_rate));
27 } // case
28
29 // If this is not the first request - calculate the elapsed,
30 // time & current rate, compare against the target rate, and
31 // sleep if necessary:
32 Some(_first_request) => {
33 // Output logging information:
34 // For some reason these trace! macros can cause a
35 // stack overflow, so they have been commented out for
36 // now:
37 /* trace!(
38 "{} requests to the `{}` API this session. This API's session began {} ago.",
39 rate.current_rate.request_count,
40 api.to_string(),
41 duration_to_string(&first_request.elapsed())
42 ); */
43 /* trace!(
44 "Current rate: {}. Target rate: {}.",
45 rate.current_rate, rate.target_rate
46 ); */
47
48 // Calculate the current rate and target rate:
49 //let target_rate = rate.target_rate.requests as f64
50 // / rate.target_rate.duration.as_secs_f64();
51 //let current_rate = rate.current_rate.request_count as f64
52 // / first_request.elapsed().as_secs_f64();
53
54 // If the current rate exceeds the targeted rate, put
55 // the thread to sleep:
56 /*
57 let difference = current_rate - target_rate;
58 if difference > 0.0 {
59 let sleep_duration = std::time::Duration::from_secs(
60 ((1.0 / target_rate) + difference).round() as u64,
61 );
62 info!(
63 "Thread is sleeping for {}.",
64 duration_to_string(&sleep_duration)
65 );
66 std::thread::sleep(sleep_duration);
67 } // if
68
69 */
70 // wait for throttle
71 match &self.throttle_pool {
72 Some(wait_pool) => wait_pool.queue().await,
73 None => ()
74 }
75
76 // Increment the request counter:
77 self.current_rate.request_count += 1;
78 } // case
79 } // match
80 */
81 } // fn
82} // impl