@@ -114,7 +114,7 @@ where
114114 /// If the cache can not make a decision it returns [`Action::Check`] indicating the returned
115115 /// quantity needs to be synchronized with a centralized store.
116116 ///
117- /// Whenever the cache returns [`Action::Check`], the cache requires a call to [`Self::update_quota `],
117+ /// Whenever the cache returns [`Action::Check`], the cache requires a call to [`Self::set_quota `],
118118 /// with a synchronized 'consumed' amount.
119119 pub fn check_quota ( & self , quota : Quota < T > , quantity : u64 ) -> Action {
120120 let cache = self . cache . pin ( ) ;
@@ -193,11 +193,14 @@ where
193193 }
194194 }
195195
196- /// Updates the quota state in the cache for the specified quota.
196+ /// Sets the quota state in the cache for the specified quota.
197+ ///
198+ /// `consumed` is the absolute value reported by the synchronized store (Redis) not a relative
199+ /// change. The value may also be negative, this may happen with refunded quotas.
197200 ///
198201 /// The cache will use the synchronized `consumed` value to derive future decisions whether it
199202 /// can accept quota requests.
200- pub fn update_quota ( & self , quota : Quota < T > , consumed : i64 ) {
203+ pub fn set_quota ( & self , quota : Quota < T > , consumed : i64 ) {
201204 let cache = self . cache . pin ( ) ;
202205
203206 // Consumed quota can be negative due to refunds, we choose to deal with negative quotas
@@ -366,7 +369,7 @@ mod tests {
366369
367370 // Sync internal state to 30, for this test, there will be 70 remaining, meaning the cache
368371 // is expected to accept 7 more items without requiring another sync.
369- cache. update_quota ( q1, 30 ) ;
372+ cache. set_quota ( q1, 30 ) ;
370373
371374 // A different key still needs a sync.
372375 assert_eq ! ( cache. check_quota( q2, 12 ) , Action :: Check ( 12 ) ) ;
@@ -384,14 +387,14 @@ mod tests {
384387 assert_eq ! ( cache. check_quota( q2, 3 ) , Action :: Check ( 3 ) ) ;
385388
386389 // Consumed state is absolute not relative.
387- cache. update_quota ( q1, 50 ) ;
390+ cache. set_quota ( q1, 50 ) ;
388391 // This is too much.
389392 assert_eq ! ( cache. check_quota( q2, 6 ) , Action :: Check ( 6 ) ) ;
390393 // Need another sync again.
391394 assert_eq ! ( cache. check_quota( q2, 1 ) , Action :: Check ( 1 ) ) ;
392395
393396 // Negative state can exist due to refunds.
394- cache. update_quota ( q1, -123 ) ;
397+ cache. set_quota ( q1, -123 ) ;
395398 // The cache considers a negative quota like `0`, `100` remaining quota -> 10 (= 10%).
396399 assert_eq ! ( cache. check_quota( q1, 10 ) , Action :: Accept ) ;
397400 // Too much, check the entire local usage.
@@ -404,7 +407,7 @@ mod tests {
404407
405408 let q1 = simple_quota ( 100 ) ;
406409
407- cache. update_quota ( q1, 0 ) ;
410+ cache. set_quota ( q1, 0 ) ;
408411 for _ in 0 ..100 {
409412 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Accept , ) ;
410413 }
@@ -426,39 +429,39 @@ mod tests {
426429 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Check ( 1 ) ) ;
427430
428431 // 700 remaining -> 70 per second -> 7 (10%).
429- cache. update_quota ( q1, 300 ) ;
432+ cache. set_quota ( q1, 300 ) ;
430433
431434 // 7 is the exact synchronization breakpoint.
432435 assert_eq ! ( cache. check_quota( q1, 8 ) , Action :: Check ( 8 ) ) ;
433436 // Under 7, but already over consumed before.
434437 assert_eq ! ( cache. check_quota( q1, 6 ) , Action :: Check ( 6 ) ) ;
435438
436439 // Reset.
437- cache. update_quota ( q1, 300 ) ;
440+ cache. set_quota ( q1, 300 ) ;
438441 // Under 7 -> that's fine.
439442 assert_eq ! ( cache. check_quota( q1, 7 ) , Action :: Accept ) ;
440443 // Way over the limit now.
441444 assert_eq ! ( cache. check_quota( q1, 90 ) , Action :: Check ( 97 ) ) ;
442445
443446 // 100 remaining -> 10 per second -> 1 (10%).
444- cache. update_quota ( q1, 900 ) ;
447+ cache. set_quota ( q1, 900 ) ;
445448 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Accept ) ;
446449 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Check ( 2 ) ) ;
447450
448451 // 90 remaining -> 9 per second -> 0 (10% floored).
449- cache. update_quota ( q1, 910 ) ;
452+ cache. set_quota ( q1, 910 ) ;
450453 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Check ( 1 ) ) ;
451454
452455 // Same for even less remaining.
453- cache. update_quota ( q1, 999 ) ;
456+ cache. set_quota ( q1, 999 ) ;
454457 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Check ( 1 ) ) ;
455458
456459 // Same for nothing remaining.
457- cache. update_quota ( q1, 1000 ) ;
460+ cache. set_quota ( q1, 1000 ) ;
458461 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Check ( 1 ) ) ;
459462
460463 // Same for less than nothing remaining.
461- cache. update_quota ( q1, 1001 ) ;
464+ cache. set_quota ( q1, 1001 ) ;
462465 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Check ( 1 ) ) ;
463466 }
464467
@@ -472,27 +475,27 @@ mod tests {
472475 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Check ( 1 ) ) ;
473476
474477 // 50 remaining -> 5 (10%), consumption still under limit threshold (70).
475- cache. update_quota ( q1, 50 ) ;
478+ cache. set_quota ( q1, 50 ) ;
476479 // Nothing special here.
477480 assert_eq ! ( cache. check_quota( q1, 5 ) , Action :: Accept ) ;
478481 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Check ( 6 ) ) ;
479482
480483 // 31 remaining -> 3 (10%), consumption still under limit threshold (70),
481484 // but maximum cached consumption would be *above* the threshold, this is currently
482485 // explicitly not considered (but this behaviour may be changed in the future).
483- cache. update_quota ( q1, 69 ) ;
486+ cache. set_quota ( q1, 69 ) ;
484487 assert_eq ! ( cache. check_quota( q1, 3 ) , Action :: Accept ) ;
485488 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Check ( 4 ) ) ;
486489
487490 // 30 remaining -> 3 (10%), *but* threshold (70%) is now reached.
488- cache. update_quota ( q1, 70 ) ;
491+ cache. set_quota ( q1, 70 ) ;
489492 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Check ( 1 ) ) ;
490493 // Sanity check, that exhausting the limit fully, still works.
491- cache. update_quota ( q1, 100 ) ;
494+ cache. set_quota ( q1, 100 ) ;
492495 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Check ( 1 ) ) ;
493496
494497 // Resetting consumption to a lower value (refunds) should still work.
495- cache. update_quota ( q1, 50 ) ;
498+ cache. set_quota ( q1, 50 ) ;
496499 assert_eq ! ( cache. check_quota( q1, 5 ) , Action :: Accept ) ;
497500 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Check ( 6 ) ) ;
498501 }
@@ -503,7 +506,7 @@ mod tests {
503506
504507 let q1 = simple_quota ( 100 ) ;
505508
506- cache. update_quota ( q1, 90 ) ;
509+ cache. set_quota ( q1, 90 ) ;
507510 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Accept ) ;
508511 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Check ( 2 ) ) ;
509512 }
@@ -515,7 +518,7 @@ mod tests {
515518 let q1 = simple_quota ( 100 ) ;
516519
517520 // A negative or `0` limit threshold essentially disables the cache.
518- cache. update_quota ( q1, 0 ) ;
521+ cache. set_quota ( q1, 0 ) ;
519522 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Check ( 1 ) ) ;
520523 }
521524
@@ -553,7 +556,7 @@ mod tests {
553556 } ;
554557
555558 // Sync internal state to an initial value.
556- cache. update_quota ( limit_100, 50 * window) ;
559+ cache. set_quota ( limit_100, 50 * window) ;
557560
558561 // With limit 100 there is enough (5) in the cache remaining.
559562 assert_eq ! ( cache. check_quota( limit_100, 3 ) , Action :: Accept ) ;
@@ -573,7 +576,7 @@ mod tests {
573576 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Check ( 1 ) ) ;
574577 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Check ( 1 ) ) ;
575578
576- cache. update_quota ( q1, 30_000_000_000 ) ;
579+ cache. set_quota ( q1, 30_000_000_000 ) ;
577580
578581 // Even after synchronization, we need to check immediately.
579582 assert_eq ! ( cache. check_quota( q1, 1 ) , Action :: Check ( 1 ) ) ;
@@ -597,8 +600,8 @@ mod tests {
597600 } ;
598601
599602 // Initialize the cache.
600- cache. update_quota ( q1, 0 ) ;
601- cache. update_quota ( q2, 0 ) ;
603+ cache. set_quota ( q1, 0 ) ;
604+ cache. set_quota ( q2, 0 ) ;
602605
603606 // Make sure some elements are stored in the cache.
604607 assert_eq ! ( cache. check_quota( q1, 9 ) , Action :: Accept ) ;
0 commit comments