@@ -122,7 +122,13 @@ impl MemoryStore {
122122 let read = self . inner . read ( ) ;
123123 if let Some ( key_lock) = read. get ( key) {
124124 let mut state = key_lock. write ( ) ;
125- Some ( evaluate_state ( & mut state, limit, window_nanos, now_mono, now_unix) )
125+ Some ( evaluate_state (
126+ & mut state,
127+ limit,
128+ window_nanos,
129+ now_mono,
130+ now_unix,
131+ ) )
126132 } else {
127133 None
128134 }
@@ -153,7 +159,12 @@ impl MemoryStore {
153159// ---------------------------------------------------------------------------
154160
155161/// Create the initial key state for a new rate-limit key.
156- fn new_key_state ( algorithm : Algorithm , limit : u64 , now_mono : Nanos , now_unix : UnixSecs ) -> KeyState {
162+ fn new_key_state (
163+ algorithm : Algorithm ,
164+ limit : u64 ,
165+ now_mono : Nanos ,
166+ now_unix : UnixSecs ,
167+ ) -> KeyState {
157168 match algorithm {
158169 Algorithm :: FixedWindow => KeyState :: FixedWindow {
159170 count : 0 ,
@@ -171,18 +182,41 @@ fn new_key_state(algorithm: Algorithm, limit: u64, now_mono: Nanos, now_unix: Un
171182}
172183
173184/// Dispatch to the correct algorithm based on the key state variant.
174- fn evaluate_state ( state : & mut KeyState , limit : u64 , window_nanos : u64 , now_mono : Nanos , now_unix : UnixSecs ) -> DimResult {
185+ fn evaluate_state (
186+ state : & mut KeyState ,
187+ limit : u64 ,
188+ window_nanos : u64 ,
189+ now_mono : Nanos ,
190+ now_unix : UnixSecs ,
191+ ) -> DimResult {
175192 match state {
176193 KeyState :: FixedWindow {
177194 count,
178195 window_start,
179196 window_start_unix,
180- } => fixed_window ( count, window_start, window_start_unix, limit, window_nanos, now_mono, now_unix) ,
181- KeyState :: SlidingWindow { timestamps } => sliding_window ( timestamps, limit, window_nanos, now_mono, now_unix) ,
197+ } => fixed_window (
198+ count,
199+ window_start,
200+ window_start_unix,
201+ limit,
202+ window_nanos,
203+ now_mono,
204+ now_unix,
205+ ) ,
206+ KeyState :: SlidingWindow { timestamps } => {
207+ sliding_window ( timestamps, limit, window_nanos, now_mono, now_unix)
208+ }
182209 KeyState :: TokenBucket {
183210 tokens_milli,
184211 last_refill,
185- } => token_bucket ( tokens_milli, last_refill, limit, window_nanos, now_mono, now_unix) ,
212+ } => token_bucket (
213+ tokens_milli,
214+ last_refill,
215+ limit,
216+ window_nanos,
217+ now_mono,
218+ now_unix,
219+ ) ,
186220 }
187221}
188222
@@ -535,7 +569,11 @@ mod tests {
535569 // Advance past the 1-hour staleness threshold and trigger sweep.
536570 let stale_time = T0 + super :: TOKEN_BUCKET_STALE_NANOS + 1 ;
537571 store. sweep ( stale_time) ;
538- assert_eq ! ( store. inner. read( ) . len( ) , 0 , "stale fixed window key must be evicted" ) ;
572+ assert_eq ! (
573+ store. inner. read( ) . len( ) ,
574+ 0 ,
575+ "stale fixed window key must be evicted"
576+ ) ;
539577 }
540578
541579 #[ test]
@@ -547,18 +585,34 @@ mod tests {
547585 assert_eq ! ( store. inner. read( ) . len( ) , 1 ) ;
548586
549587 // Access after window elapses — the per-access cutoff drains all timestamps.
550- check ( & store, "sweep:sw" , 3 , Algorithm :: SlidingWindow , T0 + WINDOW + 1 ) ;
588+ check (
589+ & store,
590+ "sweep:sw" ,
591+ 3 ,
592+ Algorithm :: SlidingWindow ,
593+ T0 + WINDOW + 1 ,
594+ ) ;
551595 // Deque now has one fresh entry; sweep should keep it.
552596 store. sweep ( T0 + WINDOW + 1 ) ;
553- assert_eq ! ( store. inner. read( ) . len( ) , 1 , "active sliding window key must be kept" ) ;
597+ assert_eq ! (
598+ store. inner. read( ) . len( ) ,
599+ 1 ,
600+ "active sliding window key must be kept"
601+ ) ;
554602
555603 // Advance far enough that a sweep after window drain would evict.
556604 let far_future = T0 + WINDOW * 100 ;
557605 // Access once to create a timestamp, then advance past its window.
558606 check ( & store, "sweep:sw2" , 1 , Algorithm :: SlidingWindow , T0 ) ;
559607 let after_window = T0 + WINDOW + 1 ;
560608 // This access drains T0, adds after_window.
561- check ( & store, "sweep:sw2" , 1 , Algorithm :: SlidingWindow , after_window) ;
609+ check (
610+ & store,
611+ "sweep:sw2" ,
612+ 1 ,
613+ Algorithm :: SlidingWindow ,
614+ after_window,
615+ ) ;
562616 // Now advance far past, access to drain the deque with a blocked request.
563617 let _ = check ( & store, "sweep:sw2" , 1 , Algorithm :: SlidingWindow , far_future) ;
564618 // The above drains old entries and adds one new one; next access after that window:
@@ -591,7 +645,11 @@ mod tests {
591645
592646 let stale_time = T0 + super :: TOKEN_BUCKET_STALE_NANOS + 1 ;
593647 store. sweep ( stale_time) ;
594- assert_eq ! ( store. inner. read( ) . len( ) , 0 , "stale token bucket key must be evicted" ) ;
648+ assert_eq ! (
649+ store. inner. read( ) . len( ) ,
650+ 0 ,
651+ "stale token bucket key must be evicted"
652+ ) ;
595653 }
596654
597655 #[ test]
@@ -600,6 +658,10 @@ mod tests {
600658 check ( & store, "sweep:active" , 10 , Algorithm :: FixedWindow , T0 ) ;
601659 // Sweep at a time within the staleness threshold — key should be kept.
602660 store. sweep ( T0 + 1_000_000_000 ) ;
603- assert_eq ! ( store. inner. read( ) . len( ) , 1 , "active key must not be evicted" ) ;
661+ assert_eq ! (
662+ store. inner. read( ) . len( ) ,
663+ 1 ,
664+ "active key must not be evicted"
665+ ) ;
604666 }
605667}
0 commit comments