12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- use std:: { num:: NonZero , time:: Duration } ;
15
+ use std:: { num:: NonZeroU32 , time:: Duration } ;
16
16
17
17
use governor:: Quota ;
18
18
use schemars:: JsonSchema ;
@@ -54,8 +54,7 @@ pub struct LoginRateLimitingConfig {
54
54
pub struct RateLimiterConfiguration {
55
55
/// A one-off burst of actions that the user can perform
56
56
/// in one go without waiting.
57
- /// Replenishes at the rate.
58
- pub burst : u32 ,
57
+ pub burst : NonZeroU32 ,
59
58
/// How quickly the allowance replenishes, in number of actions per second.
60
59
/// Can be fractional to replenish slower.
61
60
pub per_second : f64 ,
@@ -82,10 +81,6 @@ impl ConfigurationSection for RateLimitingConfig {
82
81
// Check one limiter's configuration for errors
83
82
let error_on_limiter =
84
83
|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
-
89
84
let recip = limiter. per_second . recip ( ) ;
90
85
// period must be at least 1 nanosecond according to the governor library
91
86
if recip < 1.0e-9 || !recip. is_finite ( ) {
@@ -120,21 +115,20 @@ impl RateLimiterConfiguration {
120
115
if !reciprocal. is_finite ( ) {
121
116
return None ;
122
117
}
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 ) )
125
119
}
126
120
}
127
121
128
122
fn default_login_per_address ( ) -> RateLimiterConfiguration {
129
123
RateLimiterConfiguration {
130
- burst : 3 ,
124
+ burst : NonZeroU32 :: new ( 3 ) . unwrap ( ) ,
131
125
per_second : 3.0 / 60.0 ,
132
126
}
133
127
}
134
128
135
129
fn default_login_per_account ( ) -> RateLimiterConfiguration {
136
130
RateLimiterConfiguration {
137
- burst : 1800 ,
131
+ burst : NonZeroU32 :: new ( 1800 ) . unwrap ( ) ,
138
132
per_second : 1800.0 / 3600.0 ,
139
133
}
140
134
}
0 commit comments