Skip to content

Commit

Permalink
reduce request limit for resource intensive api calls (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexskr authored Aug 15, 2023
1 parent dde48c2 commit f39bb01
Showing 1 changed file with 9 additions and 6 deletions.
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

0 comments on commit f39bb01

Please sign in to comment.