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

configurable rate limit for registrars #2469

Open
wants to merge 6 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
9 changes: 4 additions & 5 deletions app/controllers/admin/registrars_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,10 @@ def set_test_date
response = base_get_request(uri: uri, port: ENV['registry_demo_registrar_port'])

if response.code == "200"
return record_result_for_each_api_user(response: response)
record_result_for_each_api_user(response: response)
else
return redirect_to request.referer, notice: 'Registrar no found'
redirect_to request.referer, notice: 'Something went wrong'
end

redirect_to request.referer, notice: 'Something goes wrong'
end

def remove_test_date
Expand Down Expand Up @@ -160,7 +158,8 @@ def registrar_params
:legaldoc_optout,
:legaldoc_optout_comment,
:iban,
:language)
:language,
:rate_limit)
end

def registry_vat_rate
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/epp/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def respond_with_session_limit_exceeded_error(exception)
code: '2502',
msg: Shunter.default_error_message)
handle_errors
log_exception(exception)
log_exception(exception) unless Rails.env.test?
end

def respond_with_command_failed_error(exception)
Expand Down
5 changes: 3 additions & 2 deletions app/lib/shunter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

module Shunter
class Base
attr_accessor :user_id, :adapter
attr_accessor :user_id, :adapter, :rate_limit

def initialize(options = {})
@user_id = options[:user_id]
adapter_klass = Shunter.default_adapter.constantize
@adapter = adapter_klass.new(options[:conn_options])
@rate_limit = options[:rate_limit]
end

def user_key
Expand Down Expand Up @@ -49,7 +50,7 @@ def init_counter(key)
end

def allowed_requests
Shunter.default_threshold
rate_limit.nil? ? Shunter.default_threshold : rate_limit
end

def timespan
Expand Down
4 changes: 3 additions & 1 deletion app/lib/shunter/integration/throttle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def throttle

user_id = throttled_user.id

shunter = Shunter::Base.new(conn_options: connection_options, user_id: user_id)
shunter = Shunter::Base.new(conn_options: connection_options,
user_id: user_id,
rate_limit: throttled_user.registrar.rate_limit)
if shunter.throttle
logger.info "Request from #{throttled_user.class}/#{throttled_user.id} is coming through throttling"
yield if block_given?
Expand Down
9 changes: 9 additions & 0 deletions app/views/admin/registrars/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@
</div>

<div class="panel-body">
<div class="form-group">
<div class="col-md-4 control-label">
<%= f.label :rate_limit %>
</div>
<div class="col-md-7">
<%= f.text_field :rate_limit, class: 'form-control' %>
</div>
</div>

<div class="form-group">
<div class="col-md-4 control-label">
<%= f.label :code %>
Expand Down
3 changes: 3 additions & 0 deletions app/views/admin/registrars/show/_details.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

<dt><%= Registrar.human_attribute_name :website %></dt>
<dd><%= registrar.website %></dd>

<dt><%= Registrar.human_attribute_name :rate_limit %></dt>
<dd><%= registrar.rate_limit %></dd>
</dl>
</div>
</div>
5 changes: 5 additions & 0 deletions config/application.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ lhv_keystore_password:
lhv_ca_file: # Needed only in dev mode
lhv_dev_mode: 'false'

shunter_default_timespan: "60"
shunter_default_threshold: "100"
shunter_redis_host: "localhost"
shunter_redis_port: "6379"

epp_session_timeout_seconds: '300'
contact_archivation_log_file_dir:

Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20221031114812_add_rate_limit_to_registrars.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddRateLimitToRegistrars < ActiveRecord::Migration[6.1]
def change
add_column :registrars, :rate_limit, :integer, null: true
end
end
Loading