diff options
Diffstat (limited to 'server/src/rate_limit/mod.rs')
-rw-r--r-- | server/src/rate_limit/mod.rs | 19 |
1 files changed, 8 insertions, 11 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 => { |