Skip to content

Commit e0bfbb2

Browse files
authored
Merge pull request rails#55000 from Shopify/rescue_dalli_timeout_in_read_multi_entries
Rescue connection related errors in MemCacheStore#read_multi_entries
2 parents 22b86f3 + cb07bf9 commit e0bfbb2

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

activesupport/lib/active_support/cache/mem_cache_store.rb

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -219,26 +219,24 @@ def write_serialized_entry(key, payload, **options)
219219
def read_multi_entries(names, **options)
220220
keys_to_names = names.index_by { |name| normalize_key(name, options) }
221221

222-
raw_values = begin
223-
@data.with { |c| c.get_multi(keys_to_names.keys) }
224-
rescue Dalli::UnmarshalError
225-
{}
226-
end
222+
rescue_error_with({}) do
223+
raw_values = @data.with { |c| c.get_multi(keys_to_names.keys) }
227224

228-
values = {}
225+
values = {}
229226

230-
raw_values.each do |key, value|
231-
entry = deserialize_entry(value, raw: options[:raw])
227+
raw_values.each do |key, value|
228+
entry = deserialize_entry(value, raw: options[:raw])
232229

233-
unless entry.nil? || entry.expired? || entry.mismatched?(normalize_version(keys_to_names[key], options))
234-
begin
235-
values[keys_to_names[key]] = entry.value
236-
rescue DeserializationError
230+
unless entry.nil? || entry.expired? || entry.mismatched?(normalize_version(keys_to_names[key], options))
231+
begin
232+
values[keys_to_names[key]] = entry.value
233+
rescue DeserializationError
234+
end
237235
end
238236
end
239-
end
240237

241-
values
238+
values
239+
end
242240
end
243241

244242
# Delete an entry from the cache.

activesupport/test/cache/stores/mem_cache_store_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,30 @@ def test_can_read_multi_entries_raw_values_from_dalli_store
371371
assert_equal({}, @cache.send(:read_multi_entries, [key]))
372372
end
373373

374+
def test_falls_back_to_default_value_when_client_raises_dalli_error
375+
cache = lookup_store
376+
client = cache.instance_variable_get(:@data)
377+
client.stub(:get_multi, lambda { |*_args| raise Dalli::DalliError.new("test error") }) do
378+
assert_equal({}, cache.read_multi("key1", "key2"))
379+
end
380+
end
381+
382+
def test_falls_back_to_default_value_when_client_raises_connection_pool_timeout_error
383+
cache = lookup_store
384+
client = cache.instance_variable_get(:@data)
385+
client.stub(:get_multi, lambda { |*_args| raise ConnectionPool::TimeoutError.new("test error") }) do
386+
assert_equal({}, cache.read_multi("key1", "key2"))
387+
end
388+
end
389+
390+
def test_falls_back_to_default_value_when_client_raises_connection_pool_error
391+
cache = lookup_store
392+
client = cache.instance_variable_get(:@data)
393+
client.stub(:get_multi, lambda { |*_args| raise ConnectionPool::Error.new("test error") }) do
394+
assert_equal({}, cache.read_multi("key1", "key2"))
395+
end
396+
end
397+
374398
def test_pool_options_work
375399
cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, pool: { size: 2, timeout: 1 })
376400
pool = cache.instance_variable_get(:@data) # loads 'connection_pool' gem

0 commit comments

Comments
 (0)