mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-12-12 02:56:31 +00:00
Initialize rate limit allowance directly
This commit is contained in:
parent
e3046a78f1
commit
fd618d6b5b
1 changed files with 1 additions and 7 deletions
|
@ -29,7 +29,6 @@ impl RateLimitStorage {
|
||||||
/// Rate limiting Algorithm described here: https://stackoverflow.com/a/668327/1655478
|
/// Rate limiting Algorithm described here: https://stackoverflow.com/a/668327/1655478
|
||||||
///
|
///
|
||||||
/// Returns true if the request passed the rate limit, false if it failed and should be rejected.
|
/// Returns true if the request passed the rate limit, false if it failed and should be rejected.
|
||||||
#[allow(clippy::float_cmp)]
|
|
||||||
pub(super) fn check_rate_limit_full(
|
pub(super) fn check_rate_limit_full(
|
||||||
&mut self,
|
&mut self,
|
||||||
type_: RateLimitType,
|
type_: RateLimitType,
|
||||||
|
@ -41,18 +40,13 @@ impl RateLimitStorage {
|
||||||
let ip_buckets = self.buckets.entry(ip.clone()).or_insert(enum_map! {
|
let ip_buckets = self.buckets.entry(ip.clone()).or_insert(enum_map! {
|
||||||
_ => RateLimitBucket {
|
_ => RateLimitBucket {
|
||||||
last_checked: current,
|
last_checked: current,
|
||||||
allowance: -2.0,
|
allowance: rate as f32,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
#[allow(clippy::indexing_slicing)] // `EnumMap` has no `get` funciton
|
#[allow(clippy::indexing_slicing)] // `EnumMap` has no `get` funciton
|
||||||
let rate_limit = &mut ip_buckets[type_];
|
let rate_limit = &mut ip_buckets[type_];
|
||||||
let time_passed = current.duration_since(rate_limit.last_checked).as_secs() as f32;
|
let time_passed = current.duration_since(rate_limit.last_checked).as_secs() as f32;
|
||||||
|
|
||||||
// The initial value
|
|
||||||
if rate_limit.allowance == -2.0 {
|
|
||||||
rate_limit.allowance = rate as f32;
|
|
||||||
};
|
|
||||||
|
|
||||||
rate_limit.last_checked = current;
|
rate_limit.last_checked = current;
|
||||||
rate_limit.allowance += time_passed * (rate as f32 / per as f32);
|
rate_limit.allowance += time_passed * (rate as f32 / per as f32);
|
||||||
if rate_limit.allowance > rate as f32 {
|
if rate_limit.allowance > rate as f32 {
|
||||||
|
|
Loading…
Reference in a new issue