mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-12-11 10:36:30 +00:00
Shrink rate limit allowance to f32
This commit is contained in:
parent
fc5ef73a98
commit
e3046a78f1
1 changed files with 8 additions and 8 deletions
|
@ -6,7 +6,7 @@ use tracing::debug;
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct RateLimitBucket {
|
struct RateLimitBucket {
|
||||||
last_checked: Instant,
|
last_checked: Instant,
|
||||||
allowance: f64,
|
allowance: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Hash, Debug, enum_map::Enum, Copy, Clone, AsRefStr)]
|
#[derive(Eq, PartialEq, Hash, Debug, enum_map::Enum, Copy, Clone, AsRefStr)]
|
||||||
|
@ -41,22 +41,22 @@ 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: -2f64,
|
allowance: -2.0,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
#[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 f64;
|
let time_passed = current.duration_since(rate_limit.last_checked).as_secs() as f32;
|
||||||
|
|
||||||
// The initial value
|
// The initial value
|
||||||
if rate_limit.allowance == -2f64 {
|
if rate_limit.allowance == -2.0 {
|
||||||
rate_limit.allowance = f64::from(rate);
|
rate_limit.allowance = rate as f32;
|
||||||
};
|
};
|
||||||
|
|
||||||
rate_limit.last_checked = current;
|
rate_limit.last_checked = current;
|
||||||
rate_limit.allowance += time_passed * (f64::from(rate) / f64::from(per));
|
rate_limit.allowance += time_passed * (rate as f32 / per as f32);
|
||||||
if rate_limit.allowance > f64::from(rate) {
|
if rate_limit.allowance > rate as f32 {
|
||||||
rate_limit.allowance = f64::from(rate);
|
rate_limit.allowance = rate as f32;
|
||||||
}
|
}
|
||||||
|
|
||||||
if rate_limit.allowance < 1.0 {
|
if rate_limit.allowance < 1.0 {
|
||||||
|
|
Loading…
Reference in a new issue