Merge pull request #450 from fatkodima/better-failsafe

Do not rescue all errors for redis backed stores
This commit is contained in:
Gonzalo Rodriguez 2019-10-18 17:44:23 -03:00 committed by GitHub
commit 8fcd6c8559
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 24 deletions

View file

@ -15,33 +15,17 @@ module Rack
#
# 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.
rescuing do
if options[:expires_in] && !read(name)
write(name, amount, options)
if options[:expires_in] && !read(name)
write(name, amount, options)
amount
else
super
end
amount
else
super
end
end
def read(*_args)
rescuing { super }
end
def write(name, value, options = {})
rescuing do
super(name, value, options.merge!(raw: true))
end
end
private
def rescuing
yield
rescue Redis::BaseError
nil
super(name, value, options.merge!(raw: true))
end
end
end

View file

@ -47,7 +47,7 @@ module Rack
def rescuing
yield
rescue Redis::BaseError
rescue Redis::BaseConnectionError
nil
end
end

View file

@ -13,7 +13,11 @@ OfflineExamples = Minitest::SharedExamples.new do
end
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
@ -29,6 +33,18 @@ if defined?(::ActiveSupport::Cache::RedisStore)
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)
describe 'when Memcached is offline' do
include OfflineExamples
@ -46,6 +62,23 @@ if defined?(::Dalli)
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)
describe 'when Redis is offline' do
include OfflineExamples