Skip to content

Commit

Permalink
throttle: more redis limit diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
wez committed Jan 6, 2025
1 parent aedf744 commit 8029d60
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/mod-redis/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl RedisServer {
// Generate configuration
if let Some(mut stdin) = daemon.stdin.take() {
stdin
.write_all(b"bind 127.0.0.1\nlogfile /dev/stdout\n")
.write_all(b"bind 127.0.0.1\nlogfile /dev/stdout\nloglevel debug\n")
.await?;
stdin.write_all(format!("port {port}\n").as_bytes()).await?;
stdin
Expand Down
14 changes: 14 additions & 0 deletions crates/throttle/src/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ local expires_ts = math.ceil(tonumber(ARGV[2]))
local limit = tonumber(ARGV[3])
local uuid = ARGV[4]
redis.log(redis.LOG_DEBUG, "limiter: going to call ZREMRANGEBYSCORE", KEYS[1], now_ts-1)
-- prune expired values
redis.call("ZREMRANGEBYSCORE", KEYS[1], "-inf", now_ts-1)
redis.log(redis.LOG_DEBUG, "limiter: going to call ZCARD", KEYS[1])
local count = redis.call("ZCARD", KEYS[1])
if count + 1 > limit then
-- find the next expiration time
redis.log(redis.LOG_DEBUG, "limiter: going to call ZRANGE", KEYS[1])
local smallest = redis.call("ZRANGE", KEYS[1], "-inf", "+inf", "BYSCORE", "LIMIT", 0, 1, "WITHSCORES")
-- smallest holds the uuid and its expiration time;
-- we want to just return the remaining time interval
Expand All @@ -32,6 +36,7 @@ if count + 1 > limit then
end
return math.ceil(smallest[2] - now_ts)
end
redis.log(redis.LOG_DEBUG, "limiter: going to call ZADD", KEYS[1], expires_ts, uuid)
redis.call("ZADD", KEYS[1], "NX", expires_ts, uuid)
return redis.status_reply('OK')
"#,
Expand Down Expand Up @@ -123,6 +128,15 @@ impl LimitSpecWithDuration {

tokio::time::sleep(Duration::from_secs(3)).await;
}
mod_redis::RedisValue::Double(next_expiration_interval) => {
if Instant::now() >= deadline {
return Err(Error::TooManyLeases(Duration::from_secs(
next_expiration_interval as u64,
)));
}

tokio::time::sleep(Duration::from_secs(3)).await;
}
value => {
return Err(anyhow!("acquire script succeeded but returned {value:?}").into());
}
Expand Down

0 comments on commit 8029d60

Please sign in to comment.