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

reduce request limit for resource intensive api calls #121

Merged
merged 1 commit into from
Aug 15, 2023
Merged
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
15 changes: 9 additions & 6 deletions config/rack_attack.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

puts "(API) >> Throttling enabled at #{LinkedData::OntologiesAPI.settings.req_per_second_per_ip} req/sec"
limit_req_ip = LinkedData::OntologiesAPI.settings.req_per_second_per_ip
limit_req_ip_heavy = limit_req_ip / 5
puts "(API) >> Throttling enabled at #{limit_req_ip} req/sec"

require 'rack/attack'
require 'redis-activesupport'
Expand Down Expand Up @@ -28,11 +30,12 @@
request.env['REMOTE_USER']&.admin?
end

Rack::Attack.throttle('requests by ip',
limit: LinkedData::OntologiesAPI.settings.req_per_second_per_ip,
period: 1.second
) do |request|
request.ip
Rack::Attack.throttle('req/ip/heavy', limit: limit_req_ip_heavy, period: 1.second) do |req|
req.ip if req.path.include?('/recommender') || req.path.include?('/annotator')
end

Rack::Attack.throttle('req/ip', limit: limit_req_ip, period: 1.second) do |req|
req.ip
end

Rack::Attack.throttled_responder = lambda do |request|
Expand Down
Loading