Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create method to only increase the counter #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.1.2

* Add new method to only increase counter and decide what happens next

## 0.1.1

* Remove maximum dependency versions
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ end
The status will tell you the current state. You can also see the current count
and how long until the bucket resets.

If you don't want to block the request but control what happens next:

```
# limit 30 requests per second.
twitter_rate = Rollie::RateLimiter.new("twitter_requests", limit: 30, interval: 1000)
status = twitter_rate.increase_counter # This will always count blocked regardless on how you initialized the class
if status.exceeded?
# Do Something here
else
# Do something different
end
```

```ruby
status.exceeded?
# => false
Expand Down
14 changes: 12 additions & 2 deletions lib/rollie/rate_limiter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ def within_limit
end
end

# Increase counter and let you decide what to do next. Will always count blocked
#
# @return [Status] The current status for this RateLimiter.
def increase_counter
Rollie.redis do |conn|
inc(conn, true)
end
end

# @return [Integer] The current count of this RateLimiter.
def count
Rollie.redis do |conn|
Expand All @@ -44,7 +53,8 @@ def count

private

def inc(conn)
def inc(conn, count_blocked = nil)
count_blocked = count_blocked.nil? ? @count_blocked : count_blocked
time = (Time.now.to_r * 1_000_000).round
old = time - @interval
range = conn.multi do
Expand All @@ -58,7 +68,7 @@ def inc(conn)
current_count = range.length
time_remaining = range.first.to_i - time + @interval

if exceeded && !@count_blocked
if exceeded && !count_blocked
conn.zremrangebyscore(@key, time, time)
current_count -= 1
end
Expand Down
23 changes: 23 additions & 0 deletions spec/rollie/rate_limiter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,29 @@ module Rollie
end
end

describe "#increase_counter" do
before do
@limiter = RateLimiter.new(SecureRandom.hex(8), count_blocked: false)
end

it "should return status" do
status = @limiter.increase_counter
expect(status.count).to eq(1)
expect(status.exceeded?).to be(false)
expect(status.time_remaining).to eq(1000)
end

it "should increase count regardless of count_blocked setting" do
35.times do
@limiter.increase_counter
end

status = @limiter.increase_counter
expect(status.count).to eq(36)
expect(status.exceeded?).to be(true)
end
end

describe '#count' do
it 'returns the current count' do
30.times do
Expand Down