@@ -12,6 +12,7 @@ use anyhow::Error;
1212use anyhow:: Result ;
1313use anyhow:: anyhow;
1414use context:: CoreContext ;
15+ use rate_limiting:: RateLimit ;
1516use sha2:: Digest ;
1617use sha2:: Sha256 ;
1718use slog:: debug;
@@ -50,12 +51,13 @@ pub async fn counter_check_and_bump<'a>(
5051 ctx : & ' a CoreContext ,
5152 counter : BoxGlobalTimeWindowCounter ,
5253 bump_value : f64 ,
53- rate_limit_name : & ' a str ,
54- max_value : f64 ,
55- time_window : u32 ,
54+ rate_limit : RateLimit ,
5655 enforced : bool ,
5756 scuba_extras : HashMap < & ' a str , & ' a str > ,
5857) -> Result < ( ) , Error > {
58+ let max_value = rate_limit. body . raw_config . limit ;
59+ let time_window = rate_limit. fci_metric . window . as_secs ( ) as u32 ;
60+
5961 let mut scuba = ctx. scuba ( ) . clone ( ) ;
6062 for ( key, val) in scuba_extras {
6163 scuba. add ( key, val) ;
@@ -65,43 +67,30 @@ pub async fn counter_check_and_bump<'a>(
6567 Ok ( Ok ( count) ) => {
6668 let new_value = count + bump_value;
6769 if new_value <= max_value {
68- debug ! (
69- ctx. logger( ) ,
70- "Rate-limiting counter {} ({}) does not exceed threshold {} if bumped" ,
71- rate_limit_name,
72- count,
73- max_value,
74- ) ;
7570 counter. bump ( bump_value) ;
7671 Ok ( ( ) )
7772 } else if !enforced {
7873 debug ! (
7974 ctx. logger( ) ,
80- "Rate-limiting counter {} ({}) exceeds threshold {} if bumped, but enforcement is disabled" ,
81- rate_limit_name ,
75+ "Rate-limiting counter {:? } (current value: {}) exceeds threshold {} if bumped, but enforcement is disabled" ,
76+ rate_limit ,
8277 count,
8378 max_value,
8479 ) ;
8580 let log_tag = "Request would have been rejected due to rate limiting, but enforcement is disabled" ;
86- let msg = format ! (
87- "Rate limit exceeded: {}, limit: {} (log only)" ,
88- rate_limit_name, max_value
89- ) ;
81+ let msg = format ! ( "Rate limit exceeded: {:?} (log only)" , rate_limit) ;
9082 scuba. log_with_msg ( log_tag, msg) ;
9183 Ok ( ( ) )
9284 } else {
9385 debug ! (
9486 ctx. logger( ) ,
95- "Rate-limiting counter {} ({}) exceeds threshold {} if bumped. Blocking request" ,
96- rate_limit_name ,
87+ "Rate-limiting counter {:? } (current_value: {}) exceeds threshold {} if bumped. Blocking request" ,
88+ rate_limit ,
9789 count,
9890 max_value,
9991 ) ;
10092 let log_tag = "Request rejected due to rate limiting" ;
101- let msg = format ! (
102- "Rate limit exceeded: {}, limit: {} (enforced)" ,
103- rate_limit_name, max_value
104- ) ;
93+ let msg = format ! ( "Rate limit exceeded: {:?} (enforced)" , rate_limit) ;
10594 scuba. log_with_msg ( log_tag, msg. clone ( ) ) ;
10695 Err ( anyhow ! ( msg) )
10796 }
@@ -111,14 +100,14 @@ pub async fn counter_check_and_bump<'a>(
111100 // or it's not been long enough. Bump and continue.
112101 debug ! (
113102 ctx. logger( ) ,
114- "Failed getting rate limiting counter {}: {:? }" , rate_limit_name , e
103+ "Failed getting rate limiting counter {:? }: {}" , rate_limit , e
115104 ) ;
116105 counter. bump ( bump_value) ;
117106 Ok ( ( ) )
118107 }
119108 Err ( _) => {
120109 let log_tag = "Rate limiting counter fetch timed out" ;
121- let msg = format ! ( "Rate limit {}: Timed out" , rate_limit_name ) ;
110+ let msg = format ! ( "Rate limit {:? }: Timed out" , rate_limit ) ;
122111 scuba. log_with_msg ( log_tag, Some ( msg) ) ;
123112 // Fail open to prevent DoS as we can't check the rate limit
124113 Ok ( ( ) )
@@ -134,6 +123,12 @@ mod test {
134123 use async_trait:: async_trait;
135124 use fbinit:: FacebookInit ;
136125 use mononoke_macros:: mononoke;
126+ use rate_limiting:: FciMetric ;
127+ use rate_limiting:: Metric ;
128+ use rate_limiting:: RateLimitBody ;
129+ use rate_limiting:: RateLimitStatus ;
130+ use rate_limiting:: Scope ;
131+ use rate_limiting:: Target ;
137132 use time_window_counter:: GlobalTimeWindowCounter ;
138133
139134 use super :: * ;
@@ -157,10 +152,24 @@ mod test {
157152 #[ mononoke:: fbinit_test]
158153 async fn test_counter_check_and_bump ( fb : FacebookInit ) {
159154 let ctx = CoreContext :: test_mock ( fb) ;
160- let rate_limit_name = "test" ;
161155 let max_value = 10.0 ;
162156 let scuba_extras = HashMap :: new ( ) ;
163157
158+ let limit = RateLimit {
159+ body : RateLimitBody {
160+ raw_config : rate_limiting_config:: RateLimitBody {
161+ limit : max_value,
162+ status : RateLimitStatus :: Enforced ,
163+ } ,
164+ } ,
165+ fci_metric : FciMetric {
166+ metric : Metric :: Commits ,
167+ window : Duration :: from_secs ( 1 ) ,
168+ scope : Scope :: Global ,
169+ } ,
170+ target : Some ( Target :: MainClientId ( "test_target" . to_string ( ) ) ) ,
171+ } ;
172+
164173 // Test case: Counter below maximum value
165174 let counter = Box :: new ( MockBoxGlobalTimeWindowCounter {
166175 count : Arc :: new ( Mutex :: new ( 5.0 ) ) ,
@@ -169,9 +178,7 @@ mod test {
169178 & ctx,
170179 counter,
171180 1.0 ,
172- rate_limit_name,
173- max_value,
174- 1 ,
181+ limit. clone ( ) ,
175182 true ,
176183 scuba_extras. clone ( ) ,
177184 )
@@ -185,9 +192,7 @@ mod test {
185192 & ctx,
186193 counter,
187194 1.0 ,
188- rate_limit_name,
189- max_value,
190- 1 ,
195+ limit. clone ( ) ,
191196 true ,
192197 scuba_extras. clone ( ) ,
193198 )
@@ -201,9 +206,7 @@ mod test {
201206 & ctx,
202207 counter,
203208 1.0 ,
204- rate_limit_name,
205- max_value,
206- 1 ,
209+ limit. clone ( ) ,
207210 true ,
208211 scuba_extras. clone ( ) ,
209212 )
@@ -214,17 +217,8 @@ mod test {
214217 let counter = Box :: new ( MockBoxGlobalTimeWindowCounter {
215218 count : Arc :: new ( Mutex :: new ( 11.0 ) ) ,
216219 } ) ;
217- let result = counter_check_and_bump (
218- & ctx,
219- counter,
220- 1.0 ,
221- rate_limit_name,
222- max_value,
223- 1 ,
224- false ,
225- scuba_extras. clone ( ) ,
226- )
227- . await ;
220+ let result =
221+ counter_check_and_bump ( & ctx, counter, 1.0 , limit, false , scuba_extras. clone ( ) ) . await ;
228222 assert ! ( result. is_ok( ) ) ;
229223 }
230224}
0 commit comments