Skip to content

Commit

Permalink
Merge 9a19d13 into f8e3141
Browse files Browse the repository at this point in the history
  • Loading branch information
lautis committed Apr 7, 2014
2 parents f8e3141 + 9a19d13 commit 9fe6270
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 70 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ pkg

# For rubinius:
#*.rbc
Gemfile.lock
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ script: "bundle exec rake test"
rvm:
- 1.9.2
- 1.9.3
- rbx-19mode
- rbx
- jruby
- 2.0.0
- 2.1.1
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ group :test do
gem "shoulda", ">= 0"
gem "timecop"
gem "mocha", "~> 0.10"
gem "mock_redis", "~> 0.2.0"
gem "mock_redis", "~> 0.11.0"
gem "rake"
gem 'coveralls', require: false
end
60 changes: 0 additions & 60 deletions Gemfile.lock

This file was deleted.

13 changes: 5 additions & 8 deletions lib/ratelimit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def initialize(key, options = {})
def add(subject, count = 1)
bucket = get_bucket
subject = "#{@key}:#{subject}"
redis.multi do
redis.pipelined do
redis.hincrby(subject, bucket, count)
redis.hdel(subject, (bucket + 1) % @bucket_count)
redis.hdel(subject, (bucket + 2) % @bucket_count)
Expand All @@ -53,14 +53,11 @@ def count(subject, interval)
interval = [interval, @bucket_interval].max
count = (interval / @bucket_interval).floor
subject = "#{@key}:#{subject}"
counts = redis.multi do
redis.hget(subject, bucket)
count.downto(1) do
bucket -= 1
redis.hget(subject, (bucket + @bucket_count) % @bucket_count)
end

keys = (0..count).map do |i|
(bucket - i + @bucket_count) % @bucket_count
end
return counts.inject(0) {|a, i| a += i.to_i}
return redis.hmget(subject, *keys).inject(0) {|a, i| a + i.to_i}
end

# Check if the rate limit has been exceeded.
Expand Down

0 comments on commit 9fe6270

Please sign in to comment.