diff options
Diffstat (limited to 'server/src/rate_limit')
-rw-r--r-- | server/src/rate_limit/mod.rs | 19 | ||||
-rw-r--r-- | server/src/rate_limit/rate_limiter.rs | 5 |
2 files changed, 10 insertions, 14 deletions
diff --git a/server/src/rate_limit/mod.rs b/server/src/rate_limit/mod.rs index b4c2dc5d..e49a527e 100644 --- a/server/src/rate_limit/mod.rs +++ b/server/src/rate_limit/mod.rs @@ -1,5 +1,5 @@ use super::{IPAddr, Settings}; -use crate::{api::APIError, get_ip, settings::RateLimitConfig}; +use crate::{get_ip, settings::RateLimitConfig, LemmyError}; use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform}; use futures::future::{ok, Ready}; use rate_limiter::{RateLimitType, RateLimiter}; @@ -15,6 +15,8 @@ pub mod rate_limiter; #[derive(Debug, Clone)] pub struct RateLimit { + // it might be reasonable to use a std::sync::Mutex here, since we don't need to lock this + // across await points pub rate_limiter: Arc<Mutex<RateLimiter>>, } @@ -57,17 +59,11 @@ impl RateLimited { fut: impl Future<Output = Result<T, E>>, ) -> Result<T, E> where - E: From<failure::Error>, + E: From<LemmyError>, { - let rate_limit: RateLimitConfig = actix_web::web::block(move || { - // needs to be in a web::block because the RwLock in settings is from stdlib - Ok(Settings::get().rate_limit) as Result<_, failure::Error> - }) - .await - .map_err(|e| match e { - actix_web::error::BlockingError::Error(e) => e, - _ => APIError::err("Operation canceled").into(), - })?; + // Does not need to be blocking because the RwLock in settings never held across await points, + // and the operation here locks only long enough to clone + let rate_limit: RateLimitConfig = Settings::get().rate_limit; // before { @@ -83,6 +79,7 @@ impl RateLimited { false, )?; + drop(limiter); return fut.await; } RateLimitType::Post => { diff --git a/server/src/rate_limit/rate_limiter.rs b/server/src/rate_limit/rate_limiter.rs index b3ac7093..3e2ea787 100644 --- a/server/src/rate_limit/rate_limiter.rs +++ b/server/src/rate_limit/rate_limiter.rs @@ -1,6 +1,5 @@ use super::IPAddr; -use crate::api::APIError; -use failure::Error; +use crate::{api::APIError, LemmyError}; use log::debug; use std::{collections::HashMap, time::SystemTime}; use strum::IntoEnumIterator; @@ -61,7 +60,7 @@ impl RateLimiter { rate: i32, per: i32, check_only: bool, - ) -> Result<(), Error> { + ) -> Result<(), LemmyError> { self.insert_ip(ip); if let Some(bucket) = self.buckets.get_mut(&type_) { if let Some(rate_limit) = bucket.get_mut(ip) { |