mirror of
https://github.com/internetee/registry.git
synced 2025-08-01 23:42:04 +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
44
app/lib/shunter/integration/throttle.rb
Normal file
44
app/lib/shunter/integration/throttle.rb
Normal file
|
@ -0,0 +1,44 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'active_support/concern'
|
||||
|
||||
module Shunter
|
||||
module Integration
|
||||
module Throttle
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do |base|
|
||||
actions = base.const_defined?('THROTTLED_ACTIONS') && base.const_get('THROTTLED_ACTIONS')
|
||||
return if actions.blank?
|
||||
|
||||
around_action :throttle, only: actions
|
||||
|
||||
def throttle
|
||||
unless throttled_user.present? && Shunter.feature_enabled?
|
||||
yield if block_given?
|
||||
return
|
||||
end
|
||||
|
||||
user_id = throttled_user.id
|
||||
|
||||
shunter = Shunter::Base.new(conn_options: connection_options, user_id: user_id)
|
||||
if shunter.throttle
|
||||
logger.info "Request from #{throttled_user.class}/#{throttled_user.id} is coming through throttling"
|
||||
yield if block_given?
|
||||
else
|
||||
logger.info "Too many requests from #{throttled_user.class}/#{throttled_user.id}."
|
||||
raise Shunter::ThrottleError
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def connection_options
|
||||
Shunter::BASE_CONNECTION
|
||||
end
|
||||
|
||||
def logger
|
||||
Shunter::BASE_LOGGER
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue