use std::future::Future;
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use super::StatusCode;
use super::codes;
use super::failure::{ErrorDefinition, Failure, FailureKind};
use super::policy::Admission;
use super::request::Request;
use crate::{Invocation, RuntimeError};
const POLICY: &str = "default";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct RateLimitConfig {
pub prefix: &'static str,
pub max_requests: u32,
pub window: Duration,
}
impl RateLimitConfig {
#[must_use]
pub const fn new(prefix: &'static str, max_requests: u32, window: Duration) -> Self {
Self {
prefix,
max_requests,
window,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct RateLimit {
pub limit: u32,
pub window: Duration,
pub remaining: i64,
pub resets_at: SystemTime,
}
impl RateLimit {
#[must_use]
pub fn new(config: &RateLimitConfig, remaining: i64, resets_at: SystemTime) -> Self {
Self {
limit: config.max_requests,
window: config.window,
remaining,
resets_at,
}
}
#[must_use]
pub fn is_exceeded(&self) -> bool {
self.remaining < 0
}
#[must_use]
pub fn reset_after(&self) -> Duration {
self.resets_at
.duration_since(SystemTime::now())
.unwrap_or_default()
}
#[must_use]
pub fn headers(&self) -> Vec<(String, String)> {
let mut policy = format!("\"{POLICY}\";q={}", self.limit);
let window = whole_seconds(self.window);
if window > 0 {
policy.push_str(&format!(";w={window}"));
}
let state = format!(
"\"{POLICY}\";r={};t={}",
self.remaining.max(0),
whole_seconds(self.reset_after())
);
vec![
("RateLimit-Policy".to_owned(), policy),
("RateLimit".to_owned(), state),
]
}
}
fn whole_seconds(duration: Duration) -> u64 {
duration
.as_secs()
.saturating_add(u64::from(duration.subsec_nanos() > 0))
}
pub trait Counter: Send + Sync + 'static {
fn hit(
&self,
key: &str,
config: &RateLimitConfig,
) -> impl Future<Output = Result<RateLimit, RuntimeError>> + Send;
}
type KeyOf = Arc<dyn Fn(&Request<'_>) -> String + Send + Sync>;
pub struct RateLimited<C> {
counter: C,
config: RateLimitConfig,
key: KeyOf,
refusal: ErrorDefinition,
}
impl<C: Counter> RateLimited<C> {
#[must_use]
pub fn new(counter: C, config: RateLimitConfig) -> Self {
Self {
counter,
config,
key: Arc::new(|request: &Request<'_>| request.source_ip()),
refusal: ErrorDefinition::new(
codes::RATE_LIMITED,
StatusCode::TOO_MANY_REQUESTS,
"Too many requests",
),
}
}
#[must_use]
pub fn key(mut self, key: impl Fn(&Request<'_>) -> String + Send + Sync + 'static) -> Self {
self.key = Arc::new(key);
self
}
#[must_use]
pub fn refusal(mut self, refusal: ErrorDefinition) -> Self {
self.refusal = refusal;
self
}
}
impl<C: Counter> Admission for RateLimited<C> {
async fn check(
&self,
request: &Request<'_>,
_invocation: &Invocation,
) -> Result<Vec<(String, String)>, Failure> {
let key = (self.key)(request);
let outcome = self
.counter
.hit(&key, &self.config)
.await
.map_err(|error| Failure::from(error).with_kind(FailureKind::Admission))?;
let headers = outcome.headers();
if outcome.is_exceeded() {
return Err(self
.refusal
.failure()
.with_kind(FailureKind::Admission)
.with_header(
"retry-after",
&whole_seconds(outcome.reset_after()).to_string(),
)
.with_headers(headers));
}
Ok(headers)
}
}
impl<C> std::fmt::Debug for RateLimited<C> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RateLimited")
.field("config", &self.config)
.field("refusal", &self.refusal)
.finish_non_exhaustive()
}
}
#[cfg(feature = "dynamo")]
pub use dynamo::DynamoWindow;
#[cfg(feature = "dynamo")]
mod dynamo {
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use aws_sdk_dynamodb::Client;
use aws_sdk_dynamodb::types::{AttributeValue, ReturnValue};
use tracing::Instrument as _;
use super::{Counter, RateLimit, RateLimitConfig};
use crate::RuntimeError;
#[derive(Debug, Clone)]
pub struct DynamoWindow {
client: Client,
table: String,
partition: String,
sort: String,
ttl: String,
}
impl DynamoWindow {
#[must_use]
pub fn new(client: Client, table: impl Into<String>) -> Self {
Self {
client,
table: table.into(),
partition: "PK".to_owned(),
sort: "SK".to_owned(),
ttl: "ttl".to_owned(),
}
}
#[must_use]
pub fn attributes(
mut self,
partition: impl Into<String>,
sort: impl Into<String>,
ttl: impl Into<String>,
) -> Self {
self.partition = partition.into();
self.sort = sort.into();
self.ttl = ttl.into();
self
}
}
impl Counter for DynamoWindow {
async fn hit(
&self,
key: &str,
config: &RateLimitConfig,
) -> Result<RateLimit, RuntimeError> {
let window = u64::try_from(config.window.as_millis())
.unwrap_or(u64::MAX)
.max(1);
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |since| since.as_millis() as u64);
let start = now / window * window;
let reset = start.saturating_add(window);
let output = self
.client
.update_item()
.table_name(&self.table)
.key(
&self.partition,
AttributeValue::S(format!("RATE_LIMIT#{}#{key}", config.prefix)),
)
.key(&self.sort, AttributeValue::S(format!("WINDOW#{start}")))
.update_expression("SET #ttl = :ttl ADD #count :one")
.expression_attribute_names("#ttl", &self.ttl)
.expression_attribute_names("#count", "requestCount")
.expression_attribute_values(
":ttl",
AttributeValue::N(reset.div_ceil(1000).to_string()),
)
.expression_attribute_values(":one", AttributeValue::N("1".to_owned()))
.return_values(ReturnValue::UpdatedNew)
.send()
.instrument(crate::dynamo::span("UpdateItem", &self.table))
.await
.map_err(|error| RuntimeError::other("counting a request", error))?;
let count = output
.attributes()
.and_then(|attributes| attributes.get("requestCount"))
.and_then(|value| value.as_n().ok())
.and_then(|value| value.parse::<i64>().ok())
.ok_or_else(|| RuntimeError::message("the rate-limit counter was not returned"))?;
Ok(RateLimit::new(
config,
i64::from(config.max_requests).saturating_sub(count),
UNIX_EPOCH + Duration::from_millis(reset),
))
}
}
}