Skip to content

Commit

Permalink
Merge pull request #14 from olgen/master
Browse files Browse the repository at this point in the history
fix issue with wrong count on bucket_span == count(..interval)
  • Loading branch information
ejfinneran committed Aug 6, 2014
2 parents f97b31c + 68b254f commit 886bf4f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/ratelimit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def count(subject, interval)
count = (interval / @bucket_interval).floor
subject = "#{@key}:#{subject}"

keys = (0..count).map do |i|
(bucket - i + @bucket_count) % @bucket_count
keys = (0..count - 1).map do |i|
(bucket - i) % @bucket_count
end
return redis.hmget(subject, *keys).inject(0) {|a, i| a + i.to_i}
end
Expand Down
8 changes: 8 additions & 0 deletions spec/ratelimit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,12 @@
end
expect(@value).to be 1
end


it "counts correclty if bucket_span equals count-interval " do
@r = Ratelimit.new("key", {:bucket_span => 10, bucket_interval: 1})
@r.add('value1')
expect(@r.count('value1', 10)).to eql(1)
end

end

0 comments on commit 886bf4f

Please sign in to comment.