Skip to content

Commit

Permalink
Avoid rate limiter slow down
Browse files Browse the repository at this point in the history
Use to_f instead of to_i to avoid slowing down the rate limiter
  • Loading branch information
ryana committed Dec 30, 2014
1 parent be6a61a commit 469fcfe
Showing 1 changed file with 2 additions and 2 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

0 comments on commit 469fcfe

Please sign in to comment.