use std::fmt;
use std::future::Future;
use std::sync::Arc;
use std::time::Duration;
use super::named;
use crate::Result;
use crate::aio::{Store, run};
use crate::cancel;
use crate::kv::{Allowance, Key};
use crate::pool::Pool;
#[must_use = "a rate limit opens with open()"]
pub struct RateLimitBuilder {
builder: crate::kv::RateLimitBuilder,
pool: Arc<Pool>,
}
impl Store {
pub fn rate_limit(&self, name: &str) -> RateLimitBuilder {
RateLimitBuilder { builder: self.blocking().rate_limit(name), pool: Arc::clone(self.pool()) }
}
}
impl RateLimitBuilder {
pub fn rate(self, count: u64, per: Duration) -> Self {
RateLimitBuilder { builder: self.builder.rate(count, per), ..self }
}
pub fn burst(self, burst: u64) -> Self {
RateLimitBuilder { builder: self.builder.burst(burst), ..self }
}
pub fn open(self) -> Result<RateLimit> {
Ok(RateLimit { limit: self.builder.open()?, pool: self.pool })
}
}
pub struct RateLimit {
limit: crate::kv::RateLimit,
pool: Arc<Pool>,
}
impl RateLimit {
pub fn allow(&self, key: impl Key) -> impl Future<Output = Result<Allowance>> + Send + 'static {
self.allow_n(key, 1)
}
pub fn allow_n(&self, key: impl Key, n: u64) -> impl Future<Output = Result<Allowance>> + Send + 'static {
let key = key.text().into_owned();
let what = named(self.limit.scope.shown_key(&key), "an allow");
self.step(what, move |limit| limit.allow_n(&*key, n))
}
pub fn peek(&self, key: impl Key) -> impl Future<Output = Result<Allowance>> + Send + 'static {
let (limit, key) = (self.limit.clone(), key.text().into_owned());
let what = named(limit.scope.shown_key(&key), "a peek");
run::point(&self.pool, what, move || limit.peek(&*key))
}
pub fn reset(&self, key: impl Key) -> impl Future<Output = Result<()>> + Send + 'static {
let key = key.text().into_owned();
let what = named(self.limit.scope.shown_key(&key), "a reset");
self.step(what, move |limit| limit.reset(&*key))
}
pub fn under(&self, owner: impl Key) -> RateLimit {
RateLimit { limit: self.limit.under(owner), pool: Arc::clone(&self.pool) }
}
pub fn blocking(&self) -> &crate::kv::RateLimit {
&self.limit
}
fn step<T: Send + 'static>(
&self,
what: String,
step: impl Fn(&crate::kv::RateLimit) -> Result<T> + Send + 'static,
) -> impl Future<Output = Result<T>> + Send + 'static {
let (limit, pool, tokens) = (self.limit.clone(), Arc::clone(&self.pool), cancel::captured());
async move {
if limit.on_a_copy() {
return run::passed_on(what, move || step(&limit)).await;
}
tokens.run(None, || run::step(&pool, what, move || step(&limit))).await
}
}
}
impl From<crate::kv::RateLimit> for RateLimit {
fn from(limit: crate::kv::RateLimit) -> RateLimit {
let pool = limit.scope.pool();
RateLimit { limit, pool }
}
}
impl Clone for RateLimit {
fn clone(&self) -> Self {
RateLimit { limit: self.limit.clone(), pool: Arc::clone(&self.pool) }
}
}
impl fmt::Debug for RateLimit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.limit, f)
}
}
impl fmt::Debug for RateLimitBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.builder, f)
}
}
#[cfg(test)]
#[path = "rate_limit_tests.rs"]
mod tests;