]> Untitled Git - lemmy.git/commitdiff
Merge pull request #2160 from LemmyNet/rate_limit_websocket_fail
authorNutomic <me@nutomic.com>
Thu, 24 Mar 2022 20:43:02 +0000 (21:43 +0100)
committerGitHub <noreply@github.com>
Thu, 24 Mar 2022 20:43:02 +0000 (21:43 +0100)
Fix rate limit check for register. Fixes #2159

crates/utils/src/rate_limit/mod.rs
crates/utils/src/rate_limit/rate_limiter.rs

index d56dc0c5e8ea33e64a115efe02af892a160a37f1..e2a155eb5ae37e495f0ff33f8f0f899559204ae9 100644 (file)
@@ -86,7 +86,6 @@ impl RateLimited {
             &ip_addr,
             rate_limit.message,
             rate_limit.message_per_second,
-            false,
           )?;
 
           drop(limiter);
@@ -98,7 +97,6 @@ impl RateLimited {
             &ip_addr,
             rate_limit.post,
             rate_limit.post_per_second,
-            true,
           )?;
         }
         RateLimitType::Register => {
@@ -107,7 +105,6 @@ impl RateLimited {
             &ip_addr,
             rate_limit.register,
             rate_limit.register_per_second,
-            true,
           )?;
         }
         RateLimitType::Image => {
@@ -116,7 +113,6 @@ impl RateLimited {
             &ip_addr,
             rate_limit.image,
             rate_limit.image_per_second,
-            false,
           )?;
         }
         RateLimitType::Comment => {
@@ -125,7 +121,6 @@ impl RateLimited {
             &ip_addr,
             rate_limit.comment,
             rate_limit.comment_per_second,
-            false,
           )?;
         }
       };
@@ -133,34 +128,6 @@ impl RateLimited {
 
     let res = fut.await;
 
-    // after
-    {
-      let mut limiter = self.rate_limiter.lock().await;
-      if res.is_ok() {
-        match self.type_ {
-          RateLimitType::Post => {
-            limiter.check_rate_limit_full(
-              self.type_,
-              &ip_addr,
-              rate_limit.post,
-              rate_limit.post_per_second,
-              false,
-            )?;
-          }
-          RateLimitType::Register => {
-            limiter.check_rate_limit_full(
-              self.type_,
-              &ip_addr,
-              rate_limit.register,
-              rate_limit.register_per_second,
-              false,
-            )?;
-          }
-          _ => (),
-        };
-      }
-    }
-
     res
   }
 }
index de5557ea87716114fb48156fe3195811fbb12f0d..ccc483ed737f33b8e0715d839c8aa0e434d13d3f 100644 (file)
@@ -53,7 +53,6 @@ impl RateLimiter {
     ip: &IpAddr,
     rate: i32,
     per: i32,
-    check_only: bool,
   ) -> Result<(), LemmyError> {
     self.insert_ip(ip);
     if let Some(bucket) = self.buckets.get_mut(&type_) {
@@ -68,7 +67,7 @@ impl RateLimiter {
 
         rate_limit.last_checked = current;
         rate_limit.allowance += time_passed * (rate as f64 / per as f64);
-        if !check_only && rate_limit.allowance > rate as f64 {
+        if rate_limit.allowance > rate as f64 {
           rate_limit.allowance = rate as f64;
         }
 
@@ -91,9 +90,7 @@ impl RateLimiter {
             "too_many_requests",
           ))
         } else {
-          if !check_only {
-            rate_limit.allowance -= 1.0;
-          }
+          rate_limit.allowance -= 1.0;
           Ok(())
         }
       } else {