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::kv::{Allowance, Key};
use crate::pool::Pool;
#[must_use = "a quota opens with open()"]
pub struct QuotaBuilder {
builder: crate::kv::QuotaBuilder,
pool: Arc<Pool>,
}
impl Store {
pub fn quota(&self, name: &str) -> QuotaBuilder {
QuotaBuilder { builder: self.blocking().quota(name), pool: Arc::clone(self.pool()) }
}
}
impl QuotaBuilder {
pub fn window(self, name: &str, limit: u64, span: Duration) -> Self {
QuotaBuilder { builder: self.builder.window(name, limit, span), ..self }
}
pub fn open(self) -> Result<Quota> {
Ok(Quota { quota: self.builder.open()?, pool: self.pool })
}
}
pub struct Quota {
quota: crate::kv::Quota,
pool: Arc<Pool>,
}
impl Quota {
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 (quota, key) = (self.quota.clone(), key.text().into_owned());
let what = named(quota.scope.shown_key(&key), "an allow");
run::write(&self.pool, what, move |answer| quota.allow_n_then(&key, n, move |used| answer.send(used)))
}
pub fn peek(&self, key: impl Key) -> impl Future<Output = Result<Allowance>> + Send + 'static {
let (quota, key) = (self.quota.clone(), key.text().into_owned());
let what = named(quota.scope.shown_key(&key), "a peek");
run::point(&self.pool, what, move || quota.peek(&*key))
}
pub fn refund(&self, key: impl Key) -> impl Future<Output = Result<()>> + Send + 'static {
self.refund_n(key, 1)
}
pub fn refund_n(&self, key: impl Key, n: u64) -> impl Future<Output = Result<()>> + Send + 'static {
let (quota, key) = (self.quota.clone(), key.text().into_owned());
let what = named(quota.scope.shown_key(&key), "a refund");
run::write(&self.pool, what, move |answer| quota.refund_n_then(&key, n, move |refunded| answer.send(refunded)))
}
pub fn reset(&self, key: impl Key) -> impl Future<Output = Result<()>> + Send + 'static {
let (quota, key) = (self.quota.clone(), key.text().into_owned());
let what = named(quota.scope.shown_key(&key), "a reset");
run::write(&self.pool, what, move |answer| quota.reset_then(&key, move |reset| answer.send(reset)))
}
pub fn under(&self, owner: impl Key) -> Quota {
Quota { quota: self.quota.under(owner), pool: Arc::clone(&self.pool) }
}
pub fn blocking(&self) -> &crate::kv::Quota {
&self.quota
}
}
impl From<crate::kv::Quota> for Quota {
fn from(quota: crate::kv::Quota) -> Quota {
let pool = quota.scope.pool();
Quota { quota, pool }
}
}
impl Clone for Quota {
fn clone(&self) -> Self {
Quota { quota: self.quota.clone(), pool: Arc::clone(&self.pool) }
}
}
impl fmt::Debug for Quota {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.quota, f)
}
}
impl fmt::Debug for QuotaBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.builder, f)
}
}
#[cfg(test)]
#[path = "quota_tests.rs"]
mod tests;