pub struct RateLimiter { /* private fields */ }Expand description
A per-session token-bucket rate limiter.
Two backend implementations are available:
- Simple (always available) — a sliding-window counter per session
stored in a [
dashmap::DashMap]. Accurate to the granularity of the configured window. - Governor (requires feature
rate-limiting) — a true token-bucket backed by thegovernorcrate, with sub-millisecond accuracy.
§Examples
use tokio_prompt_orchestrator::enhanced::RateLimiter;
let limiter = RateLimiter::new(100, 60); // 100 requests per 60-second window
if limiter.check("user-123").await {
// allowed
} else {
// rate limit exceeded
}§Isolation
Each session (identified by a string key) has its own independent counter. Exhausting one session’s quota does not affect others.
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(max_requests: usize, window_secs: u64) -> Self
pub fn new(max_requests: usize, window_secs: u64) -> Self
Create simple rate limiter
max_requests: Maximum requests allowedwindow_secs: Time window in seconds
Sourcepub async fn check(&self, session_id: &str) -> bool
pub async fn check(&self, session_id: &str) -> bool
Check if request is allowed for session
Returns true if allowed, false if rate limit exceeded
Sourcepub fn get_usage(&self, session_id: &str) -> Option<RateLimitInfo>
pub fn get_usage(&self, session_id: &str) -> Option<RateLimitInfo>
Get current usage for session
Trait Implementations§
Source§impl Clone for RateLimiter
impl Clone for RateLimiter
Source§fn clone(&self) -> RateLimiter
fn clone(&self) -> RateLimiter
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for RateLimiter
impl !UnwindSafe for RateLimiter
impl Freeze for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl UnsafeUnpin for RateLimiter
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
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request