use std::{
collections::HashMap,
fmt,
future::Future,
hash::Hash,
pin::Pin,
sync::Arc,
sync::Mutex,
task::{Context, Poll},
time::{Duration, Instant},
};
use async_trait::async_trait;
use axum::{
body::Body,
http::{HeaderName, HeaderValue, Request, header::RETRY_AFTER},
middleware::Next,
response::{IntoResponse, Response},
};
use sha2::{Digest, Sha256};
use thiserror::Error;
use tower::{Layer, Service};
use crate::{
clock::{SharedClock, system_clock},
http::{ApiProblem, HttpRequestPolicy, ProblemCode, RequestContext},
observability::{MetricLabels, MetricRecorder, metric_labels, metric_names, metric_outcomes},
result::{ClassifyError, ErrorKind},
};
pub const DEFAULT_MAX_RATE_LIMIT_BUCKETS: usize = 10_000;
pub const MAX_RATE_LIMIT_HEADER_VALUE_BYTES: usize = 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RateLimitRequestPolicy {
Enabled,
Disabled,
}
impl RateLimitRequestPolicy {
#[must_use]
pub const fn enabled(self) -> bool {
matches!(self, Self::Enabled)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct RateLimitConfig {
capacity: u32,
refill_tokens: u32,
refill_interval: Duration,
}
impl RateLimitConfig {
#[must_use]
pub const fn new(capacity: u32, refill_tokens: u32, refill_interval: Duration) -> Self {
Self {
capacity,
refill_tokens,
refill_interval,
}
}
#[must_use]
pub const fn capacity(&self) -> u32 {
self.capacity
}
#[must_use]
pub const fn refill_tokens(&self) -> u32 {
self.refill_tokens
}
#[must_use]
pub const fn refill_interval(&self) -> Duration {
self.refill_interval
}
pub fn validate(&self) -> Result<(), RateLimitError> {
if self.capacity == 0 {
return Err(RateLimitError::InvalidCapacity);
}
if self.refill_tokens == 0 {
return Err(RateLimitError::InvalidRefillTokens);
}
if self.refill_interval.is_zero() {
return Err(RateLimitError::InvalidRefillInterval);
}
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct RateLimitDecision {
kind: RateLimitDecisionKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum RateLimitDecisionKind {
Allowed { remaining: u32 },
Denied { retry_after: Duration },
}
impl RateLimitDecision {
#[must_use]
pub const fn allowed(remaining: u32) -> Self {
Self {
kind: RateLimitDecisionKind::Allowed { remaining },
}
}
#[must_use]
pub const fn denied(retry_after: Duration) -> Self {
Self {
kind: RateLimitDecisionKind::Denied { retry_after },
}
}
#[must_use]
pub const fn is_allowed(&self) -> bool {
matches!(self.kind, RateLimitDecisionKind::Allowed { .. })
}
#[must_use]
pub const fn remaining(&self) -> u32 {
match self.kind {
RateLimitDecisionKind::Allowed { remaining } => remaining,
RateLimitDecisionKind::Denied { .. } => 0,
}
}
#[must_use]
pub const fn retry_after(&self) -> Option<Duration> {
match self.kind {
RateLimitDecisionKind::Allowed { .. } => None,
RateLimitDecisionKind::Denied { retry_after } => Some(retry_after),
}
}
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum RateLimitError {
#[error("rate limit capacity must be greater than zero")]
InvalidCapacity,
#[error("rate limit refill_tokens must be greater than zero")]
InvalidRefillTokens,
#[error("rate limit refill_interval must be greater than zero")]
InvalidRefillInterval,
#[error("rate limit max buckets must be greater than zero")]
InvalidMaxBuckets,
#[error("rate limiter bucket capacity is exhausted")]
CapacityExceeded,
#[error("rate limiter state lock is poisoned")]
LockPoisoned,
#[error("rate limit store is unavailable")]
StoreUnavailable,
}
impl ClassifyError for RateLimitError {
fn error_kind(&self) -> ErrorKind {
match self {
Self::InvalidCapacity
| Self::InvalidRefillTokens
| Self::InvalidRefillInterval
| Self::InvalidMaxBuckets => ErrorKind::Validation,
Self::CapacityExceeded | Self::LockPoisoned | Self::StoreUnavailable => {
ErrorKind::Unavailable
}
}
}
fn error_code(&self) -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed(match self {
Self::InvalidCapacity => "rate_limit_invalid_capacity",
Self::InvalidRefillTokens => "rate_limit_invalid_refill_tokens",
Self::InvalidRefillInterval => "rate_limit_invalid_refill_interval",
Self::InvalidMaxBuckets => "rate_limit_invalid_max_buckets",
Self::CapacityExceeded => "rate_limit_capacity_exceeded",
Self::LockPoisoned => "rate_limit_lock_poisoned",
Self::StoreUnavailable => "rate_limit_store_unavailable",
})
}
}
#[derive(Debug, Clone)]
struct Bucket {
tokens: u32,
refill_at: Instant,
last_seen: Instant,
}
impl Bucket {
fn new(config: RateLimitConfig, now: Instant) -> Self {
Self {
tokens: config.capacity,
refill_at: now,
last_seen: now,
}
}
fn decide(&mut self, config: RateLimitConfig, now: Instant) -> RateLimitDecision {
self.refill(config, now);
self.last_seen = now;
if self.tokens > 0 {
self.tokens -= 1;
RateLimitDecision::allowed(self.tokens)
} else {
let elapsed = now.saturating_duration_since(self.refill_at);
let retry_after = config.refill_interval.saturating_sub(elapsed);
RateLimitDecision::denied(retry_after)
}
}
fn refill(&mut self, config: RateLimitConfig, now: Instant) {
let elapsed = now.saturating_duration_since(self.refill_at);
let intervals = elapsed.as_nanos() / config.refill_interval.as_nanos();
if intervals == 0 {
return;
}
let refill = u32::try_from(intervals)
.unwrap_or(u32::MAX)
.saturating_mul(config.refill_tokens);
self.tokens = self.tokens.saturating_add(refill).min(config.capacity);
let refill_elapsed = multiply_duration(config.refill_interval, intervals);
self.refill_at = self.refill_at.checked_add(refill_elapsed).unwrap_or(now);
}
fn would_be_fully_refilled(&self, config: RateLimitConfig, now: Instant) -> bool {
let elapsed = now.saturating_duration_since(self.refill_at);
let intervals = elapsed.as_nanos() / config.refill_interval.as_nanos();
let refill = u32::try_from(intervals)
.unwrap_or(u32::MAX)
.saturating_mul(config.refill_tokens);
self.tokens.saturating_add(refill) >= config.capacity
}
}
#[non_exhaustive]
pub struct TokenBucketRateLimiter<K> {
config: RateLimitConfig,
max_buckets: usize,
state: Mutex<BucketState<K>>,
}
struct BucketState<K> {
buckets: HashMap<K, Bucket>,
overflow_bucket: Option<Bucket>,
last_capacity_prune: Option<Instant>,
}
impl<K> fmt::Debug for TokenBucketRateLimiter<K> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("TokenBucketRateLimiter")
.field("config", &self.config)
.field("max_buckets", &self.max_buckets)
.field(
"bucket_count",
&self.state.try_lock().ok().map(|state| state.buckets.len()),
)
.finish_non_exhaustive()
}
}
#[async_trait]
pub trait RateLimitStore: Send + Sync {
async fn check(
&self,
key: String,
config: RateLimitConfig,
now: Instant,
) -> Result<RateLimitDecision, RateLimitError>;
async fn prune_idle(&self, older_than: Instant) -> Result<usize, RateLimitError>;
}
#[derive(Debug)]
#[non_exhaustive]
pub struct MemoryRateLimitStore {
limiter: TokenBucketRateLimiter<String>,
}
impl MemoryRateLimitStore {
pub fn new(config: RateLimitConfig) -> Result<Self, RateLimitError> {
Ok(Self {
limiter: TokenBucketRateLimiter::new(config)?,
})
}
#[must_use]
pub const fn limiter(&self) -> &TokenBucketRateLimiter<String> {
&self.limiter
}
}
#[async_trait]
impl RateLimitStore for MemoryRateLimitStore {
async fn check(
&self,
key: String,
config: RateLimitConfig,
now: Instant,
) -> Result<RateLimitDecision, RateLimitError> {
self.limiter.check_at_with_config(key, config, now)
}
async fn prune_idle(&self, older_than: Instant) -> Result<usize, RateLimitError> {
self.limiter.prune_idle(older_than)
}
}
#[derive(Clone)]
#[non_exhaustive]
pub struct RateLimitRuntime {
config: RateLimitConfig,
store: Arc<dyn RateLimitStore>,
key_source: RateLimitKeySource,
clock: SharedClock,
metrics: Option<Arc<dyn MetricRecorder>>,
}
impl fmt::Debug for RateLimitRuntime {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("RateLimitRuntime")
.field("config", &self.config)
.field("key_source", &self.key_source)
.finish_non_exhaustive()
}
}
impl RateLimitRuntime {
pub fn new(
config: RateLimitConfig,
key_source: RateLimitKeySource,
) -> Result<Self, RateLimitError> {
Self::with_store(
config,
key_source,
Arc::new(MemoryRateLimitStore::new(config)?),
)
}
pub fn with_store(
config: RateLimitConfig,
key_source: RateLimitKeySource,
store: Arc<dyn RateLimitStore>,
) -> Result<Self, RateLimitError> {
config.validate()?;
Ok(Self {
config,
store,
key_source,
clock: system_clock(),
metrics: None,
})
}
#[must_use]
pub fn from_limiter(
limiter: Arc<TokenBucketRateLimiter<String>>,
key_source: RateLimitKeySource,
) -> Self {
Self {
config: limiter.config(),
store: Arc::new(TokenBucketRateLimiterStore { limiter }),
key_source,
clock: system_clock(),
metrics: None,
}
}
#[must_use]
pub fn with_clock(mut self, clock: SharedClock) -> Self {
self.clock = clock;
self
}
#[must_use]
pub fn with_metrics(mut self, metrics: Option<Arc<dyn MetricRecorder>>) -> Self {
self.metrics = metrics;
self
}
#[must_use]
pub const fn config(&self) -> RateLimitConfig {
self.config
}
#[must_use]
pub fn store(&self) -> &Arc<dyn RateLimitStore> {
&self.store
}
#[must_use]
pub const fn key_source(&self) -> &RateLimitKeySource {
&self.key_source
}
#[must_use]
pub fn clock(&self) -> &SharedClock {
&self.clock
}
#[must_use]
pub fn metrics(&self) -> Option<&Arc<dyn MetricRecorder>> {
self.metrics.as_ref()
}
}
#[derive(Debug)]
struct TokenBucketRateLimiterStore {
limiter: Arc<TokenBucketRateLimiter<String>>,
}
#[async_trait]
impl RateLimitStore for TokenBucketRateLimiterStore {
async fn check(
&self,
key: String,
config: RateLimitConfig,
now: Instant,
) -> Result<RateLimitDecision, RateLimitError> {
self.limiter.check_at_with_config(key, config, now)
}
async fn prune_idle(&self, older_than: Instant) -> Result<usize, RateLimitError> {
self.limiter.prune_idle(older_than)
}
}
#[derive(Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum RateLimitKeySource {
ClientIp,
PrincipalId,
RequestId,
Header(HeaderName),
Fixed(String),
}
impl fmt::Debug for RateLimitKeySource {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ClientIp => formatter.write_str("ClientIp"),
Self::PrincipalId => formatter.write_str("PrincipalId"),
Self::RequestId => formatter.write_str("RequestId"),
Self::Header(name) => formatter.debug_tuple("Header").field(name).finish(),
Self::Fixed(_) => formatter.debug_tuple("Fixed").field(&"[REDACTED]").finish(),
}
}
}
impl RateLimitKeySource {
#[must_use]
pub fn key_for<B>(&self, request: &Request<B>) -> Option<String> {
match self {
Self::ClientIp => request
.extensions()
.get::<RequestContext>()
.and_then(RequestContext::client_ip)
.map(ToOwned::to_owned),
Self::PrincipalId => request
.extensions()
.get::<RequestContext>()
.and_then(RequestContext::principal)
.map(|principal| principal.id().to_owned()),
Self::RequestId => request
.extensions()
.get::<RequestContext>()
.map(|context| context.request_id().to_string()),
Self::Header(name) => request
.headers()
.get(name)
.and_then(|value| value.to_str().ok())
.map(str::trim)
.filter(|value| !value.is_empty())
.filter(|value| value.len() <= MAX_RATE_LIMIT_HEADER_VALUE_BYTES)
.map(|value| hash_header_rate_limit_key(name, value)),
Self::Fixed(value) => Some(value.clone()),
}
}
}
fn hash_header_rate_limit_key(name: &HeaderName, value: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(b"footing-rate-limit-header-v1\0");
hash_rate_limit_part(&mut hasher, name.as_str().as_bytes());
hash_rate_limit_part(&mut hasher, value.as_bytes());
format!("header:{}", to_lower_hex(&hasher.finalize()))
}
fn hash_rate_limit_part(hasher: &mut Sha256, value: &[u8]) {
hasher.update(u64::try_from(value.len()).unwrap_or(u64::MAX).to_be_bytes());
hasher.update(value);
}
pub async fn rate_limit_middleware(request: Request<Body>, next: Next) -> Response {
let Some(runtime) = request.extensions().get::<RateLimitRuntime>().cloned() else {
return next.run(request).await;
};
match enforce_rate_limit(&runtime, &request).await {
Ok(()) => next.run(request).await,
Err(rejection) => rejection.into_response(),
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct RateLimitLayer {
runtime: RateLimitRuntime,
request_policy: HttpRequestPolicy,
}
impl RateLimitLayer {
#[must_use]
pub fn new(runtime: RateLimitRuntime) -> Self {
Self {
runtime,
request_policy: HttpRequestPolicy::default(),
}
}
#[must_use]
pub fn with_request_policy(mut self, request_policy: HttpRequestPolicy) -> Self {
self.request_policy = request_policy;
self
}
#[must_use]
pub const fn runtime(&self) -> &RateLimitRuntime {
&self.runtime
}
#[must_use]
pub const fn request_policy(&self) -> &HttpRequestPolicy {
&self.request_policy
}
}
impl<S> Layer<S> for RateLimitLayer {
type Service = RateLimitService<S>;
fn layer(&self, inner: S) -> Self::Service {
RateLimitService {
inner,
runtime: self.runtime.clone(),
request_policy: self.request_policy.clone(),
}
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct RateLimitService<S> {
inner: S,
runtime: RateLimitRuntime,
request_policy: HttpRequestPolicy,
}
impl<S> Service<Request<Body>> for RateLimitService<S>
where
S: Service<Request<Body>, Response = Response> + Clone + Send + 'static,
S::Future: Send + 'static,
S::Error: Send + 'static,
{
type Response = Response;
type Error = S::Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
fn poll_ready(&mut self, context: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(context)
}
fn call(&mut self, request: Request<Body>) -> Self::Future {
let runtime = self.runtime.clone();
let request_policy = self.request_policy.clone();
let clone = self.inner.clone();
let mut inner = std::mem::replace(&mut self.inner, clone);
Box::pin(async move {
if !request_policy.policy_for(&request).rate_limit().enabled() {
return inner.call(request).await;
}
if let Err(rejection) = enforce_rate_limit(&runtime, &request).await {
return Ok(rejection.into_response());
}
inner.call(request).await
})
}
}
pub fn enforce_rate_limit<B>(
runtime: &RateLimitRuntime,
request: &Request<B>,
) -> impl Future<Output = Result<(), RateLimitRejection>> + Send + 'static {
let runtime = runtime.clone();
let key = runtime
.key_source
.key_for(request)
.unwrap_or_else(|| "anonymous".to_owned());
async move { enforce_rate_limit_key(&runtime, key).await }
}
async fn enforce_rate_limit_key(
runtime: &RateLimitRuntime,
key: String,
) -> Result<(), RateLimitRejection> {
let decision = runtime
.store
.check(key, runtime.config, runtime.clock.now())
.await
.map_err(|_| RateLimitRejection::Unavailable)?;
match decision.kind {
RateLimitDecisionKind::Allowed { .. } => {
record_rate_limit_metric(runtime, metric_outcomes::ALLOWED);
Ok(())
}
RateLimitDecisionKind::Denied { retry_after } => {
record_rate_limit_metric(runtime, metric_outcomes::DENIED);
Err(RateLimitRejection::Limited { retry_after })
}
}
}
fn record_rate_limit_metric(runtime: &RateLimitRuntime, outcome: &'static str) {
if let Some(metrics) = runtime.metrics() {
let _ = metrics.increment_counter(
metric_names::RATE_LIMIT_DECISIONS_TOTAL,
1,
MetricLabels::from_lossy([(metric_labels::OUTCOME, outcome)]),
);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RateLimitRejection {
Limited {
#[doc = "The delay before retrying."]
retry_after: Duration,
},
Unavailable,
}
impl IntoResponse for RateLimitRejection {
fn into_response(self) -> Response {
match self {
Self::Limited { retry_after } => {
let retry_after_seconds = retry_after_header_seconds(retry_after);
let mut response = ApiProblem::too_many_requests(
ProblemCode::from_static("rate_limited"),
"too many requests",
)
.with_details(serde_json::json!({
"retry_after_seconds": retry_after_seconds,
}))
.into_response();
if let Ok(value) = HeaderValue::from_str(&retry_after_seconds.to_string()) {
response.headers_mut().insert(RETRY_AFTER, value);
}
response
}
Self::Unavailable => ApiProblem::service_unavailable(
ProblemCode::from_static("rate_limit_unavailable"),
"rate limit is unavailable",
)
.into_response(),
}
}
}
fn retry_after_header_seconds(retry_after: Duration) -> u64 {
let seconds = retry_after.as_secs();
if retry_after.is_zero() || retry_after.subsec_nanos() == 0 {
seconds
} else {
seconds.saturating_add(1)
}
}
impl<K> TokenBucketRateLimiter<K>
where
K: Eq + Hash + Clone,
{
pub fn new(config: RateLimitConfig) -> Result<Self, RateLimitError> {
Self::with_max_buckets(config, DEFAULT_MAX_RATE_LIMIT_BUCKETS)
}
pub fn with_max_buckets(
config: RateLimitConfig,
max_buckets: usize,
) -> Result<Self, RateLimitError> {
config.validate()?;
if max_buckets == 0 {
return Err(RateLimitError::InvalidMaxBuckets);
}
Ok(Self {
config,
max_buckets,
state: Mutex::new(BucketState {
buckets: HashMap::new(),
overflow_bucket: None,
last_capacity_prune: None,
}),
})
}
#[must_use]
pub const fn config(&self) -> RateLimitConfig {
self.config
}
#[must_use]
pub const fn max_buckets(&self) -> usize {
self.max_buckets
}
pub fn bucket_count(&self) -> Result<usize, RateLimitError> {
self.state
.lock()
.map(|state| state.buckets.len())
.map_err(|_| RateLimitError::LockPoisoned)
}
pub fn check(&self, key: K) -> Result<RateLimitDecision, RateLimitError> {
self.check_at(key, Instant::now())
}
pub fn check_at(&self, key: K, now: Instant) -> Result<RateLimitDecision, RateLimitError> {
self.check_at_with_config(key, self.config, now)
}
pub fn check_at_with_config(
&self,
key: K,
config: RateLimitConfig,
now: Instant,
) -> Result<RateLimitDecision, RateLimitError> {
config.validate()?;
let mut state = self
.state
.lock()
.map_err(|_| RateLimitError::LockPoisoned)?;
if !state.buckets.contains_key(&key) {
if state.buckets.len() >= self.max_buckets {
let should_prune = state.last_capacity_prune.is_none_or(|last_prune| {
now.saturating_duration_since(last_prune) >= config.refill_interval
});
if should_prune {
state.last_capacity_prune = Some(now);
state
.buckets
.retain(|_, bucket| !bucket.would_be_fully_refilled(config, now));
}
if state.buckets.len() >= self.max_buckets {
return Ok(state
.overflow_bucket
.get_or_insert_with(|| Bucket::new(config, now))
.decide(config, now));
}
state.overflow_bucket = None;
}
state.buckets.insert(key.clone(), Bucket::new(config, now));
}
state
.buckets
.get_mut(&key)
.map(|bucket| bucket.decide(config, now))
.ok_or(RateLimitError::StoreUnavailable)
}
pub fn prune_idle(&self, older_than: Instant) -> Result<usize, RateLimitError> {
let mut state = self
.state
.lock()
.map_err(|_| RateLimitError::LockPoisoned)?;
let before = state.buckets.len();
state
.buckets
.retain(|_, bucket| bucket.last_seen >= older_than);
if state.buckets.len() < self.max_buckets {
state.overflow_bucket = None;
}
Ok(before - state.buckets.len())
}
}
fn to_lower_hex(bytes: &[u8]) -> String {
const HEX: &[u8; 16] = b"0123456789abcdef";
let mut output = String::with_capacity(bytes.len() * 2);
for byte in bytes {
output.push(char::from(HEX[usize::from(byte >> 4)]));
output.push(char::from(HEX[usize::from(byte & 0x0f)]));
}
output
}
fn multiply_duration(duration: Duration, multiplier: u128) -> Duration {
let nanos = duration.as_nanos().saturating_mul(multiplier);
Duration::from_nanos(u64::try_from(nanos).unwrap_or(u64::MAX))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn token_bucket_denies_after_capacity_and_refills() {
let limiter =
TokenBucketRateLimiter::new(RateLimitConfig::new(2, 1, Duration::from_secs(10)))
.unwrap();
let now = Instant::now();
assert!(limiter.check_at("user", now).unwrap().is_allowed());
assert!(limiter.check_at("user", now).unwrap().is_allowed());
assert!(!limiter.check_at("user", now).unwrap().is_allowed());
assert!(
limiter
.check_at("user", now + Duration::from_secs(10))
.unwrap()
.is_allowed()
);
}
#[test]
fn rate_limit_debug_does_not_expose_bucket_or_fixed_keys() {
let limiter =
TokenBucketRateLimiter::new(RateLimitConfig::new(2, 1, Duration::from_secs(10)))
.expect("rate limiter should build");
let private_key = "private-account-rate-limit-key";
limiter
.check(private_key.to_owned())
.expect("rate limit check should succeed");
let output = format!(
"{limiter:?} {:?}",
RateLimitKeySource::Fixed(private_key.to_owned())
);
assert!(output.contains("bucket_count"));
assert!(output.contains("[REDACTED]"));
assert!(!output.contains(private_key));
}
#[test]
fn token_bucket_validates_dynamic_config_before_refill() {
let limiter =
TokenBucketRateLimiter::new(RateLimitConfig::new(2, 1, Duration::from_secs(10)))
.unwrap();
let now = Instant::now();
let error = limiter
.check_at_with_config("user", RateLimitConfig::new(2, 1, Duration::ZERO), now)
.expect_err("zero refill interval must be rejected before refill");
assert!(matches!(error, RateLimitError::InvalidRefillInterval));
}
#[test]
fn token_bucket_uses_bounded_overflow_bucket_for_excess_keys() {
let limiter = TokenBucketRateLimiter::with_max_buckets(
RateLimitConfig::new(1, 1, Duration::from_secs(10)),
2,
)
.unwrap();
let now = Instant::now();
assert!(limiter.check_at("first", now).unwrap().is_allowed());
assert!(limiter.check_at("second", now).unwrap().is_allowed());
assert!(limiter.check_at("third", now).unwrap().is_allowed());
assert!(
!limiter.check_at("fourth", now).unwrap().is_allowed(),
"all excess keys should share one bounded overflow bucket"
);
assert_eq!(limiter.bucket_count().unwrap(), 2);
assert!(!limiter.check_at("first", now).unwrap().is_allowed());
assert_eq!(limiter.bucket_count().unwrap(), 2);
assert!(
limiter
.check_at("third", now + Duration::from_secs(10))
.unwrap()
.is_allowed()
);
assert_eq!(limiter.bucket_count().unwrap(), 1);
}
#[test]
fn header_key_source_hashes_and_bounds_untrusted_values() {
let source = RateLimitKeySource::Header(HeaderName::from_static("x-account-id"));
let private_value = "private-account-identifier";
let request = Request::builder()
.header("x-account-id", private_value)
.body(())
.expect("request should build");
let key = source.key_for(&request).expect("header key should exist");
assert!(key.starts_with("header:"));
assert!(!key.contains(private_value));
assert_eq!(source.key_for(&request).as_deref(), Some(key.as_str()));
let oversized = Request::builder()
.header(
"x-account-id",
"x".repeat(MAX_RATE_LIMIT_HEADER_VALUE_BYTES + 1),
)
.body(())
.expect("request should build");
assert!(source.key_for(&oversized).is_none());
}
#[test]
fn retry_after_header_seconds_rounds_subsecond_delays_up() {
assert_eq!(retry_after_header_seconds(Duration::ZERO), 0);
assert_eq!(retry_after_header_seconds(Duration::from_millis(1)), 1);
assert_eq!(retry_after_header_seconds(Duration::from_millis(1500)), 2);
assert_eq!(retry_after_header_seconds(Duration::from_secs(3)), 3);
}
#[test]
fn rate_limit_decision_encodes_allowed_and_denied_invariants() {
let allowed = RateLimitDecision::allowed(7);
assert!(allowed.is_allowed());
assert_eq!(allowed.remaining(), 7);
assert_eq!(allowed.retry_after(), None);
let denied = RateLimitDecision::denied(Duration::from_secs(3));
assert!(!denied.is_allowed());
assert_eq!(denied.remaining(), 0);
assert_eq!(denied.retry_after(), Some(Duration::from_secs(3)));
}
#[test]
fn rate_limit_errors_are_classified() {
assert_eq!(
RateLimitError::InvalidCapacity.error_kind(),
ErrorKind::Validation
);
assert_eq!(
RateLimitError::StoreUnavailable.error_kind(),
ErrorKind::Unavailable
);
assert_eq!(
RateLimitError::StoreUnavailable.error_code(),
"rate_limit_store_unavailable"
);
}
}