Skip to content

Commit

Permalink
add redis for rack:attack
Browse files Browse the repository at this point in the history
  • Loading branch information
wwahammy committed Jun 16, 2018
1 parent a1a2861 commit 84c1fa6
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions config/initializers/rack_attack.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
Rack::Attack.throttle("requests by ip", limit: 5, period: 2) do |request|
request.ip
if ENV['REDISTOGO_URL']
redis_client = Redis.connect(url: ENV['REDISTOGO_URL'])
Rack::Attack.cache.store = Rack::Attack::StoreProxy::RedisStoreProxy.new(redis_client)
end

Rack::Attack.throttle('requests by ip', limit: 5, period: 2, &:ip)

Rack::Attack.throttled_response = lambda do |env|
now = Time.now
match_data = env['rack.attack.match_data']
Expand All @@ -12,5 +15,5 @@
'X-RateLimit-Reset' => (now + (match_data[:period] - now.to_i % match_data[:period])).to_s
}

[ 429, headers, ["Throttled\n"]]
[429, headers, ["Throttled\n"]]
end

0 comments on commit 84c1fa6

Please sign in to comment.