use reqwest::header::HeaderMap;
use std::sync::{Mutex, MutexGuard, PoisonError};
use std::time::Duration;
use tokio::time::Instant;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RateLimit {
pub limit: u32,
pub interval: Duration,
}
impl RateLimit {
pub const DOCUMENTED: Self = Self {
limit: 180,
interval: Duration::from_secs(60),
};
pub fn spacing(&self) -> Duration {
self.interval / self.limit.max(1)
}
}
impl Default for RateLimit {
fn default() -> Self {
Self::DOCUMENTED
}
}
pub(crate) fn retry_after(headers: &HeaderMap) -> Option<Duration> {
let seconds = headers
.get("retry-after")?
.to_str()
.ok()?
.trim()
.parse()
.ok()?;
Some(Duration::from_secs(seconds))
}
#[derive(Debug)]
pub(crate) struct Limiter {
state: Mutex<State>,
}
#[derive(Debug)]
struct State {
rate: RateLimit,
next_slot: Option<Instant>,
}
impl Limiter {
pub(crate) fn new(rate: RateLimit) -> Self {
Self {
state: Mutex::new(State {
rate,
next_slot: None,
}),
}
}
pub(crate) async fn acquire(&self) {
let slot = {
let mut state = self.lock();
let now = Instant::now();
let slot = state.next_slot.map_or(now, |slot| slot.max(now));
state.next_slot = Some(slot + state.rate.spacing());
slot
};
tokio::time::sleep_until(slot).await;
}
pub(crate) fn back_off(&self, delay: Duration) {
let mut state = self.lock();
let resume = Instant::now() + delay;
state.next_slot = Some(state.next_slot.map_or(resume, |slot| slot.max(resume)));
}
pub(crate) fn rate(&self) -> RateLimit {
self.lock().rate
}
fn lock(&self) -> MutexGuard<'_, State> {
self.state.lock().unwrap_or_else(PoisonError::into_inner)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_documented_rate_spaces_requests_a_third_of_a_second_apart() {
assert_eq!(
RateLimit::DOCUMENTED.spacing(),
Duration::from_secs(60) / 180
);
assert!(RateLimit::DOCUMENTED.spacing() >= Duration::from_millis(333));
assert!(RateLimit::DOCUMENTED.spacing() < Duration::from_millis(334));
}
#[test]
fn a_rate_of_zero_does_not_divide_by_zero() {
let rate = RateLimit {
limit: 0,
interval: Duration::from_secs(1),
};
assert_eq!(rate.spacing(), Duration::from_secs(1));
}
#[tokio::test(start_paused = true)]
async fn requests_are_spaced_by_the_rate() {
let limiter = Limiter::new(RateLimit {
limit: 2,
interval: Duration::from_secs(1),
});
let start = Instant::now();
limiter.acquire().await;
limiter.acquire().await;
limiter.acquire().await;
assert_eq!(start.elapsed(), Duration::from_secs(1));
}
#[tokio::test(start_paused = true)]
async fn backing_off_holds_everything_back() {
let limiter = Limiter::new(RateLimit::DOCUMENTED);
let start = Instant::now();
limiter.back_off(Duration::from_secs(5));
limiter.acquire().await;
assert_eq!(start.elapsed(), Duration::from_secs(5));
}
}