@@ -2,15 +2,15 @@ use anyhow::{anyhow, Context};
22use ipnetwork:: IpNetwork ;
33use oauth2:: { ClientId , ClientSecret } ;
44
5- use crate :: rate_limiter:: RateLimiter ;
5+ use crate :: rate_limiter:: { LimitedAction , RateLimiterConfig } ;
66use crate :: { env, env_optional, Env } ;
77
88use super :: base:: Base ;
99use super :: database_pools:: DatabasePools ;
1010use crate :: config:: balance_capacity:: BalanceCapacityConfig ;
1111use crate :: storage:: StorageConfig ;
1212use http:: HeaderValue ;
13- use std:: collections:: HashSet ;
13+ use std:: collections:: { HashMap , HashSet } ;
1414use std:: net:: IpAddr ;
1515use std:: time:: Duration ;
1616
@@ -30,7 +30,7 @@ pub struct Server {
3030 pub gh_client_secret : ClientSecret ,
3131 pub max_upload_size : u64 ,
3232 pub max_unpack_size : u64 ,
33- pub rate_limiter : RateLimiter ,
33+ pub rate_limiter : HashMap < LimitedAction , RateLimiterConfig > ,
3434 pub new_version_rate_limit : Option < u32 > ,
3535 pub blocked_traffic : Vec < ( String , Vec < String > ) > ,
3636 pub max_allowed_page_offset : u32 ,
@@ -140,6 +140,24 @@ impl Default for Server {
140140 . map ( |s| s. parse ( ) . expect ( "SERVER_THREADS was not a valid number" ) )
141141 . ok ( ) ;
142142
143+ // Dynamically load the configuration for all the rate limiting actions. See
144+ // `src/rate_limiter.rs` for their definition.
145+ let mut rate_limiter = HashMap :: new ( ) ;
146+ for action in LimitedAction :: VARIANTS {
147+ let env_var_key = action. env_var_key ( ) ;
148+ rate_limiter. insert (
149+ * action,
150+ RateLimiterConfig {
151+ rate : Duration :: from_secs (
152+ env_optional ( & format ! ( "RATE_LIMITER_{env_var_key}_RATE_SECONDS" ) )
153+ . unwrap_or_else ( || action. default_rate_seconds ( ) ) ,
154+ ) ,
155+ burst : env_optional ( & format ! ( "RATE_LIMITER_{env_var_key}_BURST" ) )
156+ . unwrap_or_else ( || action. default_burst ( ) ) ,
157+ } ,
158+ ) ;
159+ }
160+
143161 Server {
144162 db : DatabasePools :: full_from_environment ( & base) ,
145163 storage : StorageConfig :: from_environment ( ) ,
@@ -153,7 +171,7 @@ impl Default for Server {
153171 gh_client_secret : ClientSecret :: new ( env ( "GH_CLIENT_SECRET" ) ) ,
154172 max_upload_size : 10 * 1024 * 1024 , // 10 MB default file upload size limit
155173 max_unpack_size : 512 * 1024 * 1024 , // 512 MB max when decompressed
156- rate_limiter : Default :: default ( ) ,
174+ rate_limiter,
157175 new_version_rate_limit : env_optional ( "MAX_NEW_VERSIONS_DAILY" ) ,
158176 blocked_traffic : blocked_traffic ( ) ,
159177 max_allowed_page_offset : env_optional ( "WEB_MAX_ALLOWED_PAGE_OFFSET" ) . unwrap_or ( 200 ) ,
0 commit comments