Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 8df3ec4

Browse files
committed
fixup! Add configuration for rate-limiting of logins, replacing hardcoded limits
1 parent 11abd7a commit 8df3ec4

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

crates/config/src/sections/rate_limiting.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::{num::NonZero, time::Duration};
15+
use std::{num::NonZeroU32, time::Duration};
1616

1717
use governor::Quota;
1818
use schemars::JsonSchema;
@@ -54,8 +54,7 @@ pub struct LoginRateLimitingConfig {
5454
pub struct RateLimiterConfiguration {
5555
/// A one-off burst of actions that the user can perform
5656
/// in one go without waiting.
57-
/// Replenishes at the rate.
58-
pub burst: u32,
57+
pub burst: NonZeroU32,
5958
/// How quickly the allowance replenishes, in number of actions per second.
6059
/// Can be fractional to replenish slower.
6160
pub per_second: f64,
@@ -82,10 +81,6 @@ impl ConfigurationSection for RateLimitingConfig {
8281
// Check one limiter's configuration for errors
8382
let error_on_limiter =
8483
|limiter: &RateLimiterConfiguration| -> Option<figment::error::Error> {
85-
if limiter.burst == 0 {
86-
return Some(figment::error::Error::custom("`burst` must not be zero, as this would mean the action could never be performed"));
87-
}
88-
8984
let recip = limiter.per_second.recip();
9085
// period must be at least 1 nanosecond according to the governor library
9186
if recip < 1.0e-9 || !recip.is_finite() {
@@ -120,21 +115,20 @@ impl RateLimiterConfiguration {
120115
if !reciprocal.is_finite() {
121116
return None;
122117
}
123-
let burst = NonZero::new(self.burst)?;
124-
Some(Quota::with_period(Duration::from_secs_f64(reciprocal))?.allow_burst(burst))
118+
Some(Quota::with_period(Duration::from_secs_f64(reciprocal))?.allow_burst(self.burst))
125119
}
126120
}
127121

128122
fn default_login_per_address() -> RateLimiterConfiguration {
129123
RateLimiterConfiguration {
130-
burst: 3,
124+
burst: NonZeroU32::new(3).unwrap(),
131125
per_second: 3.0 / 60.0,
132126
}
133127
}
134128

135129
fn default_login_per_account() -> RateLimiterConfiguration {
136130
RateLimiterConfiguration {
137-
burst: 1800,
131+
burst: NonZeroU32::new(1800).unwrap(),
138132
per_second: 1800.0 / 3600.0,
139133
}
140134
}

docs/config.schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1726,7 +1726,7 @@
17261726
"description": "A one-off burst of actions that the user can perform in one go without waiting. Replenishes at the rate.",
17271727
"type": "integer",
17281728
"format": "uint32",
1729-
"minimum": 0.0
1729+
"minimum": 1.0
17301730
},
17311731
"per_second": {
17321732
"description": "How quickly the allowance replenishes, in number of actions per second. Can be fractional to replenish slower.",

0 commit comments

Comments
 (0)