Skip to content

Commit

Permalink
Introduce redis connection fault tolerance
Browse files Browse the repository at this point in the history
With fault tolerance on, which it always is, as it isn't configurable
yet, the value for any operation that raises a connection error is
`nil`. This is intended to make a site operational in the face of a
Redis outage by letting it serve computed content (via `fetch`).
  • Loading branch information
sorentwo committed Dec 8, 2015
1 parent 4ee2e42 commit 305f3ba
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/readthis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ module Readthis
def serializers
@serializers ||= Readthis::Serializers.new
end

def fault_tolerant?
true
end
end
2 changes: 2 additions & 0 deletions lib/readthis/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ def invoke(operation, key, &block)
instrument(operation, key) do
pool.with(&block)
end
rescue Redis::BaseConnectionError => error
raise error unless Readthis.fault_tolerant?
end

def extract_options!(array)
Expand Down
8 changes: 8 additions & 0 deletions spec/readthis/cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ def self.load(value)
cache.write('great-key', 'great')
expect(cache.fetch('great-key', nil)).to eq('great')
end

it 'serves computed content when the cache is down' do
allow(cache.pool).to receive(:with).and_raise(Redis::CannotConnectError)

computed = cache.fetch('error-key') { 'computed' }

expect(computed).to eq('computed')
end
end

describe '#read_multi' do
Expand Down

0 comments on commit 305f3ba

Please sign in to comment.