ejfinneran / ratelimit

A Redis-backed rate limiter written in Ruby
MIT License
257 stars 55 forks source link

ratelimit.exceeded? checks >= rather than > #27

Open oehlschl opened 7 years ago

oehlschl commented 7 years ago

This is not a bug per se, but the behavior is unexpected; exceeded? returns true AT the threshold in addition to over it, which is unintuitive. This is reinforced by the language in the readme, which states that "the following code checks if the currently rate is over 10 executions in the last 30 seconds or not. ratelimit.exceeded?(phone_number, threshold: 10, interval: 30)"; in reality, the code checks if the rate is over 9 executions / equal-to-or-over 10 executions in the last 30 seconds.

The consequences of this depend on where .add() is called, but I personally feel like this line should be > rather than >=: https://github.com/ejfinneran/ratelimit/blob/177b1bf654f027705566cf2520fcd1ca0dc41536/lib/ratelimit.rb#L75

RateLimit.js does not implement exceeded, but the example linked from your readme also suggests > over >=: https://gist.github.com/chriso/54dd46b03155fcf555adccea822193da#get-the-code

I can work around this in my implementation, but it thought this was worth mentioning. Otherwise, thanks for the great gem.

oehlschl commented 7 years ago

I can also submit a PR if needed.