Skip to content

Commit

Permalink
Adds rack-attack to throttle requests and use memcached for it (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
spilth authored Jul 29, 2024
1 parent e02c6d3 commit 0c810fe
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ gem 'omniauth', '1.9.2'
gem 'omniauth-oauth2'
gem 'omniauth-rails_csrf_protection'
gem 'puma', '~> 5.0'
gem 'rack-attack'
gem 'rails', '~> 7.0.6'
gem 'rsolr', '>= 1.0', '< 3'
gem 'sassc-rails', '~> 2.1'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ GEM
nio4r (~> 2.0)
racc (1.8.0)
rack (2.2.9)
rack-attack (6.7.0)
rack (>= 1.0, < 4)
rack-test (2.1.0)
rack (>= 1.3)
rails (7.0.8.4)
Expand Down Expand Up @@ -597,6 +599,7 @@ DEPENDENCIES
omniauth-oauth2
omniauth-rails_csrf_protection
puma (~> 5.0)
rack-attack
rails (~> 7.0.6)
rsolr (>= 1.0, < 3)
rspec-rails
Expand Down
2 changes: 1 addition & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
config.log_tags = [ :request_id ]

# Use a different cache store in production.
# config.cache_store = :mem_cache_store
config.cache_store = :mem_cache_store

# Use a real queuing backend for Active Job (and separate queues per environment).
# config.active_job.queue_adapter = :resque
Expand Down
8 changes: 8 additions & 0 deletions config/initializers/rack_attack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Rack::Attack
# Throttle all requests by IP (60rpm)
#
# Key: "rack::attack:#{Time.now.to_i/:period}:req/ip:#{req.ip}"
throttle('req/ip', limit: 300, period: 5.minutes) do |req|
req.ip unless req.path.start_with?('/assets')
end
end

0 comments on commit 0c810fe

Please sign in to comment.