pub struct RateLimitConfig {
pub qps: u32,
pub burst: u32,
pub per_tenant_default: PerTenantRateLimitConfig,
}Expand description
Static configuration for the rate limiter.
Two layers, both enforced (whichever is tighter wins):
- Per-tenant bucket keyed on
(TokenId, TenantId)(per_tenant_default) — primary defence. Prevents one tenant from saturating a shared token’s quota. - Per-token bucket keyed on
TokenId(qps,burst) — backstop. Caps aggregate usage by a single token across all tenants.
Token-level knobs come from TENSOR_WASM_API_RATE_LIMIT_QPS and
TENSOR_WASM_API_RATE_LIMIT_BURST at server startup; per-tenant defaults
to PerTenantRateLimitConfig::default. If both knobs are zero (or
unset) the token-level backstop is disabled, but the per-tenant layer is
still in force unless explicitly cleared — see
RateLimitConfig::is_disabled.
Note: this type is no longer Eq because PerTenantRateLimitConfig::qps
is f64. Use PartialEq for comparisons in tests.
Fields§
§qps: u32Steady-state requests-per-second admitted per token (backstop layer).
burst: u32Maximum burst — the per-token bucket capacity, in permits.
per_tenant_default: PerTenantRateLimitConfigDefault per-tenant configuration applied to every (token, tenant)
pair. The primary defence against a single tenant exhausting a
shared token’s quota.
Implementations§
Source§impl RateLimitConfig
impl RateLimitConfig
Sourcepub const ENV_QPS: &'static str = "TENSOR_WASM_API_RATE_LIMIT_QPS"
pub const ENV_QPS: &'static str = "TENSOR_WASM_API_RATE_LIMIT_QPS"
Environment variable carrying the steady-state QPS allowance per token.
Sourcepub const ENV_BURST: &'static str = "TENSOR_WASM_API_RATE_LIMIT_BURST"
pub const ENV_BURST: &'static str = "TENSOR_WASM_API_RATE_LIMIT_BURST"
Environment variable carrying the burst (bucket capacity) per token.
Sourcepub const DEFAULT_QPS: u32 = 100
pub const DEFAULT_QPS: u32 = 100
Default QPS applied when ENV_QPS is unset but ENV_BURST is set.
Sourcepub const DEFAULT_BURST: u32 = 200
pub const DEFAULT_BURST: u32 = 200
Default burst applied when ENV_BURST is unset but ENV_QPS is set.
Sourcepub const fn disabled() -> Self
pub const fn disabled() -> Self
Disabled config: every layer off. The middleware is a pass-through.
Sourcepub const fn is_disabled(&self) -> bool
pub const fn is_disabled(&self) -> bool
true if every layer of the limiter is disabled and the middleware
would unconditionally admit. Used by rate_limit to short-circuit
the bucket lookup entirely.
Sourcepub const fn is_token_layer_disabled(&self) -> bool
pub const fn is_token_layer_disabled(&self) -> bool
true if the per-token (backstop) layer is disabled.
Sourcepub fn from_env() -> Self
pub fn from_env() -> Self
Load from the process environment.
- Both vars unset / either
"0"/ either unparseable => token-layer disabled. The per-tenant layer still defaults toPerTenantRateLimitConfig::default. - Otherwise: missing-but-other-side-set falls back to
DEFAULT_QPS/DEFAULT_BURST.
Trait Implementations§
Source§impl Clone for RateLimitConfig
impl Clone for RateLimitConfig
Source§fn clone(&self) -> RateLimitConfig
fn clone(&self) -> RateLimitConfig
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for RateLimitConfig
Source§impl Debug for RateLimitConfig
impl Debug for RateLimitConfig
Source§impl Default for RateLimitConfig
impl Default for RateLimitConfig
Source§impl PartialEq for RateLimitConfig
impl PartialEq for RateLimitConfig
Source§fn eq(&self, other: &RateLimitConfig) -> bool
fn eq(&self, other: &RateLimitConfig) -> bool
self and other values to be equal, and is used by ==.impl StructuralPartialEq for RateLimitConfig
Auto Trait Implementations§
impl Freeze for RateLimitConfig
impl RefUnwindSafe for RateLimitConfig
impl Send for RateLimitConfig
impl Sync for RateLimitConfig
impl Unpin for RateLimitConfig
impl UnsafeUnpin for RateLimitConfig
impl UnwindSafe for RateLimitConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more