Skip to content

Commit

Permalink
fix: expiration based on Redis clock
Browse files Browse the repository at this point in the history
  • Loading branch information
chris13524 committed Jan 5, 2024
1 parent e1d3acf commit 6ac7007
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1595,8 +1595,12 @@ async fn test_notify_idempotent(notify_server: &NotifyServerContext) {
#[tokio::test]
async fn test_token_bucket(notify_server: &NotifyServerContext) {
let key = Uuid::new_v4();
// Note: max_tokens, refill_interval, and refill_rate must be set properly to avoid flaky tests.
// Although a custom clock is used, the lua script still has expiration logic based on the clock of Redis not this custom one.
// The formula for the expiration is: math.ceil(((max_tokens - remaining) / refillRate)) * interval
// If the result of this expression is less than the time between the Redis calls, then the key can expire. Setting refill_duration to 10 seconds and refill_rate to 1 should be enough to avoid this.
let max_tokens = 2;
let refill_interval = chrono::Duration::milliseconds(500);
let refill_interval = chrono::Duration::seconds(60);
let refill_rate = 1;
let rate_limit = || async {
rate_limit::token_bucket_many(
Expand Down

0 comments on commit 6ac7007

Please sign in to comment.