mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-04-27 15:07:41 +00:00
Merge pull request #450 from fatkodima/better-failsafe
Do not rescue all errors for redis backed stores
This commit is contained in:
commit
8fcd6c8559
3 changed files with 41 additions and 24 deletions
|
|
@ -15,33 +15,17 @@ module Rack
|
||||||
#
|
#
|
||||||
# So in order to workaround this we use RedisCacheStore#write (which sets expiration) to initialize
|
# So in order to workaround this we use RedisCacheStore#write (which sets expiration) to initialize
|
||||||
# the counter. After that we continue using the original RedisCacheStore#increment.
|
# the counter. After that we continue using the original RedisCacheStore#increment.
|
||||||
rescuing do
|
if options[:expires_in] && !read(name)
|
||||||
if options[:expires_in] && !read(name)
|
write(name, amount, options)
|
||||||
write(name, amount, options)
|
|
||||||
|
|
||||||
amount
|
amount
|
||||||
else
|
else
|
||||||
super
|
super
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def read(*_args)
|
|
||||||
rescuing { super }
|
|
||||||
end
|
|
||||||
|
|
||||||
def write(name, value, options = {})
|
def write(name, value, options = {})
|
||||||
rescuing do
|
super(name, value, options.merge!(raw: true))
|
||||||
super(name, value, options.merge!(raw: true))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def rescuing
|
|
||||||
yield
|
|
||||||
rescue Redis::BaseError
|
|
||||||
nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ module Rack
|
||||||
|
|
||||||
def rescuing
|
def rescuing
|
||||||
yield
|
yield
|
||||||
rescue Redis::BaseError
|
rescue Redis::BaseConnectionError
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,11 @@ OfflineExamples = Minitest::SharedExamples.new do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should count' do
|
it 'should count' do
|
||||||
@cache.send(:do_count, 'rack::attack::cache-test-key', 1)
|
@cache.count('cache-test-key', 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should delete' do
|
||||||
|
@cache.delete('cache-test-key')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -29,6 +33,18 @@ if defined?(::ActiveSupport::Cache::RedisStore)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if defined?(Redis) && defined?(ActiveSupport::Cache::RedisCacheStore) && Redis::VERSION >= '4'
|
||||||
|
describe 'when Redis is offline' do
|
||||||
|
include OfflineExamples
|
||||||
|
|
||||||
|
before do
|
||||||
|
@cache = Rack::Attack::Cache.new
|
||||||
|
# Use presumably unused port for Redis client
|
||||||
|
@cache.store = ActiveSupport::Cache::RedisCacheStore.new(host: '127.0.0.1', port: 3333)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if defined?(::Dalli)
|
if defined?(::Dalli)
|
||||||
describe 'when Memcached is offline' do
|
describe 'when Memcached is offline' do
|
||||||
include OfflineExamples
|
include OfflineExamples
|
||||||
|
|
@ -46,6 +62,23 @@ if defined?(::Dalli)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if defined?(::Dalli) && defined?(::ActiveSupport::Cache::MemCacheStore)
|
||||||
|
describe 'when Memcached is offline' do
|
||||||
|
include OfflineExamples
|
||||||
|
|
||||||
|
before do
|
||||||
|
Dalli.logger.level = Logger::FATAL
|
||||||
|
|
||||||
|
@cache = Rack::Attack::Cache.new
|
||||||
|
@cache.store = ActiveSupport::Cache::MemCacheStore.new('127.0.0.1:22122')
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
Dalli.logger.level = Logger::INFO
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if defined?(Redis)
|
if defined?(Redis)
|
||||||
describe 'when Redis is offline' do
|
describe 'when Redis is offline' do
|
||||||
include OfflineExamples
|
include OfflineExamples
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue