pub mod error;
mod bucket;
mod headers;
pub use self::{
error::{RatelimitError, RatelimitResult},
headers::RatelimitHeaders,
};
use crate::routing::Path;
use bucket::{Bucket, BucketQueueTask, TimeRemaining};
use std::{
collections::hash_map::{Entry, HashMap},
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
time::Duration,
};
use tokio::sync::{
oneshot::{self, Receiver, Sender},
Mutex,
};
#[derive(Debug, Default)]
struct GlobalLockPair(Mutex<()>, AtomicBool);
impl GlobalLockPair {
pub fn lock(&self) {
self.1.store(true, Ordering::Release);
}
pub fn unlock(&self) {
self.1.store(false, Ordering::Release);
}
pub fn is_locked(&self) -> bool {
self.1.load(Ordering::Relaxed)
}
}
#[derive(Clone, Debug, Default)]
pub struct Ratelimiter {
buckets: Arc<Mutex<HashMap<Path, Arc<Bucket>>>>,
global: Arc<GlobalLockPair>,
}
impl Ratelimiter {
pub fn new() -> Self {
Self::default()
}
pub async fn get(&self, path: Path) -> Receiver<Sender<Option<RatelimitHeaders>>> {
#[cfg(feature = "tracing")]
tracing::debug!("getting bucket for path: {:?}", path);
let (tx, rx) = oneshot::channel();
let (bucket, fresh) = self.entry(path.clone(), tx).await;
if fresh {
tokio::spawn(
BucketQueueTask::new(
bucket,
Arc::clone(&self.buckets),
Arc::clone(&self.global),
path,
)
.run(),
);
}
rx
}
pub async fn time_until_available(&self, path: &Path) -> Option<Duration> {
let buckets = self.buckets.lock().await;
match buckets.get(path)?.time_remaining().await {
TimeRemaining::Finished | TimeRemaining::NotStarted => None,
TimeRemaining::Some(duration) => Some(duration),
}
}
async fn entry(
&self,
path: Path,
tx: Sender<Sender<Option<RatelimitHeaders>>>,
) -> (Arc<Bucket>, bool) {
let mut buckets = self.buckets.lock().await;
match buckets.entry(path.clone()) {
Entry::Occupied(bucket) => {
#[cfg(feature = "tracing")]
tracing::debug!("got existing bucket: {:?}", path);
let bucket = bucket.into_mut();
bucket.queue.push(tx);
#[cfg(feature = "tracing")]
tracing::debug!("added request into bucket queue: {:?}", path);
(Arc::clone(&bucket), false)
}
Entry::Vacant(entry) => {
#[cfg(feature = "tracing")]
tracing::debug!("making new bucket for path: {:?}", path);
let bucket = Bucket::new(path.clone());
bucket.queue.push(tx);
let bucket = Arc::new(bucket);
entry.insert(Arc::clone(&bucket));
(bucket, true)
}
}
}
}