pub trait RateLimiter: Send + Sync {
// Required methods
fn check_rate_limit<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn record_request<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
}Expand description
Rate limiter port
Implements rate limiting to prevent overwhelming external services or exceeding API quotas. Supports per-key rate limiting for multi-tenant scenarios.
§Example Implementation
use stygian_graph::ports::RateLimiter;
use stygian_graph::domain::error::Result;
use async_trait::async_trait;
struct MyRateLimiter;
#[async_trait]
impl RateLimiter for MyRateLimiter {
async fn check_rate_limit(&self, key: &str) -> Result<bool> {
// Check if key is within rate limit
Ok(true)
}
async fn record_request(&self, key: &str) -> Result<()> {
// Record request for rate limiting
Ok(())
}
}Required Methods§
Sourcefn check_rate_limit<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn check_rate_limit<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Check if key is within rate limit
§Arguments
key- Rate limit key (service name, API endpoint, user ID, etc.)
§Returns
Ok(true)- Request allowedOk(false)- Rate limit exceededErr(RateLimitError)- Backend failure
§Example
if limiter.check_rate_limit("api:openai").await.unwrap() {
println!("Request allowed");
} else {
println!("Rate limit exceeded, retry later");
}Sourcefn record_request<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn record_request<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Record a request for rate limiting
Should be called after successful operation to update the rate limit counter.
§Arguments
key- Rate limit key
§Returns
Ok(())- Request recordedErr(RateLimitError)- Backend failure
§Example
// After making API call
limiter.record_request("api:openai").await.unwrap();