Skip to content

Commit 8fcd6c8

Browse files
authored
Merge pull request #450 from fatkodima/better-failsafe
Do not rescue all errors for redis backed stores
2 parents 1f216e1 + 20ec4d3 commit 8fcd6c8

3 files changed

Lines changed: 41 additions & 24 deletions

File tree

lib/rack/attack/store_proxy/redis_cache_store_proxy.rb

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,17 @@ def increment(name, amount = 1, options = {})
1515
#
1616
# So in order to workaround this we use RedisCacheStore#write (which sets expiration) to initialize
1717
# the counter. After that we continue using the original RedisCacheStore#increment.
18-
rescuing do
19-
if options[:expires_in] && !read(name)
20-
write(name, amount, options)
18+
if options[:expires_in] && !read(name)
19+
write(name, amount, options)
2120

22-
amount
23-
else
24-
super
25-
end
21+
amount
22+
else
23+
super
2624
end
2725
end
2826

29-
def read(*_args)
30-
rescuing { super }
31-
end
32-
3327
def write(name, value, options = {})
34-
rescuing do
35-
super(name, value, options.merge!(raw: true))
36-
end
37-
end
38-
39-
private
40-
41-
def rescuing
42-
yield
43-
rescue Redis::BaseError
44-
nil
28+
super(name, value, options.merge!(raw: true))
4529
end
4630
end
4731
end

lib/rack/attack/store_proxy/redis_proxy.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def delete(key, _options = {})
4747

4848
def rescuing
4949
yield
50-
rescue Redis::BaseError
50+
rescue Redis::BaseConnectionError
5151
nil
5252
end
5353
end

spec/integration/offline_spec.rb

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
end
1414

1515
it 'should count' do
16-
@cache.send(:do_count, 'rack::attack::cache-test-key', 1)
16+
@cache.count('cache-test-key', 1)
17+
end
18+
19+
it 'should delete' do
20+
@cache.delete('cache-test-key')
1721
end
1822
end
1923

@@ -29,6 +33,18 @@
2933
end
3034
end
3135

36+
if defined?(Redis) && defined?(ActiveSupport::Cache::RedisCacheStore) && Redis::VERSION >= '4'
37+
describe 'when Redis is offline' do
38+
include OfflineExamples
39+
40+
before do
41+
@cache = Rack::Attack::Cache.new
42+
# Use presumably unused port for Redis client
43+
@cache.store = ActiveSupport::Cache::RedisCacheStore.new(host: '127.0.0.1', port: 3333)
44+
end
45+
end
46+
end
47+
3248
if defined?(::Dalli)
3349
describe 'when Memcached is offline' do
3450
include OfflineExamples
@@ -46,6 +62,23 @@
4662
end
4763
end
4864

65+
if defined?(::Dalli) && defined?(::ActiveSupport::Cache::MemCacheStore)
66+
describe 'when Memcached is offline' do
67+
include OfflineExamples
68+
69+
before do
70+
Dalli.logger.level = Logger::FATAL
71+
72+
@cache = Rack::Attack::Cache.new
73+
@cache.store = ActiveSupport::Cache::MemCacheStore.new('127.0.0.1:22122')
74+
end
75+
76+
after do
77+
Dalli.logger.level = Logger::INFO
78+
end
79+
end
80+
end
81+
4982
if defined?(Redis)
5083
describe 'when Redis is offline' do
5184
include OfflineExamples

0 commit comments

Comments
 (0)