Skip to content

Commit

Permalink
Merge 850a50b into be6a61a
Browse files Browse the repository at this point in the history
  • Loading branch information
ryana committed Dec 30, 2014
2 parents be6a61a + 850a50b commit da4b9c9
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/redis_rate_limiter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def initialize key, redis, options = {}
#
# @param [String] subject A name to uniquely identify subject
# @param [time] time UNIX timestamp of event
def add subject, time = Time.now.to_i
def add subject, time = Time.now.to_f
subject = "#{@key}:#{subject}"
@redis.multi do
@redis.lpush(subject, time)
Expand All @@ -47,6 +47,6 @@ def exceeded? subject
subject = "#{@key}:#{subject}"
return false if @redis.llen(subject) < @limit
last = @redis.lindex(subject, -1)
Time.now.to_i - last.to_i < @interval
Time.now.to_f - last.to_f < @interval
end
end
2 changes: 1 addition & 1 deletion spec/redis_rate_limiter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
end
context "subject's list is at limit" do
it "should be false if last item in subject's list is outside interval" do
outside_interval_timestamp = Time.now.to_i - @rl.interval - 1
outside_interval_timestamp = Time.now.to_f - @rl.interval - 1
@rl.limit = 5
@rl.add(subject, outside_interval_timestamp)
4.times { @rl.add(subject) }
Expand Down

0 comments on commit da4b9c9

Please sign in to comment.