Skip to content

Commit c3d9d66

Browse files
committed
rate limit via env var
1 parent 0d82bdd commit c3d9d66

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

catalyst-toolbox/src/http/mod.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,29 @@ use std::marker::PhantomData;
22

33
use ::reqwest::{blocking::Response, StatusCode};
44
use color_eyre::eyre::Result;
5+
use log::warn;
56
use serde::Deserialize;
67

78
use self::{rate_limit::RateLimitClient, reqwest::ReqwestClient};
89

910
mod rate_limit;
1011
mod reqwest;
1112

13+
const RATE_LIMIT_ENV_VAR: &str = "CATALYST_RATE_LIMIT_MS";
14+
1215
pub fn default_http_client(api_key: &str) -> impl HttpClient {
13-
RateLimitClient::new(ReqwestClient::new(api_key), 10_000)
16+
let rate_limit = match std::env::var(RATE_LIMIT_ENV_VAR).map(|s| s.parse::<u64>()) {
17+
Ok(Ok(rate_limit)) => rate_limit,
18+
Ok(Err(_)) => {
19+
warn!(
20+
"{} could not be parsed as a u64, defaulting to no rate-limiting",
21+
RATE_LIMIT_ENV_VAR
22+
);
23+
0
24+
}
25+
_ => 0,
26+
};
27+
RateLimitClient::new(ReqwestClient::new(api_key), rate_limit)
1428
}
1529

1630
#[cfg(test)]
@@ -21,7 +35,7 @@ fn test_default_client_send_sync() {
2135
}
2236

2337
/// Types which can make HTTP requests
24-
pub trait HttpClient: Send + Sync + 'static {
38+
pub trait HttpClient: Send + Sync + 'static {
2539
fn get<T>(&self, path: &str) -> Result<HttpResponse<T>>
2640
where
2741
T: for<'a> Deserialize<'a>;

catalyst-toolbox/src/http/rate_limit.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ use super::{HttpClient, HttpResponse};
1414

1515
pub struct RateLimitClient<T: HttpClient> {
1616
inner: T,
17-
limiter: RateLimiter<NotKeyed, InMemoryState, DefaultClock>,
17+
limiter: Option<RateLimiter<NotKeyed, InMemoryState, DefaultClock>>,
1818
}
1919

2020
impl<T: HttpClient> RateLimitClient<T> {
2121
pub fn new(inner: T, request_interval_ms: u64) -> Self {
22-
let quota = Quota::with_period(Duration::from_millis(request_interval_ms)).unwrap();
23-
let limiter = RateLimiter::direct(quota);
22+
let limiter = if request_interval_ms == 0 {
23+
None
24+
} else {
25+
let quota = Quota::with_period(Duration::from_millis(request_interval_ms)).unwrap();
26+
Some(RateLimiter::direct(quota))
27+
};
2428
Self { inner, limiter }
2529
}
2630
}
@@ -30,10 +34,12 @@ impl<T: HttpClient> HttpClient for RateLimitClient<T> {
3034
where
3135
S: for<'a> Deserialize<'a>,
3236
{
33-
while let Err(e) = self.limiter.check() {
34-
let time = e.wait_time_from(Instant::now());
35-
debug!("waiting for {time:?}");
36-
std::thread::sleep(time);
37+
if let Some(limiter) = self.limiter {
38+
while let Err(e) = limiter.check() {
39+
let time = e.wait_time_from(Instant::now());
40+
debug!("waiting for rate limit: {time:?}");
41+
std::thread::sleep(time);
42+
}
3743
}
3844
self.inner.get(path)
3945
}

0 commit comments

Comments
 (0)