Skip to content

Commit

Permalink
Merge pull request #58 from fxn/master
Browse files Browse the repository at this point in the history
Ceil fractional retry_after and reset_after
  • Loading branch information
brandur authored Aug 6, 2022
2 parents 4eccae5 + 6ea9cc7 commit 608c454
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ impl Command for ThrottleCommand {

let (throttled, rate_limit_result) = limiter.rate_limit(key, quantity)?;

let mut retry_after = rate_limit_result.retry_after.num_seconds();
if rate_limit_result.retry_after.num_milliseconds() > 0 {
retry_after += 1
}

let mut reset_after = rate_limit_result.reset_after.num_seconds();
if rate_limit_result.reset_after.num_milliseconds() > 0 {
reset_after += 1
}

// Reply with an array containing rate limiting results. Note that
// Redis' support for interesting data types is quite weak, so we have
// to jam a few square pegs into round holes. It's a little messy, but
Expand All @@ -71,8 +81,8 @@ impl Command for ThrottleCommand {
r.reply_integer(if throttled { 1 } else { 0 })?;
r.reply_integer(rate_limit_result.limit)?;
r.reply_integer(rate_limit_result.remaining)?;
r.reply_integer(rate_limit_result.retry_after.num_seconds())?;
r.reply_integer(rate_limit_result.reset_after.num_seconds())?;
r.reply_integer(retry_after)?;
r.reply_integer(reset_after)?;

// Tell Redis that it's okay to replicate the command with the same
// parameters out to replicas.
Expand Down

0 comments on commit 608c454

Please sign in to comment.