mirror of
https://github.com/internetee/registry.git
synced 2025-08-03 08:22:05 +02:00
Move throttling feature from gem back to the app
This commit is contained in:
parent
83413213d9
commit
f17ef17d16
9 changed files with 230 additions and 2 deletions
63
app/lib/shunter/base.rb
Normal file
63
app/lib/shunter/base.rb
Normal file
|
@ -0,0 +1,63 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Shunter
|
||||
class Base
|
||||
attr_accessor :user_id, :adapter
|
||||
|
||||
def initialize(options = {})
|
||||
@user_id = options[:user_id]
|
||||
adapter_klass = Shunter.default_adapter.constantize
|
||||
@adapter = adapter_klass.new(options[:conn_options])
|
||||
end
|
||||
|
||||
def user_key
|
||||
"counting_#{@user_id}"
|
||||
end
|
||||
|
||||
def blocked_user_key
|
||||
"blocked_#{@user_id}"
|
||||
end
|
||||
|
||||
def throttle
|
||||
return false if blocked?
|
||||
|
||||
valid_counter?
|
||||
end
|
||||
|
||||
def blocked?
|
||||
adapter.find_counter(blocked_user_key).present?
|
||||
end
|
||||
|
||||
def valid_counter?
|
||||
if adapter.find_counter(user_key)
|
||||
number_of_requests = adapter.increment_counter(user_key)
|
||||
if number_of_requests > allowed_requests.to_i
|
||||
init_counter(blocked_user_key)
|
||||
return false
|
||||
end
|
||||
else
|
||||
init_counter(user_key)
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def init_counter(key)
|
||||
adapter.write_counter(key)
|
||||
adapter.expire_counter(key, timespan)
|
||||
end
|
||||
|
||||
def allowed_requests
|
||||
Shunter.default_threshold
|
||||
end
|
||||
|
||||
def timespan
|
||||
Shunter.default_timespan
|
||||
end
|
||||
|
||||
def logger
|
||||
Shunter::BASE_LOGGER
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue