Skip to content

Commit

Permalink
Merge pull request #44 from Shopify/reset
Browse files Browse the repository at this point in the history
add support for reset of RateQueue
  • Loading branch information
sbfaulkner committed Oct 28, 2023
2 parents a506844 + 4846fcc commit bd85adb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/limiter/rate_queue.rb
Expand Up @@ -8,10 +8,19 @@ def initialize(size, interval: 60, balanced: false, &blk)
@size = size
@interval = interval

@ring = balanced ? balanced_ring : unbalanced_ring
@head = 0
@balanced = balanced

@mutex = Mutex.new
@blk = blk

reset
end

def reset
@mutex.synchronize do
@ring = @balanced ? balanced_ring : unbalanced_ring
@head = 0
end
end

def shift
Expand Down
9 changes: 9 additions & 0 deletions test/limiter/rate_queue_test.rb
Expand Up @@ -36,6 +36,15 @@ def test_shift_is_rate_limited_across_multiple_threads
end
end

def test_shift_is_not_rate_limited
assert_elapsed(0) do
COUNT.times do
@queue.shift
@queue.reset
end
end
end

def test_block_was_called_on_rate_limit
@block_hit = false
@queue = RateQueue.new(RATE, interval: INTERVAL) { @block_hit = true }
Expand Down

0 comments on commit bd85adb

Please sign in to comment.