Something went wrong. Try again.
contributor fork of @ptr.pet hydrant forked from ptr.pet/hydrant
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364use crate::config::RateTier;use parking_lot::Mutex;use scc::HashMap;use std::future::Future;use std::sync::Arc;use std::sync::atomic::{AtomicI64, AtomicUsize, Ordering};use std::time::{Duration, Instant};use tokio::sync::{Notify, Semaphore, SemaphorePermit};use url::Url;
/// max concurrent in-flight requests per PDS before we start queuing/// ref pds allows 10 requests per second... so 10 should be fineconst PER_PDS_CONCURRENCY: usize = 10;
// per second, hour and dayconst DURATIONS: [Duration; 3] = [ Duration::from_secs(1), Duration::from_secs(3600), Duration::from_secs(86400),];
#[derive(Clone)]pub struct Throttler { states: Arc<HashMap<Url, Arc<State>>>,}
#[derive(Debug, Clone, Default)]pub struct ThrottleSnapshot { pub consecutive_failures: usize, pub throttled_until: i64, pub last_failure: Option<FailureSnapshot>,}
impl ThrottleSnapshot { pub fn is_failing(&self) -> bool { self.consecutive_failures != 0 || self.throttled_until != 0 }
pub fn is_throttled(&self, now: i64) -> bool { self.throttled_until != 0 && now < self.throttled_until }
pub fn retry_in_secs(&self, now: i64) -> Option<u64> { self.is_throttled(now) .then_some((self.throttled_until - now) as u64) }}
#[derive(Debug, Clone)]pub struct FailureSnapshot { pub at: i64, pub kind: String, pub detail: String,}
impl Throttler { pub fn new() -> Self { Self { states: Arc::new(HashMap::new()), } }
pub async fn get_handle(&self, url: &Url) -> ThrottleHandle { let state = self .states .entry_async(url.clone()) .await .or_insert_with(|| Arc::new(State::new())) .get() .clone();
ThrottleHandle { state } }
/// drop entries with no active throttle and no consecutive failures. pub async fn evict_clean(&self) { self.states .retain_async(|_, v| { v.throttled_until.load(Ordering::Acquire) != 0 || v.consecutive_failures.load(Ordering::Acquire) != 0 }) .await; }
pub fn snapshot(&self, url: &Url) -> ThrottleSnapshot { self.states .read_sync(url, |_, state| ThrottleSnapshot { consecutive_failures: state.consecutive_failures.load(Ordering::Acquire), throttled_until: state.throttled_until.load(Ordering::Acquire), last_failure: state.last_failure.lock().clone(), }) .unwrap_or_default() }}
struct State { throttled_until: AtomicI64, consecutive_failures: AtomicUsize, consecutive_timeouts: AtomicUsize, last_failure: Mutex<Option<FailureSnapshot>>, /// only fires on hard failures (timeout, TLS, bad gateway, etc). /// ratelimits do NOT fire this — they just store `throttled_until` and /// let tasks exit naturally, deferring to the background retry loop. failure_notify: Notify, semaphore: Semaphore, rate_limiter: RateLimiter,}
impl State { fn new() -> Self { Self { throttled_until: AtomicI64::new(0), consecutive_failures: AtomicUsize::new(0), consecutive_timeouts: AtomicUsize::new(0), last_failure: Mutex::new(None), failure_notify: Notify::new(), semaphore: Semaphore::new(PER_PDS_CONCURRENCY), rate_limiter: RateLimiter::new(), } }}
pub struct ThrottleHandle { state: Arc<State>,}
impl ThrottleHandle { pub fn is_throttled(&self) -> bool { let until = self.state.throttled_until.load(Ordering::Acquire); until != 0 && chrono::Utc::now().timestamp() < until }
/// the unix timestamp at which this throttle expires (0 if not throttled). pub fn throttled_until(&self) -> i64 { self.state.throttled_until.load(Ordering::Acquire) }
pub fn record_success(&self) { self.state.consecutive_failures.store(0, Ordering::Release); self.state.consecutive_timeouts.store(0, Ordering::Release); self.state.throttled_until.store(0, Ordering::Release); *self.state.last_failure.lock() = None; }
/// called on a 429 response. `retry_after_secs` comes from the `Retry-After` /// header if present; falls back to 60s. uses `fetch_max` so concurrent callers /// don't race each other back to a shorter window. pub fn record_ratelimit(&self, retry_after_secs: Option<u64>) { let secs = retry_after_secs.unwrap_or(60) as i64; let until = chrono::Utc::now().timestamp() + secs; self.state .throttled_until .fetch_max(until, Ordering::AcqRel); }
/// called on hard failures (timeout, TLS error, bad gateway, etc). /// always increments `consecutive_failures`. only sets a new `throttled_until` /// (and notifies waiters) if not already throttled. pub fn record_failure(&self) -> Option<u64> { self.record_failure_inner() }
pub fn record_failure_detail( &self, kind: impl Into<String>, detail: impl Into<String>, ) -> Option<u64> { *self.state.last_failure.lock() = Some(FailureSnapshot { at: chrono::Utc::now().timestamp(), kind: kind.into(), detail: detail.into(), }); self.record_failure_inner() }
fn record_failure_inner(&self) -> Option<u64> { let failures = self .state .consecutive_failures .fetch_add(1, Ordering::AcqRel) + 1;
if self.is_throttled() { return None; }
let base_secs = 15u64; let exponent = (failures as u32).saturating_sub(1); let secs = (base_secs * 2u64.pow(exponent.min(10))).min(300); #[cfg(debug_assertions)] let secs = secs.min(1);
let until = chrono::Utc::now().timestamp() + secs as i64;
self.state.throttled_until.store(until, Ordering::Release); self.state.failure_notify.notify_waiters();
Some(secs) }
/// returns current timeout duration — 3s, 6s, or 12s depending on prior timeouts. pub fn timeout(&self) -> Duration { let n = self.state.consecutive_timeouts.load(Ordering::Acquire); Duration::from_secs(3 * 2u64.pow(n.min(2) as u32)) }
pub fn consecutive_failures(&self) -> usize { self.state.consecutive_failures.load(Ordering::Acquire) }
/// returns whether the timeout attempts are exhausted pub fn record_timeout(&self) -> bool { let timeouts = self .state .consecutive_timeouts .fetch_add(1, Ordering::AcqRel) + 1; timeouts > 2 }
/// acquire a concurrency slot for this PDS. hold the returned permit /// for the duration of the request. pub async fn acquire(&self) -> SemaphorePermit<'_> { self.state .semaphore .acquire() .await .expect("throttle semaphore unexpectedly closed") }
/// resolves when this PDS gets a hard failure notification. pub async fn wait_for_failure(&self) { loop { let notified = self.state.failure_notify.notified(); if self.is_throttled() { return; } notified.await; } }
/// waits until the rate tier's limits allow more events for this PDS. /// sleeps precisely until the most restrictive window opens rather than polling. pub async fn wait_for_allow(&self, num_accounts: u64, tier: &RateTier) { let limits = limits_for(num_accounts, tier); while let Some(wait) = self.state.rate_limiter.try_acquire(limits) { tokio::time::sleep(wait).await; } }}
fn limits_for(num_accounts: u64, tier: &RateTier) -> [u64; 3] { let per_sec = tier .per_second_base .max((num_accounts as f64 * tier.per_second_account_mul) as u64); [per_sec, tier.per_hour, tier.per_day]}
struct WindowState { count: u64, prev_count: u64, window_start: Instant,}
impl WindowState { fn new() -> Self { Self { count: 0, prev_count: 0, window_start: Instant::now(), } }
fn rotate(&mut self, dur: Duration) { let elapsed = self.window_start.elapsed(); if elapsed >= dur { let n = (elapsed.as_nanos() / dur.as_nanos()).max(1) as u32; self.prev_count = if n == 1 { self.count } else { 0 }; self.count = 0; self.window_start += dur * n; } }
/// returns how long to sleep before this window would allow one more event. /// Duration::ZERO means allow now. fn wait_needed(&self, dur: Duration, limit: u64) -> Duration { let elapsed = self.window_start.elapsed(); let remaining = dur.saturating_sub(elapsed); let weight = remaining.as_secs_f64() / dur.as_secs_f64(); let effective = self.count as f64 + self.prev_count as f64 * weight;
if effective < limit as f64 { return Duration::ZERO; }
if self.prev_count == 0 || self.count as f64 >= limit as f64 { // must wait for a full window rotation remaining + Duration::from_millis(1) } else { let secs = remaining.as_secs_f64() - dur.as_secs_f64() * (limit as f64 - self.count as f64) / self.prev_count as f64; Duration::from_secs_f64(secs.max(0.0)) + Duration::from_micros(500) } }}
struct RateLimiter { // parking_lot::Mutex — uncontended path never touches the kernel windows: Mutex<[WindowState; 3]>,}
impl RateLimiter { fn new() -> Self { Self { windows: Mutex::new([WindowState::new(), WindowState::new(), WindowState::new()]), } }
/// returns None if the slot was acquired, or Some(sleep_for) if limited. fn try_acquire(&self, limits: [u64; 3]) -> Option<Duration> { let mut windows = self.windows.lock();
windows .iter_mut() .zip(DURATIONS) .for_each(|(w, d)| w.rotate(d));
let max_wait = windows .iter() .zip(DURATIONS) .zip(limits) .map(|((w, dur), limit)| w.wait_needed(dur, limit)) .max() .unwrap_or(Duration::ZERO);
if max_wait.is_zero() { windows.iter_mut().for_each(|w| w.count += 1); None } else { Some(max_wait) } }}
/// adds a method for racing the future against a hard-failure notification.#[allow(async_fn_in_trait)]pub trait OrFailure<T, E>: Future<Output = Result<T, E>> { async fn or_failure( self, handle: &ThrottleHandle, on_throttle: impl FnOnce() -> E, ) -> Result<T, E> where Self: Sized, { tokio::select! { res = self => res, _ = handle.wait_for_failure() => Err(on_throttle()), } }}
impl<T, E, F: Future<Output = Result<T, E>>> OrFailure<T, E> for F {}