Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvCW committed Oct 5, 2022
1 parent 3548f6b commit 29eb2d9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/rate_limit/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def throttle(**args)
end

def limit_exceeded?(**args)
Worker.new(**args).reloaded_limit_exceeded?
Worker.new(**args).limit_exceeded?
end

def reset_counters(**args)
Expand Down
2 changes: 1 addition & 1 deletion lib/rate_limit/throttler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module RateLimit
module Throttler
def throttle
return failure! if reloaded_limit_exceeded?
return failure! if limit_exceeded?

yield if block_given?

Expand Down
20 changes: 13 additions & 7 deletions lib/rate_limit/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module RateLimit
class Worker
include Throttler

attr_accessor :topic, :value, :limits, :windows, :exceeded_window, :result, :raise_errors, :only_failures
attr_accessor :topic, :value, :limits, :windows, :result, :raise_errors, :only_failures,
:exceeded_window, :exceeded_window_expires_at

def initialize(topic:, value:, raise_errors: false, only_failures: false)
@topic = topic.to_s
Expand All @@ -23,13 +24,9 @@ def clear_cache_counter
Window.clear_cache_counter(windows)
end

def reloaded_limit_exceeded?
@exceeded_window = Window.find_exceeded(windows)

limit_exceeded?
end

def limit_exceeded?
reload_limit_exceeded if exceeded_window.nil? || exceeded_window_expires_at >= Time.current

exceeded_window.present?
end

Expand All @@ -45,5 +42,14 @@ def failure!

raise Errors::LimitExceededError, result if raise_errors
end

private

def reload_limit_exceeded
@exceeded_window = Window.find_exceeded(windows)
return if @exceeded_window.nil?

@exceeded_window_expires_at = Time.now + @exceeded_window.interval
end
end
end
4 changes: 2 additions & 2 deletions spec/rate_limit/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@
expect(Hash).not_to have_received(:new).with(any_args)
end

it 'exceeded limit for atribute 1' do
it 'exceeded limit for attribute 1' do
expect(described_class.limit_exceeded?(**options)).to be(true)
end

it 'exceeded limit for atribute 2' do
it 'exceeded limit for attribute 2' do
expect(
described_class.limit_exceeded?(topic: topic_login, value: value_five)
).to be(true)
Expand Down
13 changes: 9 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# frozen_string_literal: true

require 'active_support/core_ext/kernel'
require 'rate_limit'
require 'byebug'
require 'simplecov'

SimpleCov.start
SimpleCov.start do
add_filter '/spec/'

enable_coverage :branch
end

require 'active_support/core_ext/kernel'
require 'byebug'
require 'rate_limit'

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand Down

0 comments on commit 29eb2d9

Please sign in to comment.