diff --git a/lib/rack/attack/store_proxy/redis_cache_store_proxy.rb b/lib/rack/attack/store_proxy/redis_cache_store_proxy.rb index f4081be..78807f8 100644 --- a/lib/rack/attack/store_proxy/redis_cache_store_proxy.rb +++ b/lib/rack/attack/store_proxy/redis_cache_store_proxy.rb @@ -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 diff --git a/lib/rack/attack/store_proxy/redis_proxy.rb b/lib/rack/attack/store_proxy/redis_proxy.rb index 1bcb4b4..d4e6f3a 100644 --- a/lib/rack/attack/store_proxy/redis_proxy.rb +++ b/lib/rack/attack/store_proxy/redis_proxy.rb @@ -47,7 +47,7 @@ module Rack def rescuing yield - rescue Redis::BaseError + rescue Redis::BaseConnectionError nil end end diff --git a/spec/integration/offline_spec.rb b/spec/integration/offline_spec.rb index f9bacab..d496063 100644 --- a/spec/integration/offline_spec.rb +++ b/spec/integration/offline_spec.rb @@ -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