summaryrefslogtreecommitdiffstats
path: root/server/src/rate_limit/mod.rs
diff options
context:
space:
mode:
authorRiley <asonix@asonix.dog>2020-07-01 07:54:29 -0500
committerGitHub <noreply@github.com>2020-07-01 08:54:29 -0400
commita074564458b8a108b77d98e5e8ce24168656763a (patch)
tree8cfb4e463b6b2dbd3c4b3ac2f312a42542f38d64 /server/src/rate_limit/mod.rs
parent4c1cb5999cad496714cec67f101be38cd281d416 (diff)
Federation async (#848)
* Asyncify more * I guess these changed * Clean PR a bit * Convert more away from failure error * config changes for testing federation * It was DNS So actix-web's client relies on TRust DNS Resolver to figure out where to send data, but TRust DNS Resolver seems to not play nice with docker, which expressed itself as not resolving the name to an IP address _the first time_ when making a request. The fix was literally to make the request again (which I limited to 3 times total, and not exceeding the request timeout in total) * Only retry for connecterror Since TRust DNS Resolver was causing ConnectError::Timeout, this change limits the retry to only this error, returning immediately for any other error * Use http sig norm 0.4.0-alpha for actix-web 3.0 support * Blocking function, retry http requests * cargo +nightly fmt * Only create one pictrs dir * Don't yarn build * cargo +nightly fmt
Diffstat (limited to 'server/src/rate_limit/mod.rs')
-rw-r--r--server/src/rate_limit/mod.rs19
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 => {