use std::time::{Duration, SystemTime, UNIX_EPOCH};
use super::retry::{hinted_wait, seconds};
const NANOS: i128 = 1_000_000_000;
const REQUESTS: [[&str; 3]; 4] = [
[
"x-ratelimit-limit-requests",
"x-ratelimit-remaining-requests",
"x-ratelimit-reset-requests",
],
[
"anthropic-ratelimit-requests-limit",
"anthropic-ratelimit-requests-remaining",
"anthropic-ratelimit-requests-reset",
],
[
"x-ratelimit-limit",
"x-ratelimit-remaining",
"x-ratelimit-reset",
],
["ratelimit-limit", "ratelimit-remaining", "ratelimit-reset"],
];
const TOKENS: [[&str; 3]; 3] = [
[
"x-ratelimit-limit-tokens",
"x-ratelimit-remaining-tokens",
"x-ratelimit-reset-tokens",
],
[
"anthropic-ratelimit-tokens-limit",
"anthropic-ratelimit-tokens-remaining",
"anthropic-ratelimit-tokens-reset",
],
[
"anthropic-ratelimit-input-tokens-limit",
"anthropic-ratelimit-input-tokens-remaining",
"anthropic-ratelimit-input-tokens-reset",
],
];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Allowance {
pub limit: u64,
pub remaining: u64,
pub resets_in: Option<Duration>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct RateLimits {
pub requests: Option<Allowance>,
pub tokens: Option<Allowance>,
}
impl RateLimits {
pub fn from_headers(headers: &http::HeaderMap, now: SystemTime) -> Self {
Self {
requests: allowance(headers, &REQUESTS, now),
tokens: allowance(headers, &TOKENS, now),
}
}
}
fn allowance(headers: &http::HeaderMap, names: &[[&str; 3]], now: SystemTime) -> Option<Allowance> {
let text = |name: &str| headers.get(name)?.to_str().ok().map(str::trim);
names.iter().find_map(|[limit, remaining, reset]| {
Some(Allowance {
limit: leading_number(text(limit)?)?,
remaining: leading_number(text(remaining)?)?,
resets_in: text(reset).and_then(|reset| reset_in(reset, now)),
})
})
}
fn leading_number(text: &str) -> Option<u64> {
let end = text
.find(|c: char| !c.is_ascii_digit())
.unwrap_or(text.len());
text.get(..end)?.parse().ok()
}
fn reset_in(text: &str, now: SystemTime) -> Option<Duration> {
if plain_decimal(text).is_some() {
let number: f64 = text.parse().ok()?;
return if number > 1e12 {
unix_wait(text, 1_000_000, now)
} else if number > 1e9 {
unix_wait(text, NANOS, now)
} else {
seconds(number)
};
}
if let Some(wait) = hinted_wait(text) {
return Some(wait);
}
let at = match rfc3339(text) {
Some(at) => at,
None => since_epoch(httpdate::parse_http_date(text).ok()?)?,
};
until(at, now)
}
fn until(at: i128, now: SystemTime) -> Option<Duration> {
nanoseconds(at.checked_sub(since_epoch(now)?)?)
}
fn since_epoch(time: SystemTime) -> Option<i128> {
match time.duration_since(UNIX_EPOCH) {
Ok(after) => i128::try_from(after.as_nanos()).ok(),
Err(before) => i128::try_from(before.duration().as_nanos())
.ok()
.map(|n| -n),
}
}
pub(crate) fn rfc3339(text: &str) -> Option<i128> {
let (date, time) = text.split_once(['T', 't', ' '])?;
let mut date = date.split('-');
let year = digits(date.next()?, 4)?;
let month = digits(date.next()?, 2)?;
let day = digits(date.next()?, 2)?;
let (clock, offset) = match time.strip_suffix(['Z', 'z']) {
Some(clock) => (clock, 0),
None => {
let (clock, offset) = time.split_at_checked(time.rfind(['+', '-'])?)?;
let sign = if offset.starts_with('-') { -1 } else { 1 };
let (hours, minutes) = offset.get(1..)?.split_once(':')?;
let (hours, minutes) = (digits(hours, 2)?, digits(minutes, 2)?);
if hours > 23 || minutes > 59 {
return None;
}
(clock, sign * (hours * 3600 + minutes * 60))
}
};
let (clock, fraction) = clock.split_once('.').unwrap_or((clock, "0"));
if fraction.is_empty() || !fraction.bytes().all(|b| b.is_ascii_digit()) {
return None;
}
let fraction: i128 = format!("{:0<9}", fraction.get(..9).unwrap_or(fraction))
.parse()
.ok()?;
let mut clock = clock.split(':');
let hour = digits(clock.next()?, 2)?;
let minute = digits(clock.next()?, 2)?;
let second = digits(clock.next()?, 2)?;
let days = days_since_epoch(year, month, day)?;
let valid = date.next().is_none() && clock.next().is_none();
if !valid || hour > 23 || minute > 59 || second > 60 {
return None;
}
let seconds = days * 86_400 + hour * 3600 + minute * 60 + second - offset;
i128::from(seconds)
.checked_mul(NANOS)?
.checked_add(fraction)
}
fn unix_wait(text: &str, unit: i128, now: SystemTime) -> Option<Duration> {
let (whole, fraction, places) = plain_decimal(text)?;
let whole = whole.checked_mul(unit)?.checked_sub(since_epoch(now)?)?;
let scale = 10_i128.checked_pow(places)?;
let fraction = fraction.checked_mul(unit)?;
let left = whole.checked_add(fraction.div_euclid(scale))?;
let twice = fraction.rem_euclid(scale).checked_mul(2)?;
let up = twice > scale || (twice == scale && left.rem_euclid(2) == 1);
nanoseconds(left.checked_add(i128::from(up))?)
}
fn plain_decimal(text: &str) -> Option<(i128, i128, u32)> {
let (whole, fraction) = text.split_once('.').unwrap_or((text, ""));
let digits = |part: &str| part.bytes().all(|b| b.is_ascii_digit());
if whole.len() + fraction.len() == 0 || !digits(whole) || !digits(fraction) {
return None;
}
let fraction = fraction.get(..18).unwrap_or(fraction);
Some((
format!("0{whole}").parse().ok()?,
format!("0{fraction}").parse().ok()?,
u32::try_from(fraction.len()).ok()?,
))
}
fn nanoseconds(nanos: i128) -> Option<Duration> {
let nanos = nanos.max(0);
let secs = u64::try_from(nanos / NANOS).ok()?;
Some(Duration::new(secs, u32::try_from(nanos % NANOS).ok()?))
}
fn digits(text: &str, width: usize) -> Option<i64> {
let ascii = text.len() == width && text.bytes().all(|b| b.is_ascii_digit());
ascii.then(|| text.parse().ok())?
}
fn days_since_epoch(year: i64, month: i64, day: i64) -> Option<i64> {
let days = |year: i64, month: i64, day: i64| {
let year = if month <= 2 { year - 1 } else { year };
let era = year.div_euclid(400);
let of_era = year - era * 400;
let of_year = (153 * ((month + 9) % 12) + 2) / 5 + day - 1;
era * 146_097 + of_era * 365 + of_era / 4 - of_era / 100 + of_year - 719_468
};
let (next_year, next_month) = if month == 12 {
(year + 1, 1)
} else {
(year, month + 1)
};
let length = days(next_year, next_month, 1) - days(year, month, 1);
((1..=12).contains(&month) && (1..=length).contains(&day)).then(|| days(year, month, day))
}
#[cfg(test)]
mod tests;