@@ -54,7 +54,6 @@ extern crate serde_derive;
5454#[ macro_use]
5555extern crate logger;
5656
57- use serde:: de:: { self , Deserialize , Deserializer , MapAccess , Visitor } ;
5857use std:: os:: unix:: io:: { AsRawFd , RawFd } ;
5958use std:: time:: Duration ;
6059use std:: { fmt, io} ;
@@ -290,67 +289,6 @@ impl fmt::Debug for RateLimiter {
290289 }
291290}
292291
293- impl < ' de > Deserialize < ' de > for RateLimiter {
294- fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
295- where
296- D : Deserializer < ' de > ,
297- {
298- #[ derive( Deserialize ) ]
299- #[ allow( non_camel_case_types) ]
300- enum Field {
301- bandwidth,
302- ops,
303- } ;
304-
305- struct RateLimiterVisitor ;
306-
307- impl < ' de > Visitor < ' de > for RateLimiterVisitor {
308- type Value = RateLimiter ;
309-
310- fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
311- formatter. write_str ( "struct RateLimiter" )
312- }
313-
314- fn visit_map < V > ( self , mut map : V ) -> Result < RateLimiter , V :: Error >
315- where
316- V : MapAccess < ' de > ,
317- {
318- let mut bandwidth: Option < TokenBucket > = None ;
319- let mut ops: Option < TokenBucket > = None ;
320- while let Some ( key) = map. next_key ( ) ? {
321- match key {
322- Field :: bandwidth => {
323- if bandwidth. is_some ( ) {
324- return Err ( de:: Error :: duplicate_field ( "bandwidth" ) ) ;
325- }
326- bandwidth = Some ( map. next_value ( ) ?) ;
327- }
328- Field :: ops => {
329- if ops. is_some ( ) {
330- return Err ( de:: Error :: duplicate_field ( "ops" ) ) ;
331- }
332- ops = Some ( map. next_value ( ) ?) ;
333- }
334- }
335- }
336- let bandwidth = bandwidth. unwrap_or_default ( ) ;
337- let ops = ops. unwrap_or_default ( ) ;
338- RateLimiter :: new (
339- bandwidth. size ,
340- bandwidth. one_time_burst ,
341- bandwidth. refill_time ,
342- ops. size ,
343- ops. one_time_burst ,
344- ops. refill_time ,
345- )
346- . map_err ( de:: Error :: custom)
347- }
348- }
349- const FIELDS : & ' static [ & ' static str ] = & [ "bandwidth" , "ops" ] ;
350- deserializer. deserialize_struct ( "RateLimiter" , FIELDS , RateLimiterVisitor )
351- }
352- }
353-
354292impl RateLimiter {
355293 // description
356294 fn make_bucket (
@@ -499,9 +437,14 @@ impl RateLimiter {
499437 /// Updates the parameters of the token buckets associated with this RateLimiter.
500438 // TODO: Pls note that, right now, the buckets become full after being updated.
501439 pub fn update_buckets ( & mut self , bytes : Option < TokenBucket > , ops : Option < TokenBucket > ) {
502- // TODO: We have to call make_bucket instead of directly assigning the bytes and/or ops
503- // because the input buckets are likely build via deserialization, which currently does not
504- // properly set up their internal state.
440+ // TODO: We should reconcile the create and update paths, such that they use the same data
441+ // format. Currently, the TokenBucket config data is used for create, but the live
442+ // TokenBucket objects are used for update.
443+ // We have to call make_bucket instead of directly assigning the bytes and/or ops
444+ // because the RateLimiter validates the TokenBucket config data (e.g. it nullifies
445+ // an unusable bucket with size 0). This is needed, because passing a 0-sized bucket is
446+ // the only method the user has to disable rate limiting. I.e. if the user passes `null`
447+ // as the token bucket config, the old config is left unchanged.
505448
506449 if let Some ( b) = bytes {
507450 self . bandwidth = Self :: make_bucket ( b. size , b. one_time_burst , b. refill_time ) ;
@@ -804,51 +747,9 @@ mod tests {
804747 //assert!(!l.consume(u64::max_value(), TokenType::Bytes));
805748 }
806749
807- #[ test]
808- fn test_rate_limiter_deserialization ( ) {
809- let jstr = r#"{
810- "bandwidth": { "size": 1000, "one_time_burst": 2000, "refill_time": 1000 },
811- "ops": { "size": 10, "one_time_burst": 20, "refill_time": 1000 }
812- }"# ;
813-
814- let x: RateLimiter = serde_json:: from_str ( jstr) . expect ( "deserialization failed." ) ;
815- assert_eq ! ( x. bandwidth. as_ref( ) . unwrap( ) . size, 1000 ) ;
816- assert_eq ! ( x. bandwidth. as_ref( ) . unwrap( ) . one_time_burst. unwrap( ) , 2000 ) ;
817- assert_eq ! ( x. bandwidth. as_ref( ) . unwrap( ) . refill_time, 1000 ) ;
818- assert_eq ! ( x. ops. as_ref( ) . unwrap( ) . size, 10 ) ;
819- assert_eq ! ( x. ops. as_ref( ) . unwrap( ) . one_time_burst. unwrap( ) , 20 ) ;
820- assert_eq ! ( x. ops. as_ref( ) . unwrap( ) . refill_time, 1000 ) ;
821-
822- let jstr = r#"{
823- "ops": { "size": 10, "one_time_burst": 20, "refill_time": 1000 }
824- }"# ;
825-
826- let x: RateLimiter = serde_json:: from_str ( jstr) . expect ( "deserialization failed." ) ;
827- assert ! ( x. bandwidth. is_none( ) ) ;
828- assert_eq ! ( x. ops. as_ref( ) . unwrap( ) . size, 10 ) ;
829- assert_eq ! ( x. ops. as_ref( ) . unwrap( ) . one_time_burst. unwrap( ) , 20 ) ;
830- assert_eq ! ( x. ops. as_ref( ) . unwrap( ) . refill_time, 1000 ) ;
831- assert_eq ! ( x. timer_active, false ) ;
832-
833- let jstr = r#"{
834- "bandwidth": { "size": 1000, "one_time_burst": 2000, "refill_time": 1000 },
835- "opz": { "size": 10, "one_time_burst": 20, "refill_time": 1000 }
836- }"# ;
837- assert ! ( serde_json:: from_str:: <RateLimiter >( jstr) . is_err( ) ) ;
838-
839- let jstr = r#"{
840- }"# ;
841- assert ! ( serde_json:: from_str:: <RateLimiter >( jstr) . is_ok( ) ) ;
842- }
843-
844750 #[ test]
845751 fn test_update_buckets ( ) {
846- let jstr = r#"{
847- "bandwidth": { "size": 1000, "one_time_burst": 2000, "refill_time": 1000 },
848- "ops": { "size": 10, "one_time_burst": 20, "refill_time": 1000 }
849- }"# ;
850-
851- let mut x: RateLimiter = serde_json:: from_str ( jstr) . unwrap ( ) ;
752+ let mut x = RateLimiter :: new ( 1000 , Some ( 2000 ) , 1000 , 10 , Some ( 20 ) , 1000 ) . unwrap ( ) ;
852753
853754 let initial_bw = x. bandwidth . clone ( ) ;
854755 let initial_ops = x. ops . clone ( ) ;
0 commit comments