The smokesignal.events web application
at main 28 lines 975 B view raw
1use async_trait::async_trait; 2use thiserror::Error; 3 4#[derive(Debug, Error)] 5pub enum ThrottleError { 6 #[error("error-smokesignal-throttle-1 Redis error: {0}")] 7 RedisError(String), 8 9 #[error("error-smokesignal-throttle-2 Time error: {0}")] 10 TimeError(String), 11} 12 13/// Trait for throttling actions by key 14/// 15/// Implementations can use different strategies (sliding window, token bucket, etc.) 16/// to limit the rate of actions for a given key. 17#[async_trait] 18pub trait Throttle: Send + Sync { 19 /// Check if an action is allowed for the given key, and if so, record it 20 /// 21 /// Returns true if the action is allowed, false if throttled 22 async fn check_and_record(&self, key: &str) -> Result<bool, ThrottleError>; 23 24 /// Check if an action is allowed for the given key without recording 25 /// 26 /// Returns true if the action would be allowed, false if it would be throttled 27 async fn check(&self, key: &str) -> Result<bool, ThrottleError>; 28}