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

Add block support to rate queue #17

Merged
merged 3 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ class Widget

# limit the rate we can call tick to 5 times per second
# when the rate has been exceeded, a call to tick will block until the rate limit would not be exceeded
limit_method :tick, rate: 5, interval: 1
# and the provided block will be executed
limit_method(:tick, rate: 5, interval: 1) do
puts 'Limit reached'
end

...
end
Expand All @@ -55,13 +58,16 @@ end
### Advanced Usage

In cases where the mixin is not appropriate the `RateQueue` class can be used directly. As in the mixin examples above,
the `interval` parameter is optional (and defaults to 1 minute).
the `interval` parameter is optional (and defaults to 1 minute). It is also possible
to provide the block to `RateQueue`, which will be executed on each limit hit (useful for metrics).

``` ruby
class Widget
def initialize
# create a rate-limited queue which allows 10000 operations per hour
@queue = Limiter::RateQueue.new(10000, interval: 3600)
@queue = Limiter::RateQueue.new(10000, interval: 3600) do
puts "Hit the limit, waiting"
end
end

def tick
Expand Down
17 changes: 5 additions & 12 deletions lib/limiter/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@

module Limiter
module Mixin
def limit_method(method, rate:, interval: 60)
queue = RateQueue.new(rate, interval: interval)
def limit_method(method, rate:, interval: 60, &b)
queue = RateQueue.new(rate, interval: interval, &b)

mixin = Module.new do
if RUBY_VERSION < "2.7"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the gem require ruby 2.6+ we can simplify this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we? (ie. since 2.6 is still < 2.7)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think my testing showed that wouldn't work on 2.6

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ruby 2.7 has deprecated automatic conversion from a hash to keyword arguments.
We do not need to worry about 2.6 here, cos the syntax is valid for 2.0+

Details

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah... I get that... I just thought I'd tested on 2.6 and needed to handle differently... I may be misremembering :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Np, just for sure i installed Ruby 2.6.3 locally and checked - everything is ok.

But in the process of this check was revealed that travisci just does not work on the repo. Just letting you know

define_method(method) do |*args|
queue.shift
super(*args)
end
else
define_method(method) do |*args, **kwargs|
queue.shift
super(*args, **kwargs)
end
define_method(method) do |*args, **options, &blk|
queue.shift
options.empty? ? super(*args, &blk) : super(*args, **options, &blk)
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/limiter/rate_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ module Limiter
class RateQueue
EPOCH = 0.0

def initialize(size, interval: 60)
def initialize(size, interval: 60, &blk)
@size = size
@interval = interval

@ring = Array.new(size, EPOCH)
@head = 0
@mutex = Mutex.new
@blk = blk
end

def shift
Expand All @@ -33,6 +34,7 @@ def shift
def sleep_until(time)
interval = time - clock.time
return unless interval.positive?
@blk.call if @blk
clock.sleep(interval)
end

Expand Down
8 changes: 8 additions & 0 deletions test/limiter/mixin_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,13 @@ def test_keyword_arguments_are_passed
@object.tock(count: 321)
assert_equal 321, @object.ticks
end

def test_block_was_called_on_rate_limit
@block_hit = false
MixinTestClass.limit_method(:tick, rate: RATE, interval: INTERVAL) { @block_hit = true }
@object.tick
@object.tick
assert @block_hit
end
end
end
9 changes: 9 additions & 0 deletions test/limiter/rate_queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,14 @@ def test_shift_is_rate_limited_across_multiple_threads
threads.each(&:join)
end
end

def test_block_was_called_on_rate_limit
@block_hit = false
@queue = RateQueue.new(RATE, interval: INTERVAL) { @block_hit = true }
@queue.stubs(:clock).returns(FakeClock)
@queue.shift
@queue.shift
assert @block_hit
end
end
end